Skip to content

Commit dabc57e

Browse files
committed
TeX reader: parse primes as superscripts.
This seems to be how TeX behaves under the hood: `f'` is equivalent to `f^{\prime}`. See #254.
1 parent 2fe4fef commit dabc57e

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/Text/TeXMath/Readers/TeX.hs

+23-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,27 @@ type TP = Parser
5252
-- The parser
5353

5454
expr1 :: TP Exp
55-
expr1 = choice
55+
expr1 = do
56+
e <- expr2
57+
-- check for primes and add them as a subscript
58+
-- in TeX ' is shorthand for ^{\prime}
59+
primes <- many (char '\'')
60+
let getPrimes cs = case cs of
61+
"" -> ""
62+
"'" -> "\x2032"
63+
"''" -> "\x2033"
64+
"'''" -> "\x2034"
65+
"''''" -> "\x2057"
66+
_ -> "\x2057" ++ getPrimes (drop 4 cs)
67+
ignorable
68+
case getPrimes primes of
69+
"" -> return e
70+
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))
73+
74+
expr2 :: TP Exp
75+
expr2 = choice
5676
[ inbraces
5777
, variable
5878
, number
@@ -62,7 +82,7 @@ expr1 = choice
6282
, enclosure
6383
, hyperref
6484
, command
65-
] <* ignorable
85+
]
6686

6787
-- | Parse a formula, returning a list of 'Exp'.
6888
readTeX :: Text -> Either Text [Exp]
@@ -233,7 +253,7 @@ operatorname = do
233253
ctrlseq "operatorname"
234254
-- these are slightly different but we won't worry about that here...
235255
convertible <- (char '*' >> spaces >> return True) <|> return False
236-
tok' <- texToken
256+
tok' <- texSymbol <|> braces (manyExp expr2) <|> texChar
237257
tok'' <- (EMathOperator <$> (expToOperatorName tok'))
238258
<|> return (EStyled TextNormal [tok'])
239259
return (tok'', convertible)

test/reader/tex/07.test

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

44

55
>>> native
6-
[ EIdentifier "u"
7-
, ESymbol Ord "\8243"
6+
[ ESuper (EIdentifier "u") (ESymbol Ord "\8243")
87
, ESymbol Bin "+"
98
, EIdentifier "p"
109
, ESymbol Open "("
1110
, EIdentifier "x"
1211
, ESymbol Close ")"
13-
, EIdentifier "u"
14-
, ESymbol Ord "\8242"
12+
, ESuper (EIdentifier "u") (ESymbol Ord "\8242")
1513
, ESymbol Bin "+"
1614
, EIdentifier "q"
1715
, ESymbol Open "("

0 commit comments

Comments
 (0)