git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Use \texttt and escapes instead of \verb!..!.
0cf2a63John MacFarlane
src/Text/Pandoc/Writers/LaTeX.hs+14−23
@@ -152,21 +152,13 @@ stringToLaTeX = escapeStringUsing latexEscapes
152inCmd :: String -> Doc -> Doc
152inCmd :: String -> Doc -> Doc
153inCmd cmd contents = char '\\' <> text cmd <> braces contents
153inCmd cmd contents = char '\\' <> text cmd <> braces contents
154
154
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−
163-- | Convert Pandoc block element to LaTeX.
155-- | Convert Pandoc block element to LaTeX.
164blockToLaTeX :: Block -- ^ Block to convert
156blockToLaTeX :: Block -- ^ Block to convert
165 -> State WriterState Doc
157 -> State WriterState Doc
166blockToLaTeX Null = return empty
158blockToLaTeX Null = return empty
167blockToLaTeX (Plain lst) = inlineListToLaTeX lst
159blockToLaTeX (Plain lst) = inlineListToLaTeX lst
168blockToLaTeX (Para [Image txt (src,tit)]) = do
160blockToLaTeX (Para [Image txt (src,tit)]) = do
169− capt <- inlineListToLaTeX $ deVerb txt
161+ capt <- inlineListToLaTeX txt
170 img <- inlineToLaTeX (Image txt (src,tit))
162 img <- inlineToLaTeX (Image txt (src,tit))
171 return $ "\\begin{figure}[htbp]" $$ "\\centering" $$ img $$
163 return $ "\\begin{figure}[htbp]" $$ "\\centering" $$ img $$
172 ("\\caption{" <> capt <> char '}') $$ "\\end{figure}" $$ blankline
164 ("\\caption{" <> capt <> char '}') $$ "\\end{figure}" $$ blankline
@@ -250,14 +242,13 @@ blockToLaTeX (DefinitionList lst) = do
250blockToLaTeX HorizontalRule = return $
242blockToLaTeX HorizontalRule = return $
251 "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
243 "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
252blockToLaTeX (Header level lst) = do
244blockToLaTeX (Header level lst) = do
253− let lst' = deVerb lst
254− txt <- inlineListToLaTeX lst'
245+ txt <- inlineListToLaTeX lst
255 let noNote (Note _) = Str ""
246 let noNote (Note _) = Str ""
256 noNote x = x
247 noNote x = x
257− let lstNoNotes = bottomUp noNote lst'
248+ let lstNoNotes = bottomUp noNote lst
258 -- footnotes in sections don't work unless you specify an optional
249 -- footnotes in sections don't work unless you specify an optional
259 -- argument: \section[mysec]{mysec\footnote{blah}}
250 -- argument: \section[mysec]{mysec\footnote{blah}}
260− optional <- if lstNoNotes == lst'
251+ optional <- if lstNoNotes == lst
261 then return empty
252 then return empty
262 else do
253 else do
263 res <- inlineListToLaTeX lstNoNotes
254 res <- inlineListToLaTeX lstNoNotes
@@ -280,7 +271,7 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
280 then return empty
271 then return empty
281 else liftM ($$ "\\ML")
272 else liftM ($$ "\\ML")
282 $ (tableRowToLaTeX True aligns widths) heads
273 $ (tableRowToLaTeX True aligns widths) heads
283− captionText <- inlineListToLaTeX $ deVerb caption
274+ captionText <- inlineListToLaTeX caption
284 let capt = if isEmpty captionText
275 let capt = if isEmpty captionText
285 then empty
276 then empty
286 else text "caption = " <> captionText <> "," <> space
277 else text "caption = " <> captionText <> "," <> space
@@ -337,7 +328,7 @@ listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
337
328
338defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
329defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
339defListItemToLaTeX (term, defs) = do
330defListItemToLaTeX (term, defs) = do
340− term' <- inlineListToLaTeX $ deVerb term
331+ term' <- inlineListToLaTeX term
341 def' <- liftM vsep $ mapM blockListToLaTeX defs
332 def' <- liftM vsep $ mapM blockListToLaTeX defs
342 return $ "\\item" <> brackets term' $$ def'
333 return $ "\\item" <> brackets term' $$ def'
343
334
@@ -355,23 +346,23 @@ isQuoted _ = False
355inlineToLaTeX :: Inline -- ^ Inline to convert
346inlineToLaTeX :: Inline -- ^ Inline to convert
356 -> State WriterState Doc
347 -> State WriterState Doc
357inlineToLaTeX (Emph lst) =
348inlineToLaTeX (Emph lst) =
358− inlineListToLaTeX (deVerb lst) >>= return . inCmd "emph"
349+ inlineListToLaTeX lst >>= return . inCmd "emph"
359inlineToLaTeX (Strong lst) =
350inlineToLaTeX (Strong lst) =
360− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textbf"
351+ inlineListToLaTeX lst >>= return . inCmd "textbf"
361inlineToLaTeX (Strikeout lst) = do
352inlineToLaTeX (Strikeout lst) = do
362− contents <- inlineListToLaTeX $ deVerb lst
353+ contents <- inlineListToLaTeX lst
363 modify $ \s -> s{ stStrikeout = True }
354 modify $ \s -> s{ stStrikeout = True }
364 return $ inCmd "sout" contents
355 return $ inCmd "sout" contents
365inlineToLaTeX (Superscript lst) =
356inlineToLaTeX (Superscript lst) =
366− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textsuperscript"
357+ inlineListToLaTeX lst >>= return . inCmd "textsuperscript"
367inlineToLaTeX (Subscript lst) = do
358inlineToLaTeX (Subscript lst) = do
368 modify $ \s -> s{ stSubscript = True }
359 modify $ \s -> s{ stSubscript = True }
369− contents <- inlineListToLaTeX $ deVerb lst
360+ contents <- inlineListToLaTeX lst
370 -- oddly, latex includes \textsuperscript but not \textsubscript
361 -- oddly, latex includes \textsuperscript but not \textsubscript
371 -- so we have to define it (using a different name so as not to conflict with memoir class):
362 -- so we have to define it (using a different name so as not to conflict with memoir class):
372 return $ inCmd "textsubscr" contents
363 return $ inCmd "textsubscr" contents
373inlineToLaTeX (SmallCaps lst) =
364inlineToLaTeX (SmallCaps lst) =
374− inlineListToLaTeX (deVerb lst) >>= return . inCmd "textsc"
365+ inlineListToLaTeX lst >>= return . inCmd "textsc"
375inlineToLaTeX (Cite cits lst) = do
366inlineToLaTeX (Cite cits lst) = do
376 st <- get
367 st <- get
377 let opts = stOptions st
368 let opts = stOptions st
@@ -386,7 +377,7 @@ inlineToLaTeX (Code _ str) = do
386 let chr = ((enumFromTo '!' '~') \\ str) !! 0
377 let chr = ((enumFromTo '!' '~') \\ str) !! 0
387 if writerListings (stOptions st)
378 if writerListings (stOptions st)
388 then return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
379 then return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
389− else return $ text $ "\\verb" ++ [chr] ++ str ++ [chr]
380+ else return $ text $ "\\texttt{" ++ stringToLaTeX str ++ "}"
390inlineToLaTeX (Quoted SingleQuote lst) = do
381inlineToLaTeX (Quoted SingleQuote lst) = do
391 contents <- inlineListToLaTeX lst
382 contents <- inlineListToLaTeX lst
392 let s1 = if (not (null lst)) && (isQuoted (head lst))
383 let s1 = if (not (null lst)) && (isQuoted (head lst))
@@ -422,7 +413,7 @@ inlineToLaTeX (Link txt (src, _)) =
422 [Code _ x] | x == src -> -- autolink
413 [Code _ x] | x == src -> -- autolink
423 do modify $ \s -> s{ stUrl = True }
414 do modify $ \s -> s{ stUrl = True }
424 return $ text $ "\\url{" ++ x ++ "}"
415 return $ text $ "\\url{" ++ x ++ "}"
425− _ -> do contents <- inlineListToLaTeX $ deVerb txt
416+ _ -> do contents <- inlineListToLaTeX txt
426 return $ text ("\\href{" ++ stringToLaTeX src ++ "}{") <>
417 return $ text ("\\href{" ++ stringToLaTeX src ++ "}{") <>
427 contents <> char '}'
418 contents <> char '}'
428inlineToLaTeX (Image _ (source, _)) = do
419inlineToLaTeX (Image _ (source, _)) = do
tests/lhs-test.latex+5−4
@@ -5,6 +5,7 @@
5\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
5\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
6\setlength{\parindent}{0pt}
6\setlength{\parindent}{0pt}
7\setlength{\parskip}{6pt plus 2pt minus 1pt}
7\setlength{\parskip}{6pt plus 2pt minus 1pt}
8+\setlength{\emergencystretch}{3em} -- prevent overfull lines
8\setcounter{secnumdepth}{0}
9\setcounter{secnumdepth}{0}
9
10
10
11
@@ -12,7 +13,7 @@
12
13
13\section{lhs test}
14\section{lhs test}
14
15
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
16return a single value:
17return a single value:
17
18
18\begin{verbatim}
19\begin{verbatim}
@@ -20,9 +21,9 @@ unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
20unsplit = arr . uncurry
21unsplit = arr . uncurry
21 -- arr (\op (x,y) -> x `op` y)
22 -- arr (\op (x,y) -> x `op` y)
22\end{verbatim}
23\end{verbatim}
23−\verb!(***)! combines two arrows into a new arrow by running the two arrows on
24+\texttt{(***)} combines two arrows into a new arrow by running the two arrows
24−a pair of values (one arrow on the first item of the pair and one arrow on the
25+on a pair of values (one arrow on the first item of the pair and one arrow on
25−second item of the pair).
26+the second item of the pair).
26
27
27\begin{verbatim}
28\begin{verbatim}
28f *** g = first f >>> second g
29f *** g = first f >>> second g
tests/lhs-test.latex+lhs+5−4
@@ -7,6 +7,7 @@
7\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
7\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
8\setlength{\parindent}{0pt}
8\setlength{\parindent}{0pt}
9\setlength{\parskip}{6pt plus 2pt minus 1pt}
9\setlength{\parskip}{6pt plus 2pt minus 1pt}
10+\setlength{\emergencystretch}{3em} -- prevent overfull lines
10\setcounter{secnumdepth}{0}
11\setcounter{secnumdepth}{0}
11
12
12
13
@@ -14,7 +15,7 @@
14
15
15\section{lhs test}
16\section{lhs test}
16
17
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
18return a single value:
19return a single value:
19
20
20\begin{code}
21\begin{code}
@@ -22,9 +23,9 @@ unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
22unsplit = arr . uncurry
23unsplit = arr . uncurry
23 -- arr (\op (x,y) -> x `op` y)
24 -- arr (\op (x,y) -> x `op` y)
24\end{code}
25\end{code}
25−\verb!(***)! combines two arrows into a new arrow by running the two arrows on
26+\texttt{(***)} combines two arrows into a new arrow by running the two arrows
26−a pair of values (one arrow on the first item of the pair and one arrow on the
27+on a pair of values (one arrow on the first item of the pair and one arrow on
27−second item of the pair).
28+the second item of the pair).
28
29
29\begin{verbatim}
30\begin{verbatim}
30f *** g = first f >>> second g
31f *** g = first f >>> second g
tests/writer.latex+10−7
@@ -28,6 +28,7 @@
28\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
28\usepackage[breaklinks=true,unicode=true,pdfborder={0 0 0}]{hyperref}
29\setlength{\parindent}{0pt}
29\setlength{\parindent}{0pt}
30\setlength{\parskip}{6pt plus 2pt minus 1pt}
30\setlength{\parskip}{6pt plus 2pt minus 1pt}
31+\setlength{\emergencystretch}{3em} -- prevent overfull lines
31\setcounter{secnumdepth}{0}
32\setcounter{secnumdepth}{0}
32\VerbatimFootnotes % allows verbatim text in footnotes
33\VerbatimFootnotes % allows verbatim text in footnotes
33
34
@@ -558,7 +559,8 @@ So is \textbf{\emph{this}} word.
558
559
559So is \textbf{\emph{this}} word.
560So is \textbf{\emph{this}} word.
560
561
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{}}.
562
564
563\sout{This is \emph{strikeout}.}
565\sout{This is \emph{strikeout}.}
564
566
@@ -582,7 +584,7 @@ spaces: a\^{}b c\^{}d, a\ensuremath{\sim}b c\ensuremath{\sim}d.
582
584
583`He said, ``I want to go.''\,' Were you alive in the 70's?
585`He said, ``I want to go.''\,' Were you alive in the 70's?
584
586
585−Here is some quoted `\verb!code!' and a
587+Here is some quoted `\texttt{code}' and a
586``\href{http://example.com/?foo=1\&bar=2}{quoted link}''.
588``\href{http://example.com/?foo=1\&bar=2}{quoted link}''.
587
589
588Some dashes: one---two --- three---four --- five.
590Some dashes: one---two --- three---four --- five.
@@ -618,14 +620,14 @@ These shouldn't be math:
618
620
619\begin{itemize}
621\begin{itemize}
620\item
622\item
621− To get the famous equation, write \verb!$e = mc^2$!.
623+ To get the famous equation, write \texttt{\$e = mc\^{}2\$}.
622\item
624\item
623 \$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if ``lot'' is
625 \$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if ``lot'' is
624 emphasized.)
626 emphasized.)
625\item
627\item
626 Shoes (\$20) and socks (\$5).
628 Shoes (\$20) and socks (\$5).
627\item
629\item
628− Escaped \verb!$!: \$73 \emph{this should be emphasized} 23\$.
630+ Escaped \texttt{\$}: \$73 \emph{this should be emphasized} 23\$.
629\end{itemize}
631\end{itemize}
630Here's a LaTeX table:
632Here's a LaTeX table:
631
633
@@ -777,7 +779,8 @@ An e-mail address:
777Blockquoted: \url{http://example.com/}
779Blockquoted: \url{http://example.com/}
778
780
779\end{quote}
781\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{}}
781
784
782\begin{verbatim}
785\begin{verbatim}
783or here: <http://example.com/>
786or here: <http://example.com/>
@@ -815,8 +818,8 @@ Here is a footnote reference,\footnote{Here is the footnote. It can go
815 indent the first line of each block.} This should \emph{not} be a footnote
818 indent the first line of each block.} This should \emph{not} be a footnote
816reference, because it contains a space.{[}\^{}my note{]} Here is an inline
819reference, because it contains a space.{[}\^{}my note{]} Here is an inline
817note.\footnote{This is \emph{easier} to type. Inline notes may contain
820note.\footnote{This is \emph{easier} to type. Inline notes may contain
818− \href{http://google.com}{links} and \verb!]! verbatim characters, as well as
821+ \href{http://google.com}{links} and \texttt{{]}} verbatim characters, as
819− {[}bracketed text{]}.}
822+ well as {[}bracketed text{]}.}
820
823
821\begin{quote}
824\begin{quote}
822Notes can go in quotes.\footnote{In quote.}
825Notes can go in quotes.\footnote{In quote.}