git.iko.soy/typeable/pandoc
on master help sign in
Modified latex template to include bare minimum of packages.

Packages will be included only if they are needed, given what is in the document. So if you never use strikeout, you don't need to install the ulem package. Also moved amsmath to the top of the package list, made \maketitle conditional on a title being present, and adjusted spacing. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1738 788f1e2b-df1e-0410-8736-df70ead52e1b

commitade6d8a3fe3caab5811b1b2e0c1811e0d7faf49d authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:18 (17 years ago) parentffed5c1
2 files changed+72−19
src/Text/Pandoc/Writers/LaTeX.hs+51−18 templates/latex.template+21−1
src/Text/Pandoc/Writers/LaTeX.hs+51−18
@@ -39,16 +39,27 @@ import Control.Monad (liftM)
3939import Text.PrettyPrint.HughesPJ hiding ( Str )
4040
4141data WriterState =
42− WriterState { stInNote :: Bool -- @True@ if we're in a note
43− , stOLLevel :: Int -- level of ordered list nesting
44− , stOptions :: WriterOptions -- writer options, so they don't have to be parameter
42+ WriterState { stInNote :: Bool -- @True@ if we're in a note
43+ , stOLLevel :: Int -- level of ordered list nesting
44+ , stOptions :: WriterOptions -- writer options, so they don't have to be parameter
45+ , stVerbInNote :: Bool -- true if document has verbatim text in note
46+ , stEnumerate :: Bool -- true if document needs fancy enumerated lists
47+ , stTable :: Bool -- true if document has a table
48+ , stStrikeout :: Bool -- true if document has strikeout
49+ , stSubscript :: Bool -- true if document has subscript
50+ , stLink :: Bool -- true if document has links
51+ , stUrl :: Bool -- true if document has visible URL link
52+ , stGraphics :: Bool -- true if document contains images
4553 }
4654
4755-- | Convert Pandoc to LaTeX.
4856writeLaTeX :: WriterOptions -> Pandoc -> String
4957writeLaTeX options document =
5058 evalState (pandocToLaTeX options document) $
51− WriterState { stInNote = False, stOLLevel = 1, stOptions = options }
59+ WriterState { stInNote = False, stOLLevel = 1, stOptions = options,
60+ stVerbInNote = False, stEnumerate = False,
61+ stTable = False, stStrikeout = False, stSubscript = False,
62+ stLink = False, stUrl = False, stGraphics = False }
5263
5364pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
5465pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
@@ -63,12 +74,22 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
6374 then empty
6475 else text $ writerIncludeAfter options
6576 let main = render $ before $$ body $$ after
77+ st <- get
6678 let context = writerVariables options ++
6779 [ ("toc", if writerTableOfContents options then "yes" else "")
6880 , ("body", main)
6981 , ("title", titletext)
7082 , ("date", dateText) ] ++
71− [ ("author", a) | a <- authorsText ]
83+ [ ("author", a) | a <- authorsText ] ++
84+ [ ("xetex", "yes") | writerXeTeX options ] ++
85+ [ ("verbatim-in-note", "yes") | stVerbInNote st ] ++
86+ [ ("fancy-enums", "yes") | stEnumerate st ] ++
87+ [ ("tables", "yes") | stTable st ] ++
88+ [ ("strikeout", "yes") | stStrikeout st ] ++
89+ [ ("subscript", "yes") | stSubscript st ] ++
90+ [ ("links", "yes") | stLink st ] ++
91+ [ ("url", "yes") | stUrl st ] ++
92+ [ ("graphics", "yes") | stGraphics st ]
7293 return $ if writerStandalone options
7394 then renderTemplate context $ writerTemplate options
7495 else main
@@ -117,12 +138,14 @@ blockToLaTeX (BlockQuote lst) = do
117138 return $ text "\\begin{quote}" $$ contents $$ text "\\end{quote}"
118139blockToLaTeX (CodeBlock (_,classes,_) str) = do
119140 st <- get
120− let env = if writerLiterateHaskell (stOptions st) && "haskell" `elem` classes &&
141+ env <- if writerLiterateHaskell (stOptions st) && "haskell" `elem` classes &&
121142 "literate" `elem` classes
122− then "code"
123− else if stInNote st
124− then "Verbatim"
125− else "verbatim"
143+ then return "code"
144+ else if stInNote st
145+ then do
146+ modify $ \s -> s{ stVerbInNote = True }
147+ return "Verbatim"
148+ else return "verbatim"
126149 return $ text ("\\begin{" ++ env ++ "}\n") <> text str <>
127150 text ("\n\\end{" ++ env ++ "}")
128151blockToLaTeX (RawHtml _) = return empty
@@ -135,11 +158,13 @@ blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
135158 put $ st {stOLLevel = oldlevel + 1}
136159 items <- mapM listItemToLaTeX lst
137160 modify (\s -> s {stOLLevel = oldlevel})
138− let exemplar = if numstyle /= DefaultStyle || numdelim /= DefaultDelim
139− then char '[' <>
140− text (head (orderedListMarkers (1, numstyle,
141− numdelim))) <> char ']'
142− else empty
161+ exemplar <- if numstyle /= DefaultStyle || numdelim /= DefaultDelim
162+ then do
163+ modify $ \s -> s{ stEnumerate = True }
164+ return $ char '[' <>
165+ text (head (orderedListMarkers (1, numstyle,
166+ numdelim))) <> char ']'
167+ else return empty
143168 let resetcounter = if start /= 1 && oldlevel <= 4
144169 then text $ "\\setcounter{enum" ++
145170 map toLower (toRomanNumeral oldlevel) ++
@@ -179,6 +204,7 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
179204 headers $$ text "\\hline" $$ vcat rows' $$
180205 text "\\end{tabular}"
181206 let centered txt = text "\\begin{center}" $$ txt $$ text "\\end{center}"
207+ modify $ \s -> s{ stTable = True }
182208 return $ if isEmpty captionText
183209 then centered tableBody <> char '\n'
184210 else text "\\begin{table}[h]" $$ centered tableBody $$
@@ -237,10 +263,12 @@ inlineToLaTeX (Strong lst) =
237263 inlineListToLaTeX (deVerb lst) >>= return . inCmd "textbf"
238264inlineToLaTeX (Strikeout lst) = do
239265 contents <- inlineListToLaTeX $ deVerb lst
266+ modify $ \s -> s{ stStrikeout = True }
240267 return $ inCmd "sout" contents
241−inlineToLaTeX (Superscript lst) =
268+inlineToLaTeX (Superscript lst) =
242269 inlineListToLaTeX (deVerb lst) >>= return . inCmd "textsuperscript"
243270inlineToLaTeX (Subscript lst) = do
271+ modify $ \s -> s{ stSubscript = True }
244272 contents <- inlineListToLaTeX $ deVerb lst
245273 -- oddly, latex includes \textsuperscript but not \textsubscript
246274 -- so we have to define it (using a different name so as not to conflict with memoir class):
@@ -250,6 +278,8 @@ inlineToLaTeX (SmallCaps lst) =
250278inlineToLaTeX (Cite _ lst) =
251279 inlineListToLaTeX lst
252280inlineToLaTeX (Code str) = do
281+ st <- get
282+ when (stInNote st) $ modify $ \s -> s{ stVerbInNote = True }
253283 let chr = ((enumFromTo '!' '~') \\ str) !! 0
254284 return $ text $ "\\verb" ++ [chr] ++ str ++ [chr]
255285inlineToLaTeX (Quoted SingleQuote lst) = do
@@ -282,13 +312,16 @@ inlineToLaTeX (HtmlInline _) = return empty
282312inlineToLaTeX (LineBreak) = return $ text "\\\\"
283313inlineToLaTeX Space = return $ char ' '
284314inlineToLaTeX (Link txt (src, _)) = do
315+ modify $ \s -> s{ stLink = True }
285316 case txt of
286317 [Code x] | x == src -> -- autolink
287− do return $ text $ "\\url{" ++ x ++ "}"
318+ do modify $ \s -> s{ stUrl = True }
319+ return $ text $ "\\url{" ++ x ++ "}"
288320 _ -> do contents <- inlineListToLaTeX $ deVerb txt
289321 return $ text ("\\href{" ++ src ++ "}{") <> contents <>
290322 char '}'
291−inlineToLaTeX (Image _ (source, _)) =
323+inlineToLaTeX (Image _ (source, _)) = do
324+ modify $ \s -> s{ stGraphics = True }
292325 return $ text $ "\\includegraphics{" ++ source ++ "}"
293326inlineToLaTeX (Note contents) = do
294327 st <- get
templates/latex.template+21−1
@@ -14,22 +14,40 @@ $endif$
1414\setlength{\parindent}{0pt}
1515\setlength{\parskip}{6pt plus 2pt minus 1pt}
1616$endif$
17+$if(verbatim-in-note)$
1718\usepackage{fancyvrb}
19+$endif$
20+$if(fancy-enums)$
1821\usepackage{enumerate}
22+$endif$
23+$if(tables)$
1924\usepackage{array}
2025% This is needed because raggedright in table elements redefines \\:
2126\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
2227\let\PBS=\PreserveBackslash
28+$endif$
29+$if(strikeout)$
2330\usepackage[normalem]{ulem}
31+$endif$
32+$if(subscript)$
2433\newcommand{\textsubscr}[1]{\ensuremath{_{\scriptsize\textrm{#1}}}}
34+$endif$
35+$if(links)$
2536\usepackage[breaklinks=true]{hyperref}
37+$endif$
38+$if(url)$
2639\usepackage{url}
40+$endif$
41+$if(graphics)$
2742\usepackage{graphicx}
28−\VerbatimFootnotes % allows verbatim text in footnotes
43+$endif$
2944$if(numbersections)$
3045$else$
3146\setcounter{secnumdepth}{0}
3247$endif$
48+$if(verbatim-in-note)$
49+\VerbatimFootnotes % allows verbatim text in footnotes
50+$endif$
3351$for(header-includes)$
3452$header-includes$
3553$endfor$
@@ -43,7 +61,9 @@ $if(date)$
4361$endif$
4462
4563\begin{document}
64+$if(title)$
4665\maketitle
66+$endif$
4767
4868$if(toc)$
4969\tableofcontents