From 0bbbdbcdb6282c7f63bdc9617455ec088fe7779b Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 11 Aug 2025 17:35:56 -0700 Subject: LaTeX reader/writer: change handling of math environments. Certain environments in LaTeX will trigger math mode and can't occur within math mode: e.g., `align` or `equation`. Previously we "downshifted" these, parsing an `align` environment as a Math element with `aligned`, and an `equation` environment as a regular display math element. With this shift, we put these in Math inlines but retain the original environments. This works because: 1) texmath handles these environments just fine, 2) and so does MathJax; 3) when writing LaTeX we detect these environments in Math elements and emit them verbatim instead of putting them in `$..$` or `$$..$$`. Closes #9711. Closes #9296. --- src/Text/Pandoc/Readers/LaTeX/Math.hs | 29 +++++++++++++++-------------- src/Text/Pandoc/Writers/LaTeX.hs | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/LaTeX/Math.hs b/src/Text/Pandoc/Readers/LaTeX/Math.hs index 9f82618e0..1f4dc4f8c 100644 --- a/src/Text/Pandoc/Readers/LaTeX/Math.hs +++ b/src/Text/Pandoc/Readers/LaTeX/Math.hs @@ -98,20 +98,21 @@ inlineEnvironments :: PandocMonad m => M.Map Text (LP m Inlines) inlineEnvironments = M.fromList [ ("displaymath", mathEnvWith id Nothing "displaymath") , ("math", math <$> mathEnv "math") - , ("equation", mathEnvWith id Nothing "equation") - , ("equation*", mathEnvWith id Nothing "equation*") - , ("gather", mathEnvWith id (Just "gathered") "gather") - , ("gather*", mathEnvWith id (Just "gathered") "gather*") - , ("multline", mathEnvWith id (Just "gathered") "multline") - , ("multline*", mathEnvWith id (Just "gathered") "multline*") - , ("eqnarray", mathEnvWith id (Just "aligned") "eqnarray") - , ("eqnarray*", mathEnvWith id (Just "aligned") "eqnarray*") - , ("align", mathEnvWith id (Just "aligned") "align") - , ("align*", mathEnvWith id (Just "aligned") "align*") - , ("alignat", mathEnvWith id (Just "aligned") "alignat") - , ("alignat*", mathEnvWith id (Just "aligned") "alignat*") - , ("flalign", mathEnvWith id (Just "aligned") "flalign") - , ("flalign*", mathEnvWith id (Just "aligned") "flalign*") + , ("equation", mathEnvWith id (Just "equation") "equation") + , ("equation*", mathEnvWith id (Just "equation*") "equation*") + , ("gather", mathEnvWith id (Just "gather") "gather") + , ("gather*", mathEnvWith id (Just "gather*") "gather*") + , ("multline", mathEnvWith id (Just "multline") "multline") + , ("multline*", mathEnvWith id (Just "multline*") "multline*") + , ("eqnarray", mathEnvWith id (Just "eqnarray") "eqnarray") + , ("eqnarray*", mathEnvWith id (Just "eqnarray*") "eqnarray*") + , ("align", mathEnvWith id (Just "align") "align") + , ("align*", mathEnvWith id (Just "align*") "align*") + , ("alignat", mathEnvWith id (Just "alignat") "alignat") + , ("alignat*", mathEnvWith id (Just "alignat*") "alignat*") + , ("flalign", mathEnvWith id (Just "flalign") "flalign") + , ("flalign*", mathEnvWith id (Just "flalign*") "flalign*") + -- the following are not yet handled by texmath, so we use substitutes: , ("dmath", mathEnvWith id Nothing "dmath") , ("dmath*", mathEnvWith id Nothing "dmath*") , ("dgroup", mathEnvWith id (Just "aligned") "dgroup") diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index b3d545a6b..9f5f6ed69 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -1021,6 +1021,10 @@ inlineToLaTeX (Quoted qt lst) = do inlineToLaTeX (Str str) = do setEmptyLine False liftM literal $ stringToLaTeX TextString str +inlineToLaTeX (Math _ str) + | isMathEnv str -- see #9711 + = do setEmptyLine False + pure $ literal str inlineToLaTeX (Math InlineMath str) = do setEmptyLine False inSoul <- gets stInSoulCommand @@ -1310,3 +1314,23 @@ ldfLanguages = , "galician" , "slovene" ] + +isMathEnv :: Text -> Bool +isMathEnv t = + case T.stripPrefix "\\begin{" t of + Nothing -> False + Just t' -> T.takeWhile (/= '}') t' `elem` + [ "align", "align*" + , "flalign", "flalign*" + , "alignat", "alignat*" + , "dmath", "dmath*" + , "dgroup", "dgroup*" + , "darray", "darray*" + , "gather", "gather*" + , "multline", "multline*" + , "split" + , "subequations" + , "equation", "equation*" + , "eqnarray" + , "displaymath" + ] -- cgit v1.2.3