Skip to content

Commit 790307f

Browse files
committed
Typst writer: don't add extra spaces...
...around `|` or escaped symbols. In some cases, e.g. with `\| x \|`, these spaces are significant and lead to spaces in the rendered formula. Closes #286.
1 parent a745e79 commit 790307f

File tree

8 files changed

+27
-23
lines changed

8 files changed

+27
-23
lines changed

src/Text/TeXMath/Writers/Typst.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ writeExps :: [Exp] -> Text
4343
writeExps = go . map writeExp
4444
where
4545
go (a : b : es)
46-
| T.take 1 b == "'" -- avoid space before a prime #239
46+
| Just (bstart, _) <- T.uncons b
47+
, Just (astart, _) <- T.uncons a
48+
, Just (_, aend) <- T.unsnoc a
49+
, bstart == '\'' -- avoid space before a prime #239
50+
|| bstart == '|' || aend == '|' || bstart == '\\' || astart == '\\' -- #286
4751
= a <> go (b:es)
4852
go (a : as)
4953
= a <> if null as

test/writer/typst/07.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
, EIdentifier "a"
2121
]
2222
>>> typst
23-
u'' + p (x) u' + q (x) u = f (x) \, quad x > a
23+
u'' + p (x) u' + q (x) u = f (x)\,quad x > a

test/writer/typst/08.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
, EDelimited "(" ")" [ Right (EIdentifier "z") ]
3131
]
3232
>>> typst
33-
lr(|macron(z)|) = lr(|z|) \, lr(|(macron(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/11.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
NormalFrac (ENumber "1") (ESub (EIdentifier "l") (ENumber "0"))
2424
]
2525
>>> typst
26-
phi.alt_n (kappa) = 0.033 C_n^2 kappa^(- 11 \/ 3) \, quad 1 / L_0 lt.double kappa lt.double 1 / l_0
26+
phi.alt_n (kappa) = 0.033 C_n^2 kappa^(- 11\/3)\,quad 1 / L_0 lt.double kappa lt.double 1 / l_0

test/writer/typst/complex1.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,22 +601,22 @@
601601
]
602602
]
603603
>>> typst
604-
upright("Bernoulli Trials") & P \( E \) = (n / k) p_()^k \( 1 - p \)_()^(n - k)\
604+
upright("Bernoulli Trials") & P\(E\) = (n / k) p_()^k\(1 - p\)_()^(n - k)\
605605
upright("Cauchy-Schwarz Inequality") & (sum_(k = 1)^n a_k^() b_k^())_()^2 lt.eq (sum_(k = 1)^n a_k^2) (sum_(k = 1)^n b_k^2)\
606-
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\
606+
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)\
608-
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") & accent(x, ˙)_() & = & sigma \( y - x \)\
608+
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") & accent(x, ˙)_() & = & sigma\(y - x\)\
610610
accent(y, ˙)_() & = & rho x - y - x z\
611611
accent(z, ˙)_() & = & - beta z + x y\
612612
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, ↼)_()\
613613
nabla zws dot.c accent(E, ↼)_() & = & 4 pi rho\
614614
nabla zws times accent(E, ↼)_() thin + thin 1 / c thin frac(partial zws accent(B, ↼)_(), partial zws t) & = & accent(0, ↼)_()\
615615
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)^()\
617-
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))))\
617+
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))\
619-
upright("Rogers-Ramanujan Identity") & 1 + sum_(k = 1)^oo frac(q_()^(k_()^2 + k), \( 1 - q \) \( 1 - q_()^2 \) dots.h.c \( 1 - q_()^k \)) = product_(j = 0)^oo frac(1, \( 1 - q_()^(5 j + 2) \) \( 1 - q_()^(5 j + 3) \)) \, upright("  ") upright("  ") f o r med \| q \| < 1 .\
619+
upright("Rogers-Ramanujan Identity") & 1 + sum_(k = 1)^oo frac(q_()^(k_()^2 + k), \(1 - q\)\(1 - q_()^2\)dots.h.c\(1 - q_()^k\)) = product_(j = 0)^oo frac(1, \(1 - q_()^(5 j + 2)\)\(1 - q_()^(5 j + 3)\))\,upright("  ") upright("  ") f o r med\|q\|< 1 .\
620620
upright("Commutative Diagram") & H & arrow.l & K\
621621
arrow.b & zws & arrow.t\
622622
H & arrow.r & K

test/writer/typst/complex2.test

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,16 @@
393393
>>> typst
394394
upright("Quadratic Equation") & x = frac(- b plus.minus sqrt(b_()^2 - 4 a c), 2 a)\
395395
upright("DisplayQuadratic Equation") & x = frac(- b plus.minus sqrt(b_()^2 - 4 a c), 2 a)\
396-
upright("Rational Function") & f \( x \) = frac(1 - x_()^2, 1 - x_()^3)\
397-
upright("Rational Function") & f \( x \) = frac(\( 1 - x_()^2 \) x_()^3, 1 - x_()^3)\
398-
upright("Rational Function") & f \( x \) = frac(\( 1 - x_()^2 \) \( x_()^3 - 5 x \), 1 - x_()^3)\
399-
upright("Parametrize Rational Function") & f \( x \) = frac(\( a_i^() - x_()^2 \)_()^5, 1 - x_()^3)\
400-
upright("Stacked exponents") & g \( z \) = e_()^(- x_()^2)\
401-
upright("Stacked exponents") & g \( z \) = e_()^(- \( z - a \)_()^2)\
402-
upright("Stacked exponents") & g \( z \) = e_()^(- sum_(i = 0)^oo z_i^2)\
403-
upright("Stacked exponents") & g \( y \) = e_()^(- sum_(i = 0)^oo y_i^2)\
404-
upright("Stacked exponents") & g \( z \) = e_()^(- sum_(i = 0)^oo z_()^(frac(2, a - i)))\
396+
upright("Rational Function") & f\(x\)= frac(1 - x_()^2, 1 - x_()^3)\
397+
upright("Rational Function") & f\(x\)= frac(\(1 - x_()^2\)x_()^3, 1 - x_()^3)\
398+
upright("Rational Function") & f\(x\)= frac(\(1 - x_()^2\)\(x_()^3 - 5 x\), 1 - x_()^3)\
399+
upright("Parametrize Rational Function") & f\(x\)= frac(\(a_i^() - x_()^2\)_()^5, 1 - x_()^3)\
400+
upright("Stacked exponents") & g\(z\)= e_()^(- x_()^2)\
401+
upright("Stacked exponents") & g\(z\)= e_()^(-\(z - a\)_()^2)\
402+
upright("Stacked exponents") & g\(z\)= e_()^(- sum_(i = 0)^oo z_i^2)\
403+
upright("Stacked exponents") & g\(y\)= e_()^(- sum_(i = 0)^oo y_i^2)\
404+
upright("Stacked exponents") & g\(z\)= e_()^(- sum_(i = 0)^oo z_()^(frac(2, a - i)))\
405405
upright("Cross Product") & frac(x_1^() - x_2^(), x_3^() - x_4^()) frac(x_1^() - x_4^(), x_2^() - x_3^())\
406-
upright("Cross Product") & \( frac(x_1^() - x_2^(), x_3^() - x_4^()) \) \( frac(x_1^() - x_4^(), x_2^() - x_3^()) \)\
406+
upright("Cross Product") & \(frac(x_1^() - x_2^(), x_3^() - x_4^())\)\(frac(x_1^() - x_4^(), x_2^() - x_3^())\)\
407407
upright("Cross Product") & (frac(x_1^() - x_2^(), x_3^() - x_4^())) (frac(x_1^() - x_4^(), x_2^() - x_3^()))\
408-
upright("Cross Product") & frac(\( x_1^() - x_2^() \) \( x_3^() - x_4^() \), \( x_1^() - x_4^() \) \( x_2^() - x_3^() \))
408+
upright("Cross Product") & frac(\(x_1^() - x_2^()\)\(x_3^() - x_4^()\), \(x_1^() - x_4^()\)\(x_2^() - x_3^()\))

test/writer/typst/differentiable_manifold.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@
5959
]
6060
]
6161
>>> typst
62-
gamma_1 equiv gamma_2 arrow.l.r.double {gamma_1 (0) = gamma_2 (0) = p \, upright(" and ")\
62+
gamma_1 equiv gamma_2 arrow.l.r.double {gamma_1 (0) = gamma_2 (0) = p\,upright(" and ")\
6363
frac(upright(d), upright(d) t) phi.alt compose gamma_1 (t)\|_(t = 0) = frac(upright(d), upright(d) t) phi.alt compose gamma_2 (t)\|_(t = 0)

test/writer/typst/sphere_volume.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
]
127127
]
128128
>>> typst
129-
S = { 0 lt.eq phi.alt lt.eq 2 pi \, med 0 lt.eq theta lt.eq pi \, med 0 lt.eq rho lt.eq R } upright(V o l u m e) & = integral.triple_S #h(-1em) rho^2 sin theta thin upright(d) rho thin upright(d) theta thin upright(d) phi.alt\
129+
S = { 0 lt.eq phi.alt lt.eq 2 pi\,med 0 lt.eq theta lt.eq pi\,med 0 lt.eq rho lt.eq R } upright(V o l u m e) & = integral.triple_S #h(-1em) rho^2 sin theta thin upright(d) rho thin upright(d) theta thin upright(d) phi.alt\
130130
& = integral_0^(2 pi) #h(-1em) upright(d) phi.alt thin integral_0^pi #h(-1em) sin theta thin upright(d) theta thin integral_0^R #h(-1em) rho^2 upright(d) rho\
131131
& = phi.alt #scale(x: 180%, y: 180%)[\|]_0^(2 pi) med (- cos theta) #scale(x: 180%, y: 180%)[\|]_0^pi med 1 / 3 rho^3 #scale(x: 180%, y: 180%)[\|]_0^R\
132132
& = 2 pi times 2 times 1 / 3 R^3\

0 commit comments

Comments
 (0)