git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: add newline if math ends in a comment.
e0290fdJohn MacFarlane
src/Text/Pandoc/Writers/LaTeX.hs+12−2
@@ -1168,10 +1168,10 @@ inlineToLaTeX (Str str) = do
1168 liftM text $ stringToLaTeX TextString str
1168 liftM text $ stringToLaTeX TextString str
1169inlineToLaTeX (Math InlineMath str) = do
1169inlineToLaTeX (Math InlineMath str) = do
1170 setEmptyLine False
1170 setEmptyLine False
1171− return $ "\\(" <> text str <> "\\)"
1171+ return $ "\\(" <> text (handleMathComment str) <> "\\)"
1172inlineToLaTeX (Math DisplayMath str) = do
1172inlineToLaTeX (Math DisplayMath str) = do
1173 setEmptyLine False
1173 setEmptyLine False
1174− return $ "\\[" <> text str <> "\\]"
1174+ return $ "\\[" <> text (handleMathComment str) <> "\\]"
1175inlineToLaTeX il@(RawInline f str)
1175inlineToLaTeX il@(RawInline f str)
1176 | f == Format "latex" || f == Format "tex"
1176 | f == Format "latex" || f == Format "tex"
1177 = do
1177 = do
@@ -1272,6 +1272,16 @@ inlineToLaTeX (Note contents) = do
1272 -- note: a \n before } needed when note ends with a Verbatim environment
1272 -- note: a \n before } needed when note ends with a Verbatim environment
1273 else "\\footnote" <> beamerMark <> braces noteContents
1273 else "\\footnote" <> beamerMark <> braces noteContents
1274
1274
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+
1275protectCode :: [Inline] -> [Inline]
1285protectCode :: [Inline] -> [Inline]
1276protectCode [] = []
1286protectCode [] = []
1277protectCode (x@(Code ("",[],[]) _) : xs) = x : protectCode xs
1287protectCode (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+```