@@ -109,9 +109,9 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
109109 blocks'' <- if writerBeamer options
110110 then toSlides blocks'
111111 else return blocks'
112− body <- blockListToLaTeX blocks''
112+ body <- mapM (elementToLaTeX options) $ hierarchicalize blocks''
113113 biblioTitle <- liftM (render colwidth) $ inlineListToLaTeX lastHeader
114− let main = render colwidth body
114+ let main = render colwidth $ cat body
115115 st <- get
116116 let biblioFiles = intercalate "," $ map dropExtension $ writerBiblioFiles options
117117 citecontext = case writerCiteMethod options of
@@ -154,8 +154,15 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
154154 then renderTemplate context template
155155 else main
156156
157−-- escape things as needed for LaTeX
157+-- | Convert Elements to LaTeX
158+elementToLaTeX :: WriterOptions -> Element -> State WriterState Doc
159+elementToLaTeX _ (Blk block) = blockToLaTeX block
160+elementToLaTeX opts (Sec level _ id' title' elements) = do
161+ header' <- sectionHeader id' level title'
162+ innerContents <- mapM (elementToLaTeX opts) elements
163+ return $ cat (header' : innerContents)
158164
165+-- escape things as needed for LaTeX
159166stringToLaTeX :: Bool -> String -> String
160167stringToLaTeX isUrl = escapeStringUsing latexEscapes
161168 where latexEscapes = backslashEscapes "{}$%&_" ++
@@ -335,30 +342,7 @@ blockToLaTeX (DefinitionList lst) = do
335342 return $ text ("\\begin{description}" ++ inc) $$ vcat items $$ "\\end{description}"
336343blockToLaTeX HorizontalRule = return $
337344 "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
338−blockToLaTeX (Header level lst) = do
339− txt <- inlineListToLaTeX lst
340− let noNote (Note _) = Str ""
341− noNote x = x
342− let lstNoNotes = bottomUp noNote lst
343− -- footnotes in sections don't work unless you specify an optional
344− -- argument: \section[mysec]{mysec\footnote{blah}}
345− optional <- if lstNoNotes == lst
346− then return empty
347− else do
348− res <- inlineListToLaTeX lstNoNotes
349− return $ char '[' <> res <> char ']'
350− let stuffing = optional <> char '{' <> txt <> char '}'
351− book <- liftM stBook get
352− let level' = if book then level - 1 else level
353− let headerWith x y = text x <> y $$ blankline
354− return $ case level' of
355− 0 -> headerWith "\\chapter" stuffing
356− 1 -> headerWith "\\section" stuffing
357− 2 -> headerWith "\\subsection" stuffing
358− 3 -> headerWith "\\subsubsection" stuffing
359− 4 -> headerWith "\\paragraph" stuffing
360− 5 -> headerWith "\\subparagraph" stuffing
361− _ -> txt $$ blankline
345+blockToLaTeX (Header level lst) = sectionHeader "" level lst
362346blockToLaTeX (Table caption aligns widths heads rows) = do
363347 modify $ \s -> s{ stInTable = True, stTableNotes = [] }
364348 headers <- if all null heads
@@ -426,6 +410,45 @@ defListItemToLaTeX (term, defs) = do
426410 def' <- liftM vsep $ mapM blockListToLaTeX defs
427411 return $ "\\item" <> brackets term' $$ def'
428412
413+-- | Craft the section header, inserting the secton reference, if supplied.
414+sectionHeader :: [Char]
415+ -> Int
416+ -> [Inline]
417+ -> State WriterState Doc
418+sectionHeader ref level lst = do
419+ txt <- inlineListToLaTeX lst
420+ let noNote (Note _) = Str ""
421+ noNote x = x
422+ let lstNoNotes = bottomUp noNote lst
423+ -- footnotes in sections don't work unless you specify an optional
424+ -- argument: \section[mysec]{mysec\footnote{blah}}
425+ optional <- if lstNoNotes == lst
426+ then return empty
427+ else do
428+ res <- inlineListToLaTeX lstNoNotes
429+ return $ char '[' <> res <> char ']'
430+ let stuffing = optional <> char '{' <> txt <> char '}'
431+ book <- liftM stBook get
432+ let level' = if book then level - 1 else level
433+ let refLabel lab = (if (not . null) ref
434+ then text "\\hyperdef"
435+ <> braces empty
436+ <> braces (text ref)
437+ <> braces (lab <> text "\\label"
438+ <> braces (text ref))
439+ else lab)
440+ $$ blankline
441+ let headerWith x y = refLabel $ text x <> y
442+ return $ case level' of
443+ 0 -> headerWith "\\chapter" stuffing
444+ 1 -> headerWith "\\section" stuffing
445+ 2 -> headerWith "\\subsection" stuffing
446+ 3 -> headerWith "\\subsubsection" stuffing
447+ 4 -> headerWith "\\paragraph" stuffing
448+ 5 -> headerWith "\\subparagraph" stuffing
449+ _ -> txt $$ blankline
450+
451+
429452-- | Convert list of inline elements to LaTeX.
430453inlineListToLaTeX :: [Inline] -- ^ Inlines to convert
431454 -> State WriterState Doc