git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Fixed strikeout + highlighted code. Closes #1294.

Previously strikeout highlighted code caused an error.

commit12efffa85a257dbe81137f97334b2c6a7e072777 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 20 Jun 2014, 10:24 (12 years ago) parent1a2a0a3
2 files changed+21−2
src/Text/Pandoc/Writers/LaTeX.hs+10−1 tests/Tests/Writers/LaTeX.hs+11−1
src/Text/Pandoc/Writers/LaTeX.hs+10−1
@@ -678,7 +678,9 @@ inlineToLaTeX (Emph lst) =
678678inlineToLaTeX (Strong lst) =
679679 inlineListToLaTeX lst >>= return . inCmd "textbf"
680680inlineToLaTeX (Strikeout lst) = do
681− contents <- inlineListToLaTeX lst
681+ -- we need to protect VERB in an mbox or we get an error
682+ -- see #1294
683+ contents <- inlineListToLaTeX $ protectCode lst
682684 modify $ \s -> s{ stStrikeout = True }
683685 return $ inCmd "sout" contents
684686inlineToLaTeX (Superscript lst) =
@@ -784,6 +786,13 @@ inlineToLaTeX (Note contents) = do
784786 -- note: a \n before } needed when note ends with a Verbatim environment
785787 else "\\footnote" <> braces noteContents
786788
789+protectCode :: [Inline] -> [Inline]
790+protectCode [] = []
791+protectCode (x@(Code ("",[],[]) _) : xs) = x : protectCode xs
792+protectCode (x@(Code _ _) : xs) = ltx "\\mbox{" : x : ltx "}" : xs
793+ where ltx = RawInline (Format "latex")
794+protectCode (x : xs) = x : protectCode xs
795+
787796citationsToNatbib :: [Citation] -> State WriterState Doc
788797citationsToNatbib (one:[])
789798 = citeCommand c p s k
tests/Tests/Writers/LaTeX.hs+11−1
@@ -8,7 +8,7 @@ import Tests.Helpers
88import Tests.Arbitrary()
99
1010latex :: (ToString a, ToPandoc a) => a -> String
11−latex = writeLaTeX def . toPandoc
11+latex = writeLaTeX def{ writerHighlight = True } . toPandoc
1212
1313latexListing :: (ToString a, ToPandoc a) => a -> String
1414latexListing = writeLaTeX def{ writerListings = True } . toPandoc
@@ -54,4 +54,14 @@ tests = [ testGroup "code blocks"
5454 (text "Header 1" <> note (plain $ text "note")) =?>
5555 "\\section*{Header 1\\footnote{note}}\\label{foo}\n\\addcontentsline{toc}{section}{Header 1}\n"
5656 ]
57+ , testGroup "inline code"
58+ [ "struck out and highlighted" =:
59+ strikeout (codeWith ("",["haskell"],[]) "foo" <> space
60+ <> str "bar") =?>
61+ "\\sout{\\mbox{\\VERB|\\NormalTok{foo}|} bar}"
62+ , "struck out and not highlighted" =:
63+ strikeout (code "foo" <> space
64+ <> str "bar") =?>
65+ "\\sout{\\texttt{foo} bar}"
66+ ]
5767 ]