Skip to content

Commit 1782269

Browse files
committed
Typst writer: Fix several issues with accents and attachments.
Closes #245.
1 parent 9c068cb commit 1782269

File tree

4 files changed

+170
-37
lines changed

4 files changed

+170
-37
lines changed

src/Text/TeXMath/Writers/Typst.hs

+52-29
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ escInQuotes t =
9494
needsEscape c = c == '\\' || c == '"'
9595

9696
writeExpS :: Exp -> Text
97-
writeExpS (EGrouped es) = "(" <> writeExps es <> ")"
9897
writeExpS e =
9998
case writeExp e of
99+
"" -> "()"
100+
t | Just (c,_) <- T.uncons t
101+
, c >= '\8988' && c <= '\8991'
102+
-> "\\" <> t -- see #245
100103
t | T.all (\c -> isDigit c || c == '.') t -> t
101104
| T.all (\c -> isAlpha c || c == '.') t -> t
102105
| otherwise -> "(" <> t <> ")"
@@ -143,36 +146,13 @@ writeExp (EOver _ (EOver _ b (ESymbol TOver "\9182")) e1) =
143146
"overbrace(" <> writeExp b <> ", " <> writeExp e1 <> ")"
144147
writeExp (EOver _ (EOver _ b (ESymbol TOver "\9140")) e1) =
145148
"overbracket(" <> writeExp b <> ", " <> writeExp e1 <> ")"
149+
writeExp (EOver _convertible b (ESymbol Accent ac))
150+
= case getAccentCommand ac of
151+
Just accCommand
152+
| not (isGrouped b) -> accCommand <> inParens (writeExp b)
153+
_ -> "accent" <> inParens (writeExp b <> ", " <> ac)
146154
writeExp (EOver _convertible b e1) =
147155
case e1 of
148-
ESymbol Accent "`" -> "grave" <> inParens (writeExp b)
149-
ESymbol Accent "\180" -> "acute" <> inParens (writeExp b)
150-
ESymbol Accent "^" -> "hat" <> inParens (writeExp b)
151-
ESymbol Accent "~" -> "tilde" <> inParens (writeExp b)
152-
ESymbol Accent "." -> "dot" <> inParens (writeExp b)
153-
ESymbol Accent "\168" -> "diaer" <> inParens (writeExp b)
154-
ESymbol Accent "\175" -> "macron" <> inParens (writeExp b)
155-
ESymbol Accent "\711" -> "caron" <> inParens (writeExp b)
156-
ESymbol Accent "\728" -> "breve" <> inParens (writeExp b)
157-
ESymbol Accent "\733" -> "acute.double" <> inParens (writeExp b)
158-
ESymbol Accent "\768" -> "grave" <> inParens (writeExp b)
159-
ESymbol Accent "\769" -> "acute" <> inParens (writeExp b)
160-
ESymbol Accent "\770" -> "hat" <> inParens (writeExp b)
161-
ESymbol Accent "\771" -> "tilde" <> inParens (writeExp b)
162-
ESymbol Accent "\772" -> "macron" <> inParens (writeExp b)
163-
ESymbol Accent "\773" -> "overline" <> inParens (writeExp b)
164-
ESymbol Accent "\774" -> "breve" <> inParens (writeExp b)
165-
ESymbol Accent "\775" -> "dot" <> inParens (writeExp b)
166-
ESymbol Accent "\776" -> "dot.double" <> inParens (writeExp b)
167-
ESymbol Accent "\777" -> "harpoon" <> inParens (writeExp b)
168-
ESymbol Accent "\778" -> "circle" <> inParens (writeExp b)
169-
ESymbol Accent "\779" -> "acute.double" <> inParens (writeExp b)
170-
ESymbol Accent "\780" -> "caron" <> inParens (writeExp b)
171-
ESymbol Accent "\781" -> "overline" <> inParens (writeExp b)
172-
ESymbol Accent "\x2218" -> "circle" <> inParens (writeExp b)
173-
ESymbol Accent "\x2192" -> "->" <> inParens (writeExp b)
174-
ESymbol Accent "\x2190" -> "<-" <> inParens (writeExp b)
175-
ESymbol Accent "\8407" -> "arrow" <> inParens (writeExp b)
176156
ESymbol TOver "\9182" -> "overbrace" <> inParens (writeExp b)
177157
ESymbol TOver "\9140" -> "overbracket" <> inParens (writeExp b)
178158
ESymbol TOver "\175" -> "overline" <> inParens (writeExp b)
@@ -184,6 +164,7 @@ writeExp (EUnder _ (EUnder _ b (ESymbol TUnder "\9140")) e1) =
184164
writeExp (EUnder _convertible b e1) =
185165
case e1 of
186166
ESymbol TUnder "_" -> "underline(" <> writeExp b <> ")"
167+
ESymbol TUnder "\817" -> "underline(" <> writeExp b <> ")"
187168
ESymbol TUnder "\9183" -> "underbrace(" <> writeExp b <> ")"
188169
ESymbol TUnder "\9140" -> "underbracket(" <> writeExp b <> ")"
189170
_ -> writeExpB b <> "_" <> writeExpS e1
@@ -317,3 +298,45 @@ typstSymbolMap :: M.Map Text Text
317298
typstSymbolMap = M.fromList $
318299
("\776", "dot.double") -- see #231
319300
: [(s,name) | (name, _, s) <- typstSymbols]
301+
302+
getAccentCommand :: Text -> Maybe Text
303+
getAccentCommand ac = do
304+
case ac of
305+
"`" -> Just "grave"
306+
"\180" -> Just "acute"
307+
"^" -> Just "hat"
308+
"~" -> Just "tilde"
309+
"." -> Just "dot"
310+
"\168" -> Just "diaer"
311+
"\175" -> Just "macron"
312+
"\711" -> Just "caron"
313+
"\728" -> Just "breve"
314+
"\733" -> Just "acute.double"
315+
"\768" -> Just "grave"
316+
"\769" -> Just "acute"
317+
"\770" -> Just "hat"
318+
"\771" -> Just "tilde"
319+
"\772" -> Just "macron"
320+
"\773" -> Just "overline"
321+
"\774" -> Just "breve"
322+
"\775" -> Just "dot"
323+
"\776" -> Just "dot.double"
324+
"\777" -> Just "harpoon"
325+
"\778" -> Just "circle"
326+
"\779" -> Just "acute.double"
327+
"\780" -> Just "caron"
328+
"\781" -> Just "overline"
329+
"\x2218" -> Just "circle"
330+
"\x2192" -> Just "->"
331+
"\x2190" -> Just "<-"
332+
"\8254" -> Just "macron"
333+
"\8400" -> Just "harpoon.lt"
334+
"\8401" -> Just "harpoon"
335+
"\8406" -> Just "arrow.l"
336+
"\8407" -> Just "arrow"
337+
"\8417" -> Just "arrow.l.r"
338+
_ -> Nothing
339+
340+
isGrouped :: Exp -> Bool
341+
isGrouped (EGrouped _) = True
342+
isGrouped _ = False

test/regression/245b.test

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<<< tex
2+
\begin{array}{c}
3+
\text{Accents above:}\\
4+
\text{\textbackslash grave} \grave{x}\\
5+
\text{\textbackslash acute} \acute{x}\\
6+
\text{\textbackslash hat} \hat{x}\\
7+
\text{\textbackslash widehat} \widehat{x}\\
8+
\text{\textbackslash tilde} \tilde{x}\\
9+
\text{\textbackslash bar} \bar{x}\\
10+
\text{\textbackslash overbar} \overbar{x}\\
11+
\text{\textbackslash overline} \overline{x}\\
12+
\text{\textbackslash breve} \breve{x}\\
13+
\text{\textbackslash dot} \dot{x}\\
14+
\text{\textbackslash ddot} \ddot{x}\\
15+
\text{\textbackslash ovhook} \ovhook{x}\\
16+
\text{\textbackslash ocirc} \ocirc{x}\\
17+
\text{\textbackslash check} \check{x}\\
18+
\text{\textbackslash candra} \candra{x}\\
19+
\text{\textbackslash oturnedcomma} \oturnedcomma{x}\\
20+
\text{\textbackslash ocommatopright} \ocommatopright{x}\\
21+
\text{\textbackslash droang} \droang{x}\\
22+
\text{\textbackslash leftharpoonaccent} \leftharpoonaccent{x}\\
23+
\text{\textbackslash rightharpoonaccent} \rightharpoonaccent{x}\\
24+
\text{\textbackslash overleftarrow} \overleftarrow{x}\\
25+
\text{\textbackslash vec} \vec{x}\\
26+
\text{\textbackslash dddot} \dddot{x}\\
27+
\text{\textbackslash ddddot} \ddddot{x}\\
28+
\text{\textbackslash overleftrightarrow} \overleftrightarrow{x}\\
29+
\text{\textbackslash annuity} \annuity{x}\\
30+
\text{\textbackslash widebridgeabove} \widebridgeabove{x}\\
31+
\text{\textbackslash asteraccent} \asteraccent{x}\\
32+
\text{Accents below:}\\
33+
\text{\textbackslash wideutilde} \wideutilde{x}\\
34+
\text{\textbackslash underbar} \underbar{x}\\
35+
\text{\textbackslash underline} \underline{x}\\
36+
\text{\textbackslash threeunderdot} \threeunderdot{x}\\
37+
\text{\textbackslash underrightharpoondown} \underrightharpoondown{x}\\
38+
\text{\textbackslash underleftharpoondown} \underleftharpoondown{x}\\
39+
\text{\textbackslash underleftarrow} \underleftarrow{x}\\
40+
\text{\textbackslash underrightarrow} \underrightarrow{x}\\
41+
\text{\textbackslash underline} \underline{x}\\
42+
\text{Trailing signs:}\\
43+
\text{^\textbackslash prime} x^\prime\\
44+
\text{^\textbackslash dprime} x^\dprime\\
45+
\text{^\textbackslash trprime} x^\trprime\\
46+
\text{^\textbackslash qprime} x^\qprime\\
47+
\text{^\textbackslash backprime} x^\backprime\\
48+
\text{^\textbackslash backdprime} x^\backdprime\\
49+
\text{^\textbackslash backtrprime} x^\backtrprime\\
50+
\text{^\textbackslash hyphenbullet} x^\hyphenbullet\\
51+
\text{^\textbackslash ast} x^\ast\\
52+
\text{^\textbackslash vysmwhtcircle} x^\vysmwhtcircle\\
53+
\text{^\textbackslash vysmblkcircle} x^\vysmblkcircle\\
54+
\text{^\textbackslash llcorner} x^\llcorner\\
55+
\text{^\textbackslash ulcorner} x^\ulcorner\\
56+
\end{array}
57+
>>> typst
58+
upright("Accents above:")\
59+
upright("\\grave") grave(x)\
60+
upright("\\acute") acute(x)\
61+
upright("\\hat") hat(x)\
62+
upright("\\widehat") hat(x)\
63+
upright("\\tilde") tilde(x)\
64+
upright("\\bar") macron(x)\
65+
upright("\\overbar") macron(x)\
66+
upright("\\overline") overline(x)\
67+
upright("\\breve") breve(x)\
68+
upright("\\dot") dot(x)\
69+
upright("\\ddot") dot.double(x)\
70+
upright("\\ovhook") harpoon(x)\
71+
upright("\\ocirc") circle(x)\
72+
upright("\\check") caron(x)\
73+
upright("\\candra") accent(x, ̐)\
74+
upright("\\oturnedcomma") accent(x, ̒)\
75+
upright("\\ocommatopright") accent(x, ̕)\
76+
upright("\\droang") accent(x, ̚)\
77+
upright("\\leftharpoonaccent") harpoon.lt(x)\
78+
upright("\\rightharpoonaccent") harpoon(x)\
79+
upright("\\overleftarrow") arrow.l(x)\
80+
upright("\\vec") arrow(x)\
81+
upright("\\dddot") accent(x, ⃛)\
82+
upright("\\ddddot") accent(x, ⃜)\
83+
upright("\\overleftrightarrow") arrow.l.r(x)\
84+
upright("\\annuity") accent(x, ⃧)\
85+
upright("\\widebridgeabove") accent(x, ⃩)\
86+
upright("\\asteraccent") accent(x, ⃰)\
87+
upright("Accents below:")\
88+
upright("\\wideutilde") accent(x, ̰)\
89+
upright("\\underbar") underline(x)\
90+
upright("\\underline") underline(x)\
91+
upright("\\threeunderdot") accent(x, ⃨)\
92+
upright("\\underrightharpoondown") accent(x, ⃬)\
93+
upright("\\underleftharpoondown") accent(x, ⃭)\
94+
upright("\\underleftarrow") accent(x, ⃮)\
95+
upright("\\underrightarrow") accent(x, ⃯)\
96+
upright("\\underline") underline(x)\
97+
upright("Trailing signs:")\
98+
upright("^\\prime") x^(')\
99+
upright("^\\dprime") x^('')\
100+
upright("^\\trprime") x^(''')\
101+
upright("^\\qprime") x^('''')\
102+
upright("^\\backprime") x^prime.rev\
103+
upright("^\\backdprime") x^prime.double.rev\
104+
upright("^\\backtrprime") x^prime.triple.rev\
105+
upright("^\\hyphenbullet") x^(⁃)\
106+
upright("^\\ast") x^(\*)\
107+
upright("^\\vysmwhtcircle") x^circle.stroked.tiny\
108+
upright("^\\vysmblkcircle") x^circle.filled.small\
109+
upright("^\\llcorner") x^\⌞\
110+
upright("^\\ulcorner") x^\⌜\

test/writer/typst/08.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
, EDelimited "(" ")" [ Right (EIdentifier "z") ]
3131
]
3232
>>> typst
33-
lr(|z^(‾)|) = lr(|z|) \, lr(|(z^(‾))^n|) = lr(|z|)^n \, arg (z^n) = n arg (z)
33+
lr(|macron(z)|) = lr(|z|) \, lr(|(macron(z))^n|) = lr(|z|)^n \, arg (z^n) = n arg (z)

test/writer/typst/complex1.test

+7-7
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,13 @@ upright("Cauchy-Schwarz Inequality") & (sum_(k = 1)^n a_k^() b_k^())_()^2 lt.eq
606606
upright("Cauchy Formula") & f \( z \) thin dot.c "Ind"_gamma^() \( z \) = frac(1, 2 pi i) integral.cont_gamma^() frac(f \( xi \), xi - z) thin d xi\
607607
upright("Cross Product") & V_1^() times V_2^() = mat(delim: "|", i, j, k; frac(partial X, partial u), frac(partial Y, partial u), 0; frac(partial X, partial v), frac(partial Y, partial v), 0)\
608608
upright("Vandermonde Determinant") & mat(delim: "|", 1, 1, dots.h.c, 1; v_1^(), v_2^(), dots.h.c, v_n^(); v_1^2, v_2^2, dots.h.c, v_n^2; dots.v, dots.v, dots.down, dots.v; v_1^(n - 1), v_2^(n - 1), dots.h.c, v_n^(n - 1)) = product_(1 lt.eq i < j lt.eq n)^() \( v_j^() - v_i^() \)\
609-
upright("Lorenz Equations") & x^(˙)_() & = & sigma \( y - x \)\
610-
y^(˙)_() & = & rho x - y - x z\
611-
z^(˙)_() & = & - beta z + x y\
612-
upright("Maxwell's Equations") & {nabla zws times B^harpoon.lt_() - thin 1 / c thin frac(partial zws E^harpoon.lt_(), partial zws t) & = & frac(4 pi, c) thin j^harpoon.lt_()\
613-
nabla zws dot.c E^harpoon.lt_() & = & 4 pi rho\
614-
nabla zws times E^harpoon.lt_() thin + thin 1 / c thin frac(partial zws B^harpoon.lt_(), partial zws t) & = & 0^harpoon.lt_()\
615-
nabla zws dot.c B^harpoon.lt_() & = & 0\
609+
upright("Lorenz Equations") & accent(x, ˙)_() & = & sigma \( y - x \)\
610+
accent(y, ˙)_() & = & rho x - y - x z\
611+
accent(z, ˙)_() & = & - beta z + x y\
612+
upright("Maxwell's Equations") & {nabla zws times accent(B, ↼)_() - thin 1 / c thin frac(partial zws accent(E, ↼)_(), partial zws t) & = & frac(4 pi, c) thin accent(j, ↼)_()\
613+
nabla zws dot.c accent(E, ↼)_() & = & 4 pi rho\
614+
nabla zws times accent(E, ↼)_() thin + thin 1 / c thin frac(partial zws accent(B, ↼)_(), partial zws t) & = & accent(0, ↼)_()\
615+
nabla zws dot.c accent(B, ↼)_() & = & 0\
616616
upright("Einstein Field Equations") & R_(mu nu)^() - 1 / 2 thin g_(mu nu)^() thin R = frac(8 pi G, c_()^4) thin T_(mu nu)^()\
617617
upright("Ramanujan Identity") & frac(1, \( sqrt(phi sqrt(5)) - phi \) e_()^(25 / pi)) = 1 + frac(e_()^(- 2 pi), 1 + frac(e_()^(- 4 pi), 1 + frac(e_()^(- 6 pi), 1 + frac(e_()^(- 8 pi), 1 + dots.h))))\
618618
upright("Another Ramanujan identity") & sum_(k = 1)^oo 1 / 2_()^(floor.l k dot.c zws phi floor.r) = frac(1, 2_()^0 + frac(1, 2_()^1 + dots.h.c))\

0 commit comments

Comments
 (0)