Skip to content

Commit 4f2acdc

Browse files
committed
TeX reader: allow decimals in \hspace.
Closes #259. Note: `\vspace` is not supported at all, because we have no corresponding AST element.
1 parent bdf2608 commit 4f2acdc

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Text/TeXMath/Readers/TeX.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Data.Char (isDigit, isAscii, isLetter)
3131
import qualified Data.Map as M
3232
import qualified Data.Text as T
3333
import Data.Text (Text)
34+
import Data.Ratio ((%))
3435
import Data.Maybe (catMaybes, fromJust, mapMaybe)
3536
import Text.Parsec hiding (label)
3637
import Text.Parsec.Error
@@ -718,17 +719,27 @@ xspace "\\mspace" =
718719
_ -> mzero
719720
xspace "\\hspace" = do
720721
braces $ do
721-
len <- many1 digit
722+
as <- option "" $ many1 digit
723+
bs <- option "" $ char '.' *> many1 digit
724+
let denominator = 10^(length bs)
725+
as' <- if null as then pure 0 else stringToInteger as
726+
bs' <- if null bs then pure 0 else stringToInteger bs
727+
let numerator = (as' * denominator) + bs'
728+
let n = numerator % denominator
722729
scaleFactor <-
723730
1 <$ (string "em")
724731
<|> (1/12) <$ (string "pt")
725732
<|> 6 <$ (string "in")
726733
<|> (50/21) <$ (string "cm")
727-
case reads len of
728-
((n :: Integer,[]):_) -> return $ ESpace (fromIntegral n * scaleFactor)
729-
_ -> mzero
734+
return $ ESpace (n * scaleFactor)
730735
xspace _ = mzero
731736

737+
stringToInteger :: String -> TP Integer
738+
stringToInteger s =
739+
case reads s of
740+
((n :: Integer, []):_) -> pure n
741+
_ -> fail $ "Could not read " <> s <> " as Integer."
742+
732743
mathop :: Text -> TP Exp
733744
mathop c =
734745
case c of

test/regression/259.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<<< tex
2+
\hspace{1.2em}
3+
>>> native
4+
[ ESpace (6 % 5) ]

0 commit comments

Comments
 (0)