Skip to content

Commit fa823f5

Browse files
committed
Better handling of primes in eqn, typst, and tex writers.
These writers now render a superscript containing prime characters using a sequence of `'` characters, e.g. `f''`. This syntax is supported by all three formats.
1 parent dabc57e commit fa823f5

File tree

10 files changed

+40
-20
lines changed

10 files changed

+40
-20
lines changed

src/Text/TeXMath/Writers/Eqn.hs

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2020

2121
module Text.TeXMath.Writers.Eqn (writeEqn) where
2222

23+
import Text.TeXMath.Shared (toPrimes)
2324
import Data.List (transpose)
2425
import Data.Char (isAscii, ord)
2526
import qualified Data.Text as T
@@ -163,9 +164,17 @@ writeExp (EFraction fractype e1 e2) = writeExp' e1 <> op <> writeExp' e2
163164
then " / "
164165
else " over "
165166
writeExp (ESub b e1) = writeExp' b <> " sub " <> writeExp' e1
166-
writeExp (ESuper b e1) = writeExp' b <> " sup " <> writeExp' e1
167+
writeExp (ESuper b e1) = writeExp' b <>
168+
case e1 of
169+
ESymbol Ord s
170+
| Just ps <- toPrimes s -> ps
171+
_ -> " sup " <> writeExp' e1
167172
writeExp (ESubsup b e1 e2) =
168-
writeExp' b <> " sub " <> writeExp' e1 <> " sup " <> writeExp' e2
173+
writeExp' b <> " sub " <> writeExp' e1 <>
174+
case e2 of
175+
ESymbol Ord s
176+
| Just cs <- toPrimes s -> writeExp' b <> cs
177+
_ -> " sup " <> writeExp' e2
169178
writeExp (EOver _convertible b e1) =
170179
writeExp' b <> " to " <> writeExp' e1
171180
writeExp (EUnder _convertible b e1) =

src/Text/TeXMath/Writers/TeX.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,19 @@ writeExp (ESub b e1) = do
200200
writeExp (ESuper b e1) = do
201201
(if isFancy b then tellGroup else id) $ writeExp b
202202
case e1 of
203-
ESymbol Ord "\8242" -> tell [Token '\'']
203+
ESymbol Ord s
204+
| Just ps <- S.toPrimes s -> tell (replicate (T.length ps) (Token '\''))
204205
_ -> do tell [Token '^']
205206
tellGroup (writeExp e1)
206207
writeExp (ESubsup b e1 e2) = do
207208
(if isFancy b then tellGroup else id) $ writeExp b
208209
tell [Token '_']
209210
tellGroup (writeExp e1)
210-
tell [Token '^']
211-
tellGroup (writeExp e2)
211+
case e2 of
212+
ESymbol Ord s
213+
| Just ps <- S.toPrimes s -> tell (replicate (T.length ps) (Token '\''))
214+
_ -> do tell [Token '^']
215+
tellGroup (writeExp e2)
212216
writeExp (EOver convertible b e1) = do
213217
env <- asks mathEnv
214218
case xarrow b of

src/Text/TeXMath/Writers/Typst.hs

+9-2
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,16 @@ writeExp (EFraction _fractype e1 e2) =
139139
(_, EGrouped _) -> "frac(" <> writeExp e1 <> ", " <> writeExp e2 <> ")"
140140
_ -> writeExp e1 <> " / " <> writeExp e2
141141
writeExp (ESub b e1) = writeExpB b <> "_" <> writeExpS e1
142-
writeExp (ESuper b e1) = writeExpB b <> "^" <> writeExpS e1
142+
writeExp (ESuper b e1) = writeExpB b <>
143+
case e1 of
144+
ESymbol Ord t
145+
| Just ps <- S.toPrimes t -> ps
146+
_ -> "^" <> writeExpS e1
143147
writeExp (ESubsup b e1 e2) = writeExpB b <> "_" <> writeExpS e1 <>
144-
"^" <> writeExpS e2
148+
case e2 of
149+
ESymbol Ord t
150+
| Just ps <- S.toPrimes t -> ps
151+
_ -> "^" <> writeExpS e2
145152
writeExp (EOver _ (EOver _ b (ESymbol TOver "\9182")) e1) =
146153
"overbrace(" <> writeExp b <> ", " <> writeExp e1 <> ")"
147154
writeExp (EOver _ (EOver _ b (ESymbol TOver "\9140")) e1) =

test/regression/245b.test

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ upright("\\underleftarrow") accent(x, ⃮)\
9595
upright("\\underrightarrow") accent(x, ⃯)\
9696
upright("\\underline") underline(x)\
9797
upright("Trailing signs:")\
98-
upright("^\\prime") x^(')\
99-
upright("^\\dprime") x^('')\
100-
upright("^\\trprime") x^(''')\
101-
upright("^\\qprime") x^('''')\
98+
upright("^\\prime") x'\
99+
upright("^\\dprime") x''\
100+
upright("^\\trprime") x'''\
101+
upright("^\\qprime") x''''\
102102
upright("^\\backprime") x^prime.rev\
103103
upright("^\\backdprime") x^prime.double.rev\
104104
upright("^\\backtrprime") x^prime.triple.rev\

test/writer/eqn/complex3.test

+4-4
Large diffs are not rendered by default.

test/writer/eqn/moAprime16.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
, EDelimited "(" ")" [ Right (EIdentifier "x") ]
77
]
88
>>> eqn
9-
f sup ' left ( x right ) = f sup prime left ( x right )
9+
f sup ' left ( x right ) = f' left ( x right )

test/writer/eqn/primes1.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
, ESuper (ESymbol Accent "'") (ESymbol Accent "'")
3838
]
3939
>>> eqn
40-
x sup 2 + 2 sup 2 + x sup prime + x sup ' + x sup "''" + x prime + {x sup '} sup ' + {x sup '} sup 2 + x sup {' + '} + {x sup '} sup ' + x sup {' sup '} + ' sup ' + ' sup '
40+
x sup 2 + 2 sup 2 + x' + x sup ' + x sup "''" + x prime + {x sup '} sup ' + {x sup '} sup 2 + x sup {' + '} + {x sup '} sup ' + x sup {' sup '} + ' sup ' + ' sup '

test/writer/eqn/primes2.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
, ESuper (EIdentifier "H") (ESymbol Accent "\8279")
2828
]
2929
>>> eqn
30-
H sup " H sup ' H sup * H sup ` H sup \[u00AA] H sup \[u00B0] H sup \[u00B2] H sup \[u00B3] H sup \[u00B4] H sup \[u00B9] H sup \[u00BA] H sup \[u2018] H sup \[u2019] H sup \[u201A] H sup \[u201B] H sup \[u201C] H sup \[u201D] H sup \[u201E] H sup \[u201F] H sup prime H sup \[u2033] H sup \[u2034] H sup \[u2035] H sup \[u2036] H sup \[u2037] H sup \[u2057]
30+
H sup " H sup ' H sup * H sup ` H sup \[u00AA] H sup \[u00B0] H sup \[u00B2] H sup \[u00B3] H sup \[u00B4] H sup \[u00B9] H sup \[u00BA] H sup \[u2018] H sup \[u2019] H sup \[u201A] H sup \[u201B] H sup \[u201C] H sup \[u201D] H sup \[u201E] H sup \[u201F] H' H sup \[u2033] H sup \[u2034] H sup \[u2035] H sup \[u2036] H sup \[u2037] H sup \[u2057]

test/writer/typst/primes1.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
, ESuper (ESymbol Accent "'") (ESymbol Accent "'")
3838
]
3939
>>> typst
40-
x^2 + 2^2 + x^(') + x^(') + x^("''") + x' + x^(')^(') + x^(')^2 + x^(' +') + x^(')^(') + x^('^(')) +'^(') +'^(')
40+
x^2 + 2^2 + x' + x^(') + x^("''") + x' + x^(')^(') + x^(')^2 + x^(' +') + x^(')^(') + x^('^(')) +'^(') +'^(')

test/writer/typst/primes2.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
, ESuper (EIdentifier "H") (ESymbol Accent "\8279")
2828
]
2929
>>> typst
30-
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^('''')
30+
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 commit comments

Comments
 (0)