Skip to content

Commit 1e2548e

Browse files
committed
Use Pun for primes in TeX reader, revise type of toPrimes.
This will ensure that `mo` is used for primes in MathML.
1 parent fa823f5 commit 1e2548e

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

src/Text/TeXMath/Readers/TeX.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ expr1 = do
6868
case getPrimes primes of
6969
"" -> return e
7070
cs -> return $ case e of
71-
ESub b sub -> ESubsup b sub (ESymbol Ord (T.pack cs))
72-
_ -> ESuper e (ESymbol Ord (T.pack cs))
71+
ESub b sub -> ESubsup b sub (ESymbol Pun (T.pack cs))
72+
_ -> ESuper e (ESymbol Pun (T.pack cs))
7373

7474
expr2 :: TP Exp
7575
expr2 = choice

src/Text/TeXMath/Shared.hs

+13-10
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,16 @@ isUppercaseGreek t = not (T.null t) && T.all isUGreek t
362362
where
363363
isUGreek c = c >= '\x0391' && c <= '\x03A9'
364364

365-
toPrimes :: T.Text -> Maybe T.Text
366-
toPrimes t =
367-
case T.uncons t of
368-
Nothing -> Just ""
369-
Just ('\'', t') -> ("'" <>) <$> toPrimes t'
370-
Just ('\x2032', t') -> ("'" <>) <$> toPrimes t'
371-
Just ('\x2033', t') -> ("''" <>) <$> toPrimes t'
372-
Just ('\x2034', t') -> ("'''" <>) <$> toPrimes t'
373-
Just ('\x2057', t') -> ("''''" <>) <$> toPrimes t'
374-
_ -> Nothing
365+
toPrimes :: Exp -> Maybe T.Text
366+
toPrimes (ESymbol ty s) | ty == Pun || ty == Ord = go s
367+
where
368+
go t =
369+
case T.uncons t of
370+
Nothing -> Just ""
371+
Just ('\'', t') -> ("'" <>) <$> go t'
372+
Just ('\x2032', t') -> ("'" <>) <$> go t'
373+
Just ('\x2033', t') -> ("''" <>) <$> go t'
374+
Just ('\x2034', t') -> ("'''" <>) <$> go t'
375+
Just ('\x2057', t') -> ("''''" <>) <$> go t'
376+
_ -> Nothing
377+
toPrimes _ = Nothing

src/Text/TeXMath/Writers/Eqn.hs

+4-6
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,13 @@ writeExp (EFraction fractype e1 e2) = writeExp' e1 <> op <> writeExp' e2
165165
else " over "
166166
writeExp (ESub b e1) = writeExp' b <> " sub " <> writeExp' e1
167167
writeExp (ESuper b e1) = writeExp' b <>
168-
case e1 of
169-
ESymbol Ord s
170-
| Just ps <- toPrimes s -> ps
168+
case toPrimes e1 of
169+
Just ps -> ps
171170
_ -> " sup " <> writeExp' e1
172171
writeExp (ESubsup b e1 e2) =
173172
writeExp' b <> " sub " <> writeExp' e1 <>
174-
case e2 of
175-
ESymbol Ord s
176-
| Just cs <- toPrimes s -> writeExp' b <> cs
173+
case toPrimes e2 of
174+
Just ps -> ps
177175
_ -> " sup " <> writeExp' e2
178176
writeExp (EOver _convertible b e1) =
179177
writeExp' b <> " to " <> writeExp' e1

src/Text/TeXMath/Writers/TeX.hs

+4-6
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,16 @@ writeExp (ESub b e1) = do
199199
tellGroup (writeExp e1)
200200
writeExp (ESuper b e1) = do
201201
(if isFancy b then tellGroup else id) $ writeExp b
202-
case e1 of
203-
ESymbol Ord s
204-
| Just ps <- S.toPrimes s -> tell (replicate (T.length ps) (Token '\''))
202+
case S.toPrimes e1 of
203+
Just ps -> tell (replicate (T.length ps) (Token '\''))
205204
_ -> do tell [Token '^']
206205
tellGroup (writeExp e1)
207206
writeExp (ESubsup b e1 e2) = do
208207
(if isFancy b then tellGroup else id) $ writeExp b
209208
tell [Token '_']
210209
tellGroup (writeExp e1)
211-
case e2 of
212-
ESymbol Ord s
213-
| Just ps <- S.toPrimes s -> tell (replicate (T.length ps) (Token '\''))
210+
case S.toPrimes e2 of
211+
Just ps -> tell (replicate (T.length ps) (Token '\''))
214212
_ -> do tell [Token '^']
215213
tellGroup (writeExp e2)
216214
writeExp (EOver convertible b e1) = do

src/Text/TeXMath/Writers/Typst.hs

+4-6
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,12 @@ writeExp (EFraction _fractype e1 e2) =
140140
_ -> writeExp e1 <> " / " <> writeExp e2
141141
writeExp (ESub b e1) = writeExpB b <> "_" <> writeExpS e1
142142
writeExp (ESuper b e1) = writeExpB b <>
143-
case e1 of
144-
ESymbol Ord t
145-
| Just ps <- S.toPrimes t -> ps
143+
case S.toPrimes e1 of
144+
Just ps -> ps
146145
_ -> "^" <> writeExpS e1
147146
writeExp (ESubsup b e1 e2) = writeExpB b <> "_" <> writeExpS e1 <>
148-
case e2 of
149-
ESymbol Ord t
150-
| Just ps <- S.toPrimes t -> ps
147+
case S.toPrimes e2 of
148+
Just ps -> ps
151149
_ -> "^" <> writeExpS e2
152150
writeExp (EOver _ (EOver _ b (ESymbol TOver "\9182")) e1) =
153151
"overbrace(" <> writeExp b <> ", " <> writeExp e1 <> ")"

test/reader/tex/07.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ u'' + p(x)u' + q(x)u=f(x),\quad x>a
33

44

55
>>> native
6-
[ ESuper (EIdentifier "u") (ESymbol Ord "\8243")
6+
[ ESuper (EIdentifier "u") (ESymbol Pun "\8243")
77
, ESymbol Bin "+"
88
, EIdentifier "p"
99
, ESymbol Open "("
1010
, EIdentifier "x"
1111
, ESymbol Close ")"
12-
, ESuper (EIdentifier "u") (ESymbol Ord "\8242")
12+
, ESuper (EIdentifier "u") (ESymbol Pun "\8242")
1313
, ESymbol Bin "+"
1414
, EIdentifier "q"
1515
, ESymbol Open "("

0 commit comments

Comments
 (0)