@@ -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