diff options
| author | John MacFarlane <[email protected]> | 2025-08-27 13:08:28 +0200 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2025-08-27 13:09:07 +0200 |
| commit | 27851bbe9dc005b85f885a94925371dd3903d4c6 (patch) | |
| tree | da8c2a007bd1408234b758280a9c08f1a0716db5 /src | |
| parent | 1882f0088af3385154e40c6a11142106029b42ed (diff) | |
LaTeX writer: include cancel package only if needed.
That is, only if there is math that contains `\cancel`,
`\bcancel`, or `\xcancel`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 25 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX/Types.hs | 2 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index 2c71e689a..fe549ad17 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -65,8 +65,11 @@ import Text.Pandoc.Writers.LaTeX.Util (stringToLaTeX, StringContext(..), wrapDiv, hypertarget, labelFor, getListingsLanguage, mbBraced) import Text.Pandoc.Writers.Shared +import qualified Data.Attoparsec.Text as A import qualified Text.Pandoc.UTF8 as UTF8 import qualified Text.Pandoc.Writers.AnnotatedTable as Ann +import Data.Char (isLetter) +import Control.Applicative ((<|>)) -- Work around problems with notes inside emphasis (see #8982) isolateBigNotes :: ([Inline] -> Inline) -> [Inline] -> [Inline] @@ -229,6 +232,7 @@ pandocToLaTeX options (Pandoc meta blocks) = do defField "verbatim-in-note" (stVerbInNote st) $ defField "tables" (stTable st) $ defField "multirow" (stMultiRow st) $ + defField "cancel" (stCancel st) $ defField "strikeout" (stStrikeout st) $ defField "url" (stUrl st) $ defField "numbersections" (writerNumberSections options) $ @@ -1029,11 +1033,13 @@ inlineToLaTeX (Str str) = do inlineToLaTeX (Math _ str) | isMathEnv str -- see #9711 = do setEmptyLine False + when (needsCancel str) $ modify $ \st -> st{ stCancel = True } pure $ literal str inlineToLaTeX (Math InlineMath str) = do setEmptyLine False inSoul <- gets stInSoulCommand let contents = literal (handleMathComment str) + when (needsCancel str) $ modify $ \st -> st{ stCancel = True } return $ if inSoul -- #9597 then "$" <> contents <> "$" @@ -1042,6 +1048,7 @@ inlineToLaTeX (Math DisplayMath str) = do setEmptyLine False inSoul <- gets stInSoulCommand let contents = literal (handleMathComment str) + when (needsCancel str) $ modify $ \st -> st{ stCancel = True } return $ if inSoul -- # 9597 then "$$" <> contents <> "$$" @@ -1339,3 +1346,21 @@ isMathEnv t = , "eqnarray" , "displaymath" ] + +-- True if the math needs the cancel package +needsCancel :: Text -> Bool +needsCancel t = + case A.parseOnly pCancel t of + Right True -> True + _ -> False + where + pCancel = (False <$ A.endOfInput) <|> do + c <- A.anyChar + case c of + '\\' -> do + x <- A.takeWhile isLetter + if x == "cancel" || x == "xcancel" || x == "bcancel" + then return True + else pCancel + _ -> pCancel + diff --git a/src/Text/Pandoc/Writers/LaTeX/Types.hs b/src/Text/Pandoc/Writers/LaTeX/Types.hs index 2477c2fa7..1295d2d01 100644 --- a/src/Text/Pandoc/Writers/LaTeX/Types.hs +++ b/src/Text/Pandoc/Writers/LaTeX/Types.hs @@ -53,6 +53,7 @@ data WriterState = , stIsFirstInDefinition :: Bool -- ^ first block in a defn list , stLang :: Maybe Lang -- ^ lang specified in metadata , stInSoulCommand :: Bool -- ^ in a soul command like ul + , stCancel :: Bool -- ^ true if document uses \cancel } startingState :: WriterOptions -> WriterState @@ -93,4 +94,5 @@ startingState options = , stIsFirstInDefinition = False , stLang = Nothing , stInSoulCommand = False + , stCancel = False } |
