git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: add newline if math ends in a comment.

This prevents the closing delimiter from being swalled up in the comment. Closes #4880.

commite0290fd18bb523f5f08e2d5f5d2a8b89916654d2 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 31 Oct 2018, 21:51 (8 years ago) parentffd3aa4
2 files changed+19−2
src/Text/Pandoc/Writers/LaTeX.hs+12−2 test/command/4880.md+7
src/Text/Pandoc/Writers/LaTeX.hs+12−2
@@ -1168,10 +1168,10 @@ inlineToLaTeX (Str str) = do
11681168 liftM text $ stringToLaTeX TextString str
11691169inlineToLaTeX (Math InlineMath str) = do
11701170 setEmptyLine False
1171− return $ "\\(" <> text str <> "\\)"
1171+ return $ "\\(" <> text (handleMathComment str) <> "\\)"
11721172inlineToLaTeX (Math DisplayMath str) = do
11731173 setEmptyLine False
1174− return $ "\\[" <> text str <> "\\]"
1174+ return $ "\\[" <> text (handleMathComment str) <> "\\]"
11751175inlineToLaTeX il@(RawInline f str)
11761176 | f == Format "latex" || f == Format "tex"
11771177 = do
@@ -1272,6 +1272,16 @@ inlineToLaTeX (Note contents) = do
12721272 -- note: a \n before } needed when note ends with a Verbatim environment
12731273 else "\\footnote" <> beamerMark <> braces noteContents
12741274
1275+-- A comment at the end of math needs to be followed by a newline,
1276+-- or the closing delimiter gets swallowed.
1277+handleMathComment :: String -> String
1278+handleMathComment s =
1279+ let (xs, ys) = break (\c -> c == '\n' || c == '%') $ reverse s
1280+ in case ys of
1281+ '%':'\\':_ -> s
1282+ '%':_ -> s ++ "\n"
1283+ _ -> s
1284+
12751285protectCode :: [Inline] -> [Inline]
12761286protectCode [] = []
12771287protectCode (x@(Code ("",[],[]) _) : xs) = x : protectCode xs
test/command/4880.mdnew file+7
@@ -0,0 +1,7 @@
1+```
2+% pandoc -t latex
3+$x=y%comment$
4+^D
5+\(x=y%comment
6+\)
7+```