git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Better treatment of footnotes in tables.
8053ba2John MacFarlane
src/Text/Pandoc/Writers/LaTeX.hs+29−6
@@ -50,6 +50,8 @@ import Text.Pandoc.Highlighting (highlight, styleToLaTeX,
50
50
51data WriterState =
51data WriterState =
52 WriterState { stInNote :: Bool -- true if we're in a note
52 WriterState { stInNote :: Bool -- true if we're in a note
53+ , stInMinipage :: Bool -- true if in minipage
54+ , stNotes :: [Doc] -- notes in a minipage
53 , stOLLevel :: Int -- level of ordered list nesting
55 , stOLLevel :: Int -- level of ordered list nesting
54 , stOptions :: WriterOptions -- writer options, so they don't have to be parameter
56 , stOptions :: WriterOptions -- writer options, so they don't have to be parameter
55 , stVerbInNote :: Bool -- true if document has verbatim text in note
57 , stVerbInNote :: Bool -- true if document has verbatim text in note
@@ -70,7 +72,7 @@ data WriterState =
70writeLaTeX :: WriterOptions -> Pandoc -> String
72writeLaTeX :: WriterOptions -> Pandoc -> String
71writeLaTeX options document =
73writeLaTeX options document =
72 evalState (pandocToLaTeX options document) $
74 evalState (pandocToLaTeX options document) $
73− WriterState { stInNote = False,
75+ WriterState { stInNote = False, stInMinipage = False, stNotes = [],
74 stOLLevel = 1, stOptions = options,
76 stOLLevel = 1, stOptions = options,
75 stVerbInNote = False,
77 stVerbInNote = False,
76 stTable = False, stStrikeout = False,
78 stTable = False, stStrikeout = False,
@@ -501,16 +503,31 @@ tableCellToLaTeX :: Bool -> (Double, Alignment, [Block])
501 -> State WriterState Doc
503 -> State WriterState Doc
502tableCellToLaTeX _ (0, _, blocks) = blockListToLaTeX blocks
504tableCellToLaTeX _ (0, _, blocks) = blockListToLaTeX blocks
503tableCellToLaTeX header (width, align, blocks) = do
505tableCellToLaTeX header (width, align, blocks) = do
506+ modify $ \st -> st{ stInMinipage = True, stNotes = [] }
504 cellContents <- blockListToLaTeX blocks
507 cellContents <- blockListToLaTeX blocks
508+ notes <- gets stNotes
509+ modify $ \st -> st{ stInMinipage = False, stNotes = [] }
505 let valign = text $ if header then "[b]" else "[t]"
510 let valign = text $ if header then "[b]" else "[t]"
506 let halign = case align of
511 let halign = case align of
507 AlignLeft -> "\\raggedright"
512 AlignLeft -> "\\raggedright"
508 AlignRight -> "\\raggedleft"
513 AlignRight -> "\\raggedleft"
509 AlignCenter -> "\\centering"
514 AlignCenter -> "\\centering"
510 AlignDefault -> "\\raggedright"
515 AlignDefault -> "\\raggedright"
511− return $ "\\begin{minipage}" <> valign <>
516+ return $ ("\\begin{minipage}" <> valign <>
512− braces (text (printf "%.2f\\columnwidth" width)) <>
517+ braces (text (printf "%.2f\\columnwidth" width)) <>
513− (halign <> cr <> cellContents <> cr) <> "\\end{minipage}"
518+ (halign <> cr <> cellContents <> cr) <> "\\end{minipage}")
519+ $$ case notes of
520+ [] -> empty
521+ ns -> (case length ns of
522+ n | n > 1 -> "\\addtocounter" <>
523+ braces "footnote" <>
524+ braces (text $ show $ 1 - n)
525+ | otherwise -> empty)
526+ $$
527+ vcat (intersperse
528+ ("\\addtocounter" <> braces "footnote" <> braces "1")
529+ $ map (\x -> "\\footnotetext" <> braces x)
530+ $ reverse ns)
514
531
515listItemToLaTeX :: [Block] -> State WriterState Doc
532listItemToLaTeX :: [Block] -> State WriterState Doc
516listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
533listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
@@ -713,14 +730,20 @@ inlineToLaTeX (Image _ (source, _)) = do
713 source'' <- stringToLaTeX URLString source'
730 source'' <- stringToLaTeX URLString source'
714 return $ "\\includegraphics" <> braces (text source'')
731 return $ "\\includegraphics" <> braces (text source'')
715inlineToLaTeX (Note contents) = do
732inlineToLaTeX (Note contents) = do
733+ inMinipage <- gets stInMinipage
716 modify (\s -> s{stInNote = True})
734 modify (\s -> s{stInNote = True})
717 contents' <- blockListToLaTeX contents
735 contents' <- blockListToLaTeX contents
718 modify (\s -> s {stInNote = False})
736 modify (\s -> s {stInNote = False})
719 let optnl = case reverse contents of
737 let optnl = case reverse contents of
720 (CodeBlock _ _ : _) -> cr
738 (CodeBlock _ _ : _) -> cr
721 _ -> empty
739 _ -> empty
722− return $ "\\footnote" <> braces (nest 2 contents' <> optnl)
740+ let noteContents = nest 2 contents' <> optnl
741+ modify $ \st -> st{ stNotes = noteContents : stNotes st }
742+ return $
743+ if inMinipage
744+ then "\\footnotemark{}"
723− -- note: a \n before } needed when note ends with a Verbatim environment
745+ -- note: a \n before } needed when note ends with a Verbatim environment
746+ else "\\footnote" <> braces noteContents
724
747
725citationsToNatbib :: [Citation] -> State WriterState Doc
748citationsToNatbib :: [Citation] -> State WriterState Doc
726citationsToNatbib (one:[])
749citationsToNatbib (one:[])