git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: parse math environments as inline when possible.

Closes #1821.

commit7e41d0b1eed6d90fbfe2d27399df00e670708bb8 authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 16 Dec 2014, 12:27 (12 years ago) parentfcd1599
1 file changed+42−15
src/Text/Pandoc/Readers/LaTeX.hs+42−15
src/Text/Pandoc/Readers/LaTeX.hs+42−15
@@ -191,6 +191,7 @@ inline = (mempty <$ comment)
191191 <|> (space <$ sp)
192192 <|> inlineText
193193 <|> inlineCommand
194+ <|> inlineEnvironment
194195 <|> inlineGroup
195196 <|> (char '-' *> option (str "-")
196197 ((char '-') *> option (str "–") (str "—" <$ char '-')))
@@ -401,6 +402,24 @@ unlessParseRaw = getOption readerParseRaw >>= guard . not
401402isBlockCommand :: String -> Bool
402403isBlockCommand s = maybe False (const True) $ M.lookup s blockCommands
403404
405+
406+inlineEnvironments :: M.Map String (LP Inlines)
407+inlineEnvironments = M.fromList
408+ [ ("displaymath", mathEnv id Nothing "displaymath")
409+ , ("equation", mathEnv id Nothing "equation")
410+ , ("equation*", mathEnv id Nothing "equation*")
411+ , ("gather", mathEnv id (Just "gathered") "gather")
412+ , ("gather*", mathEnv id (Just "gathered") "gather*")
413+ , ("multline", mathEnv id (Just "gathered") "multline")
414+ , ("multline*", mathEnv id (Just "gathered") "multline*")
415+ , ("eqnarray", mathEnv id (Just "aligned") "eqnarray")
416+ , ("eqnarray*", mathEnv id (Just "aligned") "eqnarray*")
417+ , ("align", mathEnv id (Just "aligned") "align")
418+ , ("align*", mathEnv id (Just "aligned") "align*")
419+ , ("alignat", mathEnv id (Just "aligned") "alignat")
420+ , ("alignat*", mathEnv id (Just "aligned") "alignat*")
421+ ]
422+
404423inlineCommands :: M.Map String (LP Inlines)
405424inlineCommands = M.fromList $
406425 [ ("emph", extractSpaces emph <$> tok)
@@ -810,6 +829,14 @@ environment = do
810829 Just p -> p <|> rawEnv name
811830 Nothing -> rawEnv name
812831
832+inlineEnvironment :: LP Inlines
833+inlineEnvironment = try $ do
834+ controlSeq "begin"
835+ name <- braced
836+ case M.lookup name inlineEnvironments of
837+ Just p -> p
838+ Nothing -> mzero
839+
813840rawEnv :: String -> LP Blocks
814841rawEnv name = do
815842 let addBegin x = "\\begin{" ++ name ++ "}" ++ x
@@ -1034,19 +1061,19 @@ environments = M.fromList
10341061 , ("obeylines", parseFromString
10351062 (para . trimInlines . mconcat <$> many inline) =<<
10361063 intercalate "\\\\\n" . lines <$> verbEnv "obeylines")
1037− , ("displaymath", mathEnv Nothing "displaymath")
1038− , ("equation", mathEnv Nothing "equation")
1039− , ("equation*", mathEnv Nothing "equation*")
1040− , ("gather", mathEnv (Just "gathered") "gather")
1041− , ("gather*", mathEnv (Just "gathered") "gather*")
1042− , ("multline", mathEnv (Just "gathered") "multline")
1043− , ("multline*", mathEnv (Just "gathered") "multline*")
1044− , ("eqnarray", mathEnv (Just "aligned") "eqnarray")
1045− , ("eqnarray*", mathEnv (Just "aligned") "eqnarray*")
1046− , ("align", mathEnv (Just "aligned") "align")
1047− , ("align*", mathEnv (Just "aligned") "align*")
1048− , ("alignat", mathEnv (Just "aligned") "alignat")
1049− , ("alignat*", mathEnv (Just "aligned") "alignat*")
1064+ , ("displaymath", mathEnv para Nothing "displaymath")
1065+ , ("equation", mathEnv para Nothing "equation")
1066+ , ("equation*", mathEnv para Nothing "equation*")
1067+ , ("gather", mathEnv para (Just "gathered") "gather")
1068+ , ("gather*", mathEnv para (Just "gathered") "gather*")
1069+ , ("multline", mathEnv para (Just "gathered") "multline")
1070+ , ("multline*", mathEnv para (Just "gathered") "multline*")
1071+ , ("eqnarray", mathEnv para (Just "aligned") "eqnarray")
1072+ , ("eqnarray*", mathEnv para (Just "aligned") "eqnarray*")
1073+ , ("align", mathEnv para (Just "aligned") "align")
1074+ , ("align*", mathEnv para (Just "aligned") "align*")
1075+ , ("alignat", mathEnv para (Just "aligned") "alignat")
1076+ , ("alignat*", mathEnv para (Just "aligned") "alignat*")
10501077 ]
10511078
10521079letter_contents :: LP Blocks
@@ -1106,8 +1133,8 @@ listenv name p = try $ do
11061133 updateState $ \st -> st{ stateParserContext = oldCtx }
11071134 return res
11081135
1109−mathEnv :: Maybe String -> String -> LP Blocks
1110−mathEnv innerEnv name = para <$> mathDisplay (inner <$> verbEnv name)
1136+mathEnv :: (Inlines -> a) -> Maybe String -> String -> LP a
1137+mathEnv f innerEnv name = f <$> mathDisplay (inner <$> verbEnv name)
11111138 where inner x = case innerEnv of
11121139 Nothing -> x
11131140 Just y -> "\\begin{" ++ y ++ "}\n" ++ x ++