From 64e1cbf1fdbe19382804be487cc49bdcea2ac799 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 10 Apr 2024 12:19:51 -0700 Subject: [PATCH] Parse TeX `\mathbf` as both bold and upright. Closes #236. --- src/Text/TeXMath/Readers/TeX/Commands.hs | 2 +- src/Text/TeXMath/Writers/TeX.hs | 3 +++ test/reader/tex/22.test | 17 ++++++++++------- test/reader/tex/binomial_coefficient.test | 8 +++++--- test/regression/236a.test | 4 ++++ test/regression/236b.test | 4 ++++ 6 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 test/regression/236a.test create mode 100644 test/regression/236b.test diff --git a/src/Text/TeXMath/Readers/TeX/Commands.hs b/src/Text/TeXMath/Readers/TeX/Commands.hs index 12a4c029..81c2b22c 100644 --- a/src/Text/TeXMath/Readers/TeX/Commands.hs +++ b/src/Text/TeXMath/Readers/TeX/Commands.hs @@ -41,7 +41,7 @@ styleOps :: M.Map Text ([Exp] -> Exp) styleOps = M.fromList [ ("\\mathrm", EStyled TextNormal) , ("\\mathup", EStyled TextNormal) - , ("\\mathbf", EStyled TextBold) + , ("\\mathbf", EStyled TextNormal . (:[]) . EStyled TextBold) , ("\\boldsymbol", EStyled TextBold) , ("\\bm", EStyled TextBold) , ("\\symbf", EStyled TextBold) diff --git a/src/Text/TeXMath/Writers/TeX.hs b/src/Text/TeXMath/Writers/TeX.hs index cc8657af..c5ebc8db 100644 --- a/src/Text/TeXMath/Writers/TeX.hs +++ b/src/Text/TeXMath/Writers/TeX.hs @@ -274,6 +274,9 @@ writeExp (EText ttype s) = do case map (fixSpace . escapeLaTeX) (T.unpack s) of [] -> return () xs -> tell $ txtcmd (Grouped xs) +writeExp (EStyled TextNormal [EStyled TextBold es]) = do + tell [ControlSeq "\\mathbf"] + tellGroup $ mapM_ writeExp $ everywhere (mkT (fromUnicode TextBold)) es writeExp (EStyled ttype es) = do txtcmd <- (flip S.getLaTeXTextCommand ttype) <$> asks mathEnv tell [ControlSeq txtcmd] diff --git a/test/reader/tex/22.test b/test/reader/tex/22.test index fdb3f653..25fa73ca 100644 --- a/test/reader/tex/22.test +++ b/test/reader/tex/22.test @@ -66,13 +66,16 @@ ] , [ [ EText TextMonospace "mathbf" ] , [ EStyled - TextBold - [ EIdentifier "A" - , EIdentifier "B" - , EIdentifier "C" - , EIdentifier "a" - , EIdentifier "b" - , EIdentifier "c" + TextNormal + [ EStyled + TextBold + [ EIdentifier "A" + , EIdentifier "B" + , EIdentifier "C" + , EIdentifier "a" + , EIdentifier "b" + , EIdentifier "c" + ] ] ] ] diff --git a/test/reader/tex/binomial_coefficient.test b/test/reader/tex/binomial_coefficient.test index ca28588e..ba0b8617 100644 --- a/test/reader/tex/binomial_coefficient.test +++ b/test/reader/tex/binomial_coefficient.test @@ -2,7 +2,7 @@ \mathbf{C}(n,k) = \mathbf{C}_k^n = {}_n\mathbf{C}_k = \binom{n}{k} = \frac{n!}{k!\,(n -k)!} >>> native -[ EStyled TextBold [ EIdentifier "C" ] +[ EStyled TextNormal [ EStyled TextBold [ EIdentifier "C" ] ] , EDelimited "(" ")" @@ -12,12 +12,14 @@ ] , ESymbol Rel "=" , ESubsup - (EStyled TextBold [ EIdentifier "C" ]) + (EStyled TextNormal [ EStyled TextBold [ EIdentifier "C" ] ]) (EIdentifier "k") (EIdentifier "n") , ESymbol Rel "=" , ESub (EGrouped []) (EIdentifier "n") -, ESub (EStyled TextBold [ EIdentifier "C" ]) (EIdentifier "k") +, ESub + (EStyled TextNormal [ EStyled TextBold [ EIdentifier "C" ] ]) + (EIdentifier "k") , ESymbol Rel "=" , EDelimited "(" diff --git a/test/regression/236a.test b/test/regression/236a.test new file mode 100644 index 00000000..94cae12c --- /dev/null +++ b/test/regression/236a.test @@ -0,0 +1,4 @@ +<<< tex +\mathbf{A} +>>> typst +upright(bold(A)) diff --git a/test/regression/236b.test b/test/regression/236b.test new file mode 100644 index 00000000..0825567a --- /dev/null +++ b/test/regression/236b.test @@ -0,0 +1,4 @@ +<<< tex +\mathbf{A} +>>> tex +\mathbf{A}