git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Modified to use Pretty.

Improved footnote formatting, removed spurious blank lines.

commit99a58e51f593cec317076429bf73efd4b784d3b8 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 19 Dec 2010, 10:13 (16 years ago) parent09aec9f
2 files changed+65−106
src/Text/Pandoc/Writers/LaTeX.hs+30−34 tests/writer.latex+35−72
src/Text/Pandoc/Writers/LaTeX.hs+30−34
@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
12{-
23Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
34
@@ -32,10 +33,10 @@ import Text.Pandoc.Definition
3233import Text.Pandoc.Shared
3334import Text.Pandoc.Templates
3435import Text.Printf ( printf )
35−import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse, intercalate )
36+import Data.List ( (\\), isSuffixOf, isPrefixOf, intercalate )
3637import Data.Char ( toLower, isPunctuation )
3738import Control.Monad.State
38−import Text.PrettyPrint.HughesPJ hiding ( Str )
39+import Text.Pandoc.Pretty
3940import System.FilePath (dropExtension)
4041
4142data WriterState =
@@ -71,17 +72,21 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
7172 "{report}" `isSuffixOf` x)
7273 when (any usesBookClass (lines template)) $
7374 modify $ \s -> s{stBook = True}
74− titletext <- liftM render $ inlineListToLaTeX title
75− authorsText <- mapM (liftM render . inlineListToLaTeX) authors
76− dateText <- liftM render $ inlineListToLaTeX date
75+ opts <- liftM stOptions get
76+ let colwidth = if writerWrapText opts
77+ then Just $ writerColumns opts
78+ else Nothing
79+ titletext <- liftM (render colwidth) $ inlineListToLaTeX title
80+ authorsText <- mapM (liftM (render colwidth) . inlineListToLaTeX) authors
81+ dateText <- liftM (render colwidth) $ inlineListToLaTeX date
7782 let (blocks', lastHeader) = if writerCiteMethod options == Citeproc then
7883 (blocks, [])
7984 else case last blocks of
8085 Header 1 il -> (init blocks, il)
8186 _ -> (blocks, [])
8287 body <- blockListToLaTeX blocks'
83− biblioTitle <- liftM render $ inlineListToLaTeX lastHeader
84− let main = render body
88+ biblioTitle <- liftM (render colwidth) $ inlineListToLaTeX lastHeader
89+ let main = render colwidth body
8590 st <- get
8691 let biblioFiles = intercalate "," $ map dropExtension $ writerBiblioFiles options
8792 citecontext = case writerCiteMethod options of
@@ -152,20 +157,15 @@ deVerb (other:rest) = other:(deVerb rest)
152157blockToLaTeX :: Block -- ^ Block to convert
153158 -> State WriterState Doc
154159blockToLaTeX Null = return empty
155−blockToLaTeX (Plain lst) = do
156− st <- get
157− let opts = stOptions st
158− wrapTeXIfNeeded opts True inlineListToLaTeX lst
160+blockToLaTeX (Plain lst) = inlineListToLaTeX lst
159161blockToLaTeX (Para [Image txt (src,tit)]) = do
160162 capt <- inlineListToLaTeX txt
161163 img <- inlineToLaTeX (Image txt (src,tit))
162164 return $ text "\\begin{figure}[htb]" $$ text "\\centering" $$ img $$
163− (text "\\caption{" <> capt <> char '}') $$ text "\\end{figure}\n"
165+ (text "\\caption{" <> capt <> char '}') $$ text "\\end{figure}" $$ blankline
164166blockToLaTeX (Para lst) = do
165− st <- get
166− let opts = stOptions st
167− result <- wrapTeXIfNeeded opts True inlineListToLaTeX lst
168− return $ result <> char '\n'
167+ result <- inlineListToLaTeX lst
168+ return $ result <> blankline
169169blockToLaTeX (BlockQuote lst) = do
170170 contents <- blockListToLaTeX lst
171171 return $ text "\\begin{quote}" $$ contents $$ text "\\end{quote}"
@@ -181,8 +181,8 @@ blockToLaTeX (CodeBlock (_,classes,_) str) = do
181181 modify $ \s -> s{ stVerbInNote = True }
182182 return "Verbatim"
183183 else return "verbatim"
184− return $ text ("\\begin{" ++ env ++ "}\n") <> text str <>
185− text ("\n\\end{" ++ env ++ "}")
184+ return $ "\\begin{" <> text env <> "}" $$ flush (text str) $$
185+ "\\end{" <> text env <> "}" $$ cr -- final cr needed because of footnotes
186186blockToLaTeX (RawHtml _) = return empty
187187blockToLaTeX (BulletList lst) = do
188188 items <- mapM listItemToLaTeX lst
@@ -211,8 +211,8 @@ blockToLaTeX (DefinitionList lst) = do
211211 items <- mapM defListItemToLaTeX lst
212212 return $ text "\\begin{description}" $$ vcat items $$
213213 text "\\end{description}"
214−blockToLaTeX HorizontalRule = return $ text $
215− "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n"
214+blockToLaTeX HorizontalRule = return $
215+ "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
216216blockToLaTeX (Header level lst) = do
217217 let lst' = deVerb lst
218218 txt <- inlineListToLaTeX lst'
@@ -229,7 +229,7 @@ blockToLaTeX (Header level lst) = do
229229 let stuffing = optional <> char '{' <> txt <> char '}'
230230 book <- liftM stBook get
231231 let level' = if book then level - 1 else level
232− let headerWith x y = text x <> y <> char '\n'
232+ let headerWith x y = text x <> y $$ blankline
233233 return $ case level' of
234234 0 -> headerWith "\\chapter" stuffing
235235 1 -> headerWith "\\section" stuffing
@@ -237,7 +237,7 @@ blockToLaTeX (Header level lst) = do
237237 3 -> headerWith "\\subsubsection" stuffing
238238 4 -> headerWith "\\paragraph" stuffing
239239 5 -> headerWith "\\subparagraph" stuffing
240− _ -> txt <> char '\n'
240+ _ -> txt $$ blankline
241241blockToLaTeX (Table caption aligns widths heads rows) = do
242242 headers <- if all null heads
243243 then return empty
@@ -246,13 +246,13 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
246246 rows' <- mapM tableRowToLaTeX rows
247247 let colDescriptors = concat $ zipWith toColDescriptor widths aligns
248248 let tableBody = text ("\\begin{tabular}{" ++ colDescriptors ++ "}") $$
249− headers $$ vcat rows' $$ text "\\end{tabular}"
249+ headers $$ vcat rows' $$ text "\\end{tabular}"
250250 let centered txt = text "\\begin{center}" $$ txt $$ text "\\end{center}"
251251 modify $ \s -> s{ stTable = True }
252252 return $ if isEmpty captionText
253− then centered tableBody <> char '\n'
254− else text "\\begin{table}[h]" $$ centered tableBody $$
255− inCmd "caption" captionText $$ text "\\end{table}\n"
253+ then centered tableBody $$ blankline
254+ else text "\\begin{table}[h]" $$ centered tableBody $$
255+ inCmd "caption" captionText $$ text "\\end{table}" $$ blankline
256256
257257toColDescriptor :: Double -> Alignment -> String
258258toColDescriptor 0 align =
@@ -285,7 +285,7 @@ listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
285285defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
286286defListItemToLaTeX (term, defs) = do
287287 term' <- inlineListToLaTeX $ deVerb term
288− def' <- liftM (vcat . intersperse (text "")) $ mapM blockListToLaTeX defs
288+ def' <- liftM vsep $ mapM blockListToLaTeX defs
289289 return $ text "\\item[" <> term' <> text "]" $$ def'
290290
291291-- | Convert list of inline elements to LaTeX.
@@ -360,7 +360,7 @@ inlineToLaTeX (Math DisplayMath str) = return $ text "\\[" <> text str <> text "
360360inlineToLaTeX (TeX str) = return $ text str
361361inlineToLaTeX (HtmlInline _) = return empty
362362inlineToLaTeX (LineBreak) = return $ text "\\\\"
363−inlineToLaTeX Space = return $ char ' '
363+inlineToLaTeX Space = return space
364364inlineToLaTeX (Link txt (src, _)) =
365365 case txt of
366366 [Code x] | x == src -> -- autolink
@@ -373,15 +373,11 @@ inlineToLaTeX (Image _ (source, _)) = do
373373 modify $ \s -> s{ stGraphics = True }
374374 return $ text $ "\\includegraphics{" ++ source ++ "}"
375375inlineToLaTeX (Note contents) = do
376− st <- get
377− put (st {stInNote = True})
376+ modify (\s -> s{stInNote = True})
378377 contents' <- blockListToLaTeX contents
379378 modify (\s -> s {stInNote = False})
380− let rawnote = stripTrailingNewlines $ render contents'
381379 -- note: a \n before } is needed when note ends with a Verbatim environment
382− let optNewline = "\\end{Verbatim}" `isSuffixOf` rawnote
383− return $ text "\\footnote{" <>
384− text rawnote <> (if optNewline then char '\n' else empty) <> char '}'
380+ return $ text "\\footnote{" <> nest 2 contents' <> char '}'
385381
386382
387383citationsToNatbib :: [Citation] -> State WriterState Doc
tests/writer.latex+35−72
@@ -36,8 +36,8 @@
3636\begin{document}
3737\maketitle
3838
39−This is a set of tests for pandoc. Most of them are adapted from
40−John Gruber's markdown test suite.
39+This is a set of tests for pandoc. Most of them are adapted from John Gruber's
40+markdown test suite.
4141
4242\begin{center}\rule{3in}{0.4pt}\end{center}
4343
@@ -69,9 +69,9 @@ with no blank line
6969
7070Here's a regular paragraph.
7171
72−In Markdown 1.0.0 and earlier. Version 8. This line turns into a
73−list item. Because a hard-wrapped line in the middle of a paragraph
74−looked like a list item.
72+In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item.
73+Because a hard-wrapped line in the middle of a paragraph looked like a list
74+item.
7575
7676Here's one with a bullet. * criminey.
7777
@@ -161,13 +161,10 @@ Asterisks loose:
161161\begin{itemize}
162162\item
163163 asterisk 1
164−
165164\item
166165 asterisk 2
167−
168166\item
169167 asterisk 3
170−
171168\end{itemize}
172169Pluses tight:
173170
@@ -184,13 +181,10 @@ Pluses loose:
184181\begin{itemize}
185182\item
186183 Plus 1
187−
188184\item
189185 Plus 2
190−
191186\item
192187 Plus 3
193−
194188\end{itemize}
195189Minuses tight:
196190
@@ -207,13 +201,10 @@ Minuses loose:
207201\begin{itemize}
208202\item
209203 Minus 1
210−
211204\item
212205 Minus 2
213−
214206\item
215207 Minus 3
216−
217208\end{itemize}
218209\subsection{Ordered}
219210
@@ -242,26 +233,20 @@ Loose using tabs:
242233\begin{enumerate}[1.]
243234\item
244235 First
245−
246236\item
247237 Second
248−
249238\item
250239 Third
251−
252240\end{enumerate}
253241and using spaces:
254242
255243\begin{enumerate}[1.]
256244\item
257245 One
258−
259246\item
260247 Two
261−
262248\item
263249 Three
264−
265250\end{enumerate}
266251Multiple paragraphs:
267252
@@ -269,15 +254,11 @@ Multiple paragraphs:
269254\item
270255 Item 1, graf one.
271256
272− Item 1. graf two. The quick brown fox jumped over the lazy dog's
273− back.
274−
257+ Item 1. graf two. The quick brown fox jumped over the lazy dog's back.
275258\item
276259 Item 2.
277−
278260\item
279261 Item 3.
280−
281262\end{enumerate}
282263\subsection{Nested}
283264
@@ -316,7 +297,6 @@ Same thing but with paragraphs:
316297\begin{enumerate}[1.]
317298\item
318299 First
319−
320300\item
321301 Second:
322302
@@ -330,24 +310,20 @@ Same thing but with paragraphs:
330310 \end{itemize}
331311\item
332312 Third
333−
334313\end{enumerate}
335314\subsection{Tabs and spaces}
336315
337316\begin{itemize}
338317\item
339318 this is a list item indented with tabs
340−
341319\item
342320 this is a list item indented with spaces
343321
344322 \begin{itemize}
345323 \item
346324 this is an example list item indented with tabs
347−
348325 \item
349326 this is an example list item indented with spaces
350−
351327 \end{itemize}
352328\end{itemize}
353329\subsection{Fancy list markers}
@@ -487,13 +463,11 @@ Multiple definitions, loose:
487463\item[apple]
488464red fruit
489465
490−
491466computer
492467
493468\item[orange]
494469orange fruit
495470
496−
497471bank
498472
499473\end{description}
@@ -503,7 +477,6 @@ Blank line after term, indented marker, alternate markers:
503477\item[apple]
504478red fruit
505479
506−
507480computer
508481
509482\item[orange]
@@ -583,20 +556,17 @@ So is \textbf{\emph{this}} word.
583556
584557So is \textbf{\emph{this}} word.
585558
586−This is code: \verb!>!, \verb!$!, \verb!\!, \verb!\$!,
587−\verb!<html>!.
559+This is code: \verb!>!, \verb!$!, \verb!\!, \verb!\$!, \verb!<html>!.
588560
589561\sout{This is \emph{strikeout}.}
590562
591−Superscripts: a\textsuperscript{bc}d
592−a\textsuperscript{\emph{hello}} a\textsuperscript{hello~there}.
563+Superscripts: a\textsuperscript{bc}d a\textsuperscript{\emph{hello}}
564+a\textsuperscript{hello~there}.
593565
594−Subscripts: H\textsubscr{2}O, H\textsubscr{23}O,
595−H\textsubscr{many~of~them}O.
566+Subscripts: H\textsubscr{2}O, H\textsubscr{23}O, H\textsubscr{many~of~them}O.
596567
597−These should not be superscripts or subscripts, because of the
598−unescaped spaces: a\^{}b c\^{}d, a\ensuremath{\sim}b
599−c\ensuremath{\sim}d.
568+These should not be superscripts or subscripts, because of the unescaped
569+spaces: a\^{}b c\^{}d, a\ensuremath{\sim}b c\ensuremath{\sim}d.
600570
601571\begin{center}\rule{3in}{0.4pt}\end{center}
602572
@@ -640,8 +610,7 @@ Ellipses\ldots{}and\ldots{}and\ldots{}.
640610 Here's some display math:
641611 \[\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}\]
642612\item
643− Here's one that has a line break in it:
644− $\alpha + \omega \times x^2$.
613+ Here's one that has a line break in it: $\alpha + \omega \times x^2$.
645614\end{itemize}
646615These shouldn't be math:
647616
@@ -649,8 +618,8 @@ These shouldn't be math:
649618\item
650619 To get the famous equation, write \verb!$e = mc^2$!.
651620\item
652− \$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if
653− ``lot'' is emphasized.)
621+ \$22,000 is a \emph{lot} of money. So is \$34,000. (It worked if ``lot'' is
622+ emphasized.)
654623\item
655624 Shoes (\$20) and socks (\$5).
656625\item
@@ -777,16 +746,15 @@ Foo \href{/url/}{biz}.
777746
778747\subsection{With ampersands}
779748
780−Here's a
781−\href{http://example.com/?foo=1&bar=2}{link with an ampersand in the URL}.
749+Here's a \href{http://example.com/?foo=1&bar=2}{link with an ampersand in the
750+URL}.
782751
783752Here's a link with an amersand in the link text:
784753\href{http://att.com/}{AT\&T}.
785754
786755Here's an \href{/script?foo=1&bar=2}{inline link}.
787756
788−Here's an
789−\href{/script?foo=1&bar=2}{inline link in pointy braces}.
757+Here's an \href{/script?foo=1&bar=2}{inline link in pointy braces}.
790758
791759\subsection{Autolinks}
792760
@@ -830,37 +798,32 @@ Here is a movie \includegraphics{movie.jpg} icon.
830798
831799\section{Footnotes}
832800
833−Here is a footnote reference,%
834−\footnote{Here is the footnote. It can go anywhere after the footnote
835−reference. It need not be placed at the end of the document.}
836−and another.%
837−\footnote{Here's the long note. This one contains multiple blocks.
801+Here is a footnote reference,\footnote{Here is the footnote. It can go
802+ anywhere after the footnote reference. It need not be placed at the end of
803+ the document.} and another.\footnote{Here's the long note. This one contains
804+ multiple blocks.
838805
839−Subsequent blocks are indented to show that they belong to the
840−footnote (as with list items).
806+ Subsequent blocks are indented to show that they belong to the footnote (as
807+ with list items).
841808
842−\begin{Verbatim}
809+ \begin{Verbatim}
843810 { <code> }
844−\end{Verbatim}
845−If you want, you can indent every line, but you can also be lazy
846−and just indent the first line of each block.}
847−This should \emph{not} be a footnote reference, because it contains
848−a space.{[}\^{}my note{]} Here is an inline note.%
849−\footnote{This is \emph{easier} to type. Inline notes may contain
850−\href{http://google.com}{links} and \verb!]! verbatim characters,
851−as well as {[}bracketed text{]}.}
811+ \end{Verbatim}
812+ If you want, you can indent every line, but you can also be lazy and just
813+ indent the first line of each block.} This should \emph{not} be a footnote
814+reference, because it contains a space.{[}\^{}my note{]} Here is an inline
815+note.\footnote{This is \emph{easier} to type. Inline notes may contain
816+ \href{http://google.com}{links} and \verb!]! verbatim characters, as well as
817+ {[}bracketed text{]}.}
852818
853819\begin{quote}
854−Notes can go in quotes.%
855−\footnote{In quote.}
820+Notes can go in quotes.\footnote{In quote.}
856821
857822\end{quote}
858823\begin{enumerate}[1.]
859824\item
860− And in list items.%
861− \footnote{In list.}
825+ And in list items.\footnote{In list.}
862826\end{enumerate}
863−This paragraph should not be part of the note, as it is not
864−indented.
827+This paragraph should not be part of the note, as it is not indented.
865828
866829\end{document}