git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Use \texttt and escapes instead of \verb!..!.

\verb is simply too fragile; it doesn't work inside command arguments.

commit0cf2a631e8d51d59096faeb9497da85baf80d925 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 22 Jul 2011, 12:19 (15 years ago) parente3e9225
4 files changed+34−38
src/Text/Pandoc/Writers/LaTeX.hs+14−23 tests/lhs-test.latex+5−4 tests/lhs-test.latex+lhs+5−4 tests/writer.latex+10−7
src/Text/Pandoc/Writers/LaTeX.hs+14−23
@@ -152,21 +152,13 @@ stringToLaTeX = escapeStringUsing latexEscapes
152152inCmd :: String -> Doc -> Doc
153153inCmd cmd contents = char '\\' <> text cmd <> braces contents
154154
155−-- | Remove all code elements from list of inline elements
156−-- (because it's illegal to have verbatim inside some command arguments)
157−deVerb :: [Inline] -> [Inline]
158−deVerb [] = []
159−deVerb ((Code _ str):rest) =
160− (RawInline "latex" $ "\\texttt{" ++ stringToLaTeX str ++ "}"):(deVerb rest)
161−deVerb (other:rest) = other:(deVerb rest)
162−
163155-- | Convert Pandoc block element to LaTeX.
164156blockToLaTeX :: Block -- ^ Block to convert
165157 -> State WriterState Doc
166158blockToLaTeX Null = return empty
167159blockToLaTeX (Plain lst) = inlineListToLaTeX lst
168160blockToLaTeX (Para [Image txt (src,tit)]) = do
169− capt <- inlineListToLaTeX $ deVerb txt
161+ capt <- inlineListToLaTeX txt
170162 img <- inlineToLaTeX (Image txt (src,tit))
171163 return $ "\\begin{figure}[htbp]" $$ "\\centering" $$ img $$
172164 ("\\caption{" <> capt <> char '}') $$ "\\end{figure}" $$ blankline
@@ -250,14 +242,13 @@ blockToLaTeX (DefinitionList lst) = do
250242blockToLaTeX HorizontalRule = return $
251243 "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
252244blockToLaTeX (Header level lst) = do
253− let lst' = deVerb lst
254− txt <- inlineListToLaTeX lst'
245+ txt <- inlineListToLaTeX lst
255246 let noNote (Note _) = Str ""
256247 noNote x = x
257− let lstNoNotes = bottomUp noNote lst'
248+ let lstNoNotes = bottomUp noNote lst
258249 -- footnotes in sections don't work unless you specify an optional
259250 -- argument: \section[mysec]{mysec\footnote{blah}}
260− optional <- if lstNoNotes == lst'
251+ optional <- if lstNoNotes == lst
261252 then return empty
262253 else do
263254 res <- inlineListToLaTeX lstNoNotes
@@ -280,7 +271,7 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
280271 then return empty
281272 else liftM ($$ "\\ML")
282273 $ (tableRowToLaTeX True aligns widths) heads
283− captionText <- inlineListToLaTeX $ deVerb caption
274+ captionText <- inlineListToLaTeX caption
284275 let capt = if isEmpty captionText
285276 then empty
286277 else text "caption = " <> captionText <> "," <> space
@@ -337,7 +328,7 @@ listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
337328
338329defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
339330defListItemToLaTeX (term, defs) = do
340− term' <- inlineListToLaTeX $ deVerb term
331+ term' <- inlineListToLaTeX term
341332 def' <- liftM vsep $ mapM blockListToLaTeX defs
342333 return $ "\\item" <> brackets term' $$ def'
343334
@@ -355,23 +346,23 @@ isQuoted _ = False
355346inlineToLaTeX :: Inline -- ^ Inline to convert
356347 -> State WriterState Doc
357348inlineToLaTeX (Emph lst) =
358− inlineListToLaTeX (deVerb lst) >>= return . inCmd "emph"
349+ inlineListToLaTeX lst >>= return . inCmd "emph"
359350inlineToLaTeX (Strong lst) =
360− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textbf"
351+ inlineListToLaTeX lst >>= return . inCmd "textbf"
361352inlineToLaTeX (Strikeout lst) = do
362− contents <- inlineListToLaTeX $ deVerb lst
353+ contents <- inlineListToLaTeX lst
363354 modify $ \s -> s{ stStrikeout = True }
364355 return $ inCmd "sout" contents
365356inlineToLaTeX (Superscript lst) =
366− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textsuperscript"
357+ inlineListToLaTeX lst >>= return . inCmd "textsuperscript"
367358inlineToLaTeX (Subscript lst) = do
368359 modify $ \s -> s{ stSubscript = True }
369− contents <- inlineListToLaTeX $ deVerb lst
360+ contents <- inlineListToLaTeX lst
370361 -- oddly, latex includes \textsuperscript but not \textsubscript
371362 -- so we have to define it (using a different name so as not to conflict with memoir class):
372363 return $ inCmd "textsubscr" contents
373364inlineToLaTeX (SmallCaps lst) =
374− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textsc"
365+ inlineListToLaTeX lst >>= return . inCmd "textsc"
375366inlineToLaTeX (Cite cits lst) = do
376367 st <- get
377368 let opts = stOptions st
@@ -386,7 +377,7 @@ inlineToLaTeX (Code _ str) = do
386377 let chr = ((enumFromTo '!' '~') \\ str) !! 0
387378 if writerListings (stOptions st)
388379 then return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
389− else return $ text $ "\\verb" ++ [chr] ++ str ++ [chr]
380+ else return $ text $ "\\texttt{" ++ stringToLaTeX str ++ "}"
390381inlineToLaTeX (Quoted SingleQuote lst) = do
391382 contents <- inlineListToLaTeX lst
392383 let s1 = if (not (null lst)) && (isQuoted (head lst))
@@ -422,7 +413,7 @@ inlineToLaTeX (Link txt (src, _)) =
422413 [Code _ x] | x == src -> -- autolink
423414 do modify $ \s -> s{ stUrl = True }
424415 return $ text $ "\\url{" ++ x ++ "}"
425− _ -> do contents <- inlineListToLaTeX $ deVerb txt
416+ _ -> do contents <- inlineListToLaTeX txt
426417 return $ text ("\\href{" ++ stringToLaTeX src ++ "}{") <>
427418 contents <> char '}'
428419inlineToLaTeX (Image _ (source, _)) = do
tests/lhs-test.latex+5−4
@@ -5,6 +5,7 @@
55\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
66\setlength{\parindent}{0pt}
77\setlength{\parskip}{6pt plus 2pt minus 1pt}
8+\setlength{\emergencystretch}{3em} -- prevent overfull lines
89\setcounter{secnumdepth}{0}
910
1011
@@ -12,7 +13,7 @@
1213
1314\section{lhs test}
1415
15−\verb!unsplit! is an arrow that takes a pair of values and combines them to
16+\texttt{unsplit} is an arrow that takes a pair of values and combines them to
1617return a single value:
1718
1819\begin{verbatim}
@@ -20,9 +21,9 @@ unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
2021unsplit = arr . uncurry
2122 -- arr (\op (x,y) -> x `op` y)
2223\end{verbatim}
23−\verb!(***)! combines two arrows into a new arrow by running the two arrows on
24−a pair of values (one arrow on the first item of the pair and one arrow on the
25−second item of the pair).
24+\texttt{(***)} combines two arrows into a new arrow by running the two arrows
25+on a pair of values (one arrow on the first item of the pair and one arrow on
26+the second item of the pair).
2627
2728\begin{verbatim}
2829f *** g = first f >>> second g
tests/lhs-test.latex+lhs+5−4
@@ -7,6 +7,7 @@
77\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
88\setlength{\parindent}{0pt}
99\setlength{\parskip}{6pt plus 2pt minus 1pt}
10+\setlength{\emergencystretch}{3em} -- prevent overfull lines
1011\setcounter{secnumdepth}{0}
1112
1213
@@ -14,7 +15,7 @@
1415
1516\section{lhs test}
1617
17−\verb!unsplit! is an arrow that takes a pair of values and combines them to
18+\texttt{unsplit} is an arrow that takes a pair of values and combines them to
1819return a single value:
1920
2021\begin{code}
@@ -22,9 +23,9 @@ unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
2223unsplit = arr . uncurry
2324 -- arr (\op (x,y) -> x `op` y)
2425\end{code}
25−\verb!(***)! combines two arrows into a new arrow by running the two arrows on
26−a pair of values (one arrow on the first item of the pair and one arrow on the
27−second item of the pair).
26+\texttt{(***)} combines two arrows into a new arrow by running the two arrows
27+on a pair of values (one arrow on the first item of the pair and one arrow on
28+the second item of the pair).
2829
2930\begin{verbatim}
3031f *** g = first f >>> second g
tests/writer.latex+10−7
@@ -28,6 +28,7 @@
2828\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
2929\setlength{\parindent}{0pt}
3030\setlength{\parskip}{6pt plus 2pt minus 1pt}
31+\setlength{\emergencystretch}{3em} -- prevent overfull lines
3132\setcounter{secnumdepth}{0}
3233\VerbatimFootnotes % allows verbatim text in footnotes
3334
@@ -558,7 +559,8 @@ So is \textbf{\emph{this}} word.
558559
559560So is \textbf{\emph{this}} word.
560561
561−This is code: \verb!>!, \verb!$!, \verb!\!, \verb!\$!, \verb!<html>!.
562+This is code: \texttt{\textgreater{}}, \texttt{\$}, \texttt{\textbackslash{}},
563+\texttt{\textbackslash{}\$}, \texttt{\textless{}html\textgreater{}}.
562564
563565\sout{This is \emph{strikeout}.}
564566
@@ -582,7 +584,7 @@ spaces: a\^{}b c\^{}d, a\ensuremath{\sim}b c\ensuremath{\sim}d.
582584
583585`He said, ``I want to go.''\,' Were you alive in the 70's?
584586
585−Here is some quoted `\verb!code!' and a
587+Here is some quoted `\texttt{code}' and a
586588``\href{http://example.com/?foo=1\&bar=2}{quoted link}''.
587589
588590Some dashes: one---two --- three---four --- five.
@@ -618,14 +620,14 @@ These shouldn't be math:
618620
619621\begin{itemize}
620622\item
621− To get the famous equation, write \verb!$e = mc^2$!.
623+ To get the famous equation, write \texttt{\$e = mc\^{}2\$}.
622624\item
623625 \$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if ``lot'' is
624626 emphasized.)
625627\item
626628 Shoes (\$20) and socks (\$5).
627629\item
628− Escaped \verb!$!: \$73 \emph{this should be emphasized} 23\$.
630+ Escaped \texttt{\$}: \$73 \emph{this should be emphasized} 23\$.
629631\end{itemize}
630632Here's a LaTeX table:
631633
@@ -777,7 +779,8 @@ An e-mail address:
777779Blockquoted: \url{http://example.com/}
778780
779781\end{quote}
780−Auto-links should not occur here: \verb!<http://example.com/>!
782+Auto-links should not occur here:
783+\texttt{\textless{}http://example.com/\textgreater{}}
781784
782785\begin{verbatim}
783786or here: <http://example.com/>
@@ -815,8 +818,8 @@ Here is a footnote reference,\footnote{Here is the footnote. It can go
815818 indent the first line of each block.} This should \emph{not} be a footnote
816819reference, because it contains a space.{[}\^{}my note{]} Here is an inline
817820note.\footnote{This is \emph{easier} to type. Inline notes may contain
818− \href{http://google.com}{links} and \verb!]! verbatim characters, as well as
819− {[}bracketed text{]}.}
821+ \href{http://google.com}{links} and \texttt{{]}} verbatim characters, as
822+ well as {[}bracketed text{]}.}
820823
821824\begin{quote}
822825Notes can go in quotes.\footnote{In quote.}