@@ -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