Skip to content

Commit

Permalink
Typst writer: improved handling of primes.
Browse files Browse the repository at this point in the history
+ Use `'` instead of e.g. `prime`.
+ Don't put a space before primes.

Closes #239.
  • Loading branch information
jgm committed Sep 5, 2024
1 parent 7099b90 commit 86077b5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/Text/TeXMath/Writers/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ import Data.Maybe (fromMaybe)
-- | Transforms an expression tree to equivalent Typst
writeTypst :: DisplayType -> [Exp] -> Text
writeTypst dt exprs =
T.unwords $ map writeExp $ everywhere (mkT $ S.handleDownup dt) exprs
writeExps $ everywhere (mkT $ S.handleDownup dt) exprs

writeExps :: [Exp] -> Text
writeExps = T.intercalate " " . map writeExp
writeExps = go . map writeExp
where
go (a : b : es)
| T.take 1 b == "'" -- avoid space before a prime #239
= a <> go (b:es)
go (a : as)
= a <> if null as
then mempty
else " " <> go as
go [] = mempty

inParens :: Text -> Text
inParens s = "(" <> s <> ")"
Expand Down Expand Up @@ -101,6 +110,10 @@ writeExp (ENumber s) = s
writeExp (ESymbol _t s)
| T.all isAscii s = esc s -- use '+' not 'plus'
| s == "\x2212" = "-" -- use '-' not 'minus'
| s == "\8242" = "'" -- use ' for prime, see #239
| s == "\8243" = "''"
| s == "\8244" = "'''"
| s == "\8279" = "''''"
| otherwise = fromMaybe (esc s) $ M.lookup s typstSymbolMap
writeExp (EIdentifier s) =
if T.length s == 1
Expand Down
4 changes: 4 additions & 0 deletions test/regression/239.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<<< tex
f'_n
>>> typst
f'_n
2 changes: 1 addition & 1 deletion test/writer/typst/07.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
, EIdentifier "a"
]
>>> typst
u prime.double + p (x) u prime + q (x) u = f (x) , quad x > a
u'' + p (x) u' + q (x) u = f (x) , quad x > a
2 changes: 1 addition & 1 deletion test/writer/typst/primes1.test
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
, ESuper (ESymbol Accent "'") (ESymbol Accent "'")
]
>>> typst
x^2 + 2^2 + x^prime + x^(') + x^("''") + x prime + x^(')^(') + x^(')^2 + x^(' + ') + x^(')^(') + x^('^(')) + '^(') + '^(')
x^2 + 2^2 + x^(') + x^(') + x^("''") + x' + x^(')^(') + x^(')^2 + x^(' +') + x^(')^(') + x^('^(')) +'^(') +'^(')
2 changes: 1 addition & 1 deletion test/writer/typst/primes2.test
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
, ESuper (EIdentifier "H") (ESymbol Accent "\8279")
]
>>> typst
H^(\") H^(') H^(\*) H^(`) H^ª H^degree H^(²) H^(³) H^acute H^(¹) H^º H^quote.l.single H^quote.r.single H^quote.low.single H^quote.high.single H^quote.l.double H^quote.r.double H^quote.low.double H^quote.high.double H^prime H^prime.double H^prime.triple H^prime.rev H^prime.double.rev H^prime.triple.rev H^prime.quad
H^(\") H^(') H^(\*) H^(`) H^ª H^degree H^(²) H^(³) H^acute H^(¹) H^º H^quote.l.single H^quote.r.single H^quote.low.single H^quote.high.single H^quote.l.double H^quote.r.double H^quote.low.double H^quote.high.double H^(') H^('') H^(''') H^prime.rev H^prime.double.rev H^prime.triple.rev H^('''')

0 comments on commit 86077b5

Please sign in to comment.