Skip to content

Commit

Permalink
LaTeX writer: make Figure and Table sensitive to caption position.
Browse files Browse the repository at this point in the history
Closes #5116.
  • Loading branch information
jgm committed Sep 7, 2024
1 parent d2581c3 commit 215feb4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ blockToLaTeX (Table attr blkCapt specs thead tbodies tfoot) =
tableToLaTeX inlineListToLaTeX blockListToLaTeX
(Ann.toTable attr blkCapt specs thead tbodies tfoot)
blockToLaTeX (Figure (ident, _, _) captnode body) = do
opts <- gets stOptions
(capt, captForLof, footnotes) <- getCaption inlineListToLaTeX True captnode
lab <- labelFor ident
let caption = "\\caption" <> captForLof <> braces capt <> lab
Expand All @@ -615,7 +616,10 @@ blockToLaTeX (Figure (ident, _, _) captnode body) = do
[b] -> blockToLaTeX b
bs -> mconcat . intersperse (cr <> "\\hfill") <$>
mapM (toSubfigure (length bs)) bs
let innards = "\\centering" $$ contents $$ caption <> cr
let innards = "\\centering" $$
(case writerFigureCaptionPosition opts of
CaptionBelow -> contents $$ caption
CaptionAbove -> caption $$ contents) <> cr
modify $ \st ->
st{ stInFigure = isSubfigure
, stSubfigure = stSubfigure st || isSubfigure
Expand Down
44 changes: 27 additions & 17 deletions src/Text/Pandoc/Writers/LaTeX/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,26 @@ import Text.Pandoc.Writers.LaTeX.Caption (getCaption)
import Text.Pandoc.Writers.LaTeX.Notes (notesToLaTeX)
import Text.Pandoc.Writers.LaTeX.Types
( LW, WriterState (stBeamer, stExternalNotes, stInMinipage, stMultiRow
, stNotes, stTable) )
, stNotes, stTable, stOptions) )
import Text.Pandoc.Writers.LaTeX.Util (labelFor)
import Text.Printf (printf)
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.Writers.AnnotatedTable as Ann
import Text.Pandoc.Options (CaptionPosition(..), WriterOptions(..))

tableToLaTeX :: PandocMonad m
=> ([Inline] -> LW m (Doc Text))
-> ([Block] -> LW m (Doc Text))
-> Ann.Table
-> LW m (Doc Text)
tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
opts <- gets stOptions
let (Ann.Table (ident, _, _) caption specs thead tbodies tfoot) = tbl
CaptionDocs capt captNotes <- captionToLaTeX inlnsToLaTeX caption ident
let hasTopCaption = not (isEmpty capt) &&
writerTableCaptionPosition opts == CaptionAbove
let hasBottomCaption = not (isEmpty capt) &&
writerTableCaptionPosition opts == CaptionBelow
let isSimpleTable =
all ((== ColWidthDefault) . snd) specs &&
all (all isSimpleCell)
Expand All @@ -64,24 +70,31 @@ tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
-- duplicate the header rows for this.
head' <- do
let mkHead = headToLaTeX blksToLaTeX isSimpleTable colCount
case (not $ isEmpty capt, not $ isEmptyHead thead) of
(False, False) -> return "\\toprule\\noalign{}"
(False, True) -> mkHead thead
(True, False) -> return (capt $$ "\\toprule\\noalign{}" $$ "\\endfirsthead")
(True, True) -> do
case (hasTopCaption, isEmptyHead thead) of
(False, True) -> return "\\toprule\\noalign{}"
(False, False) -> mkHead thead
(True, True) -> return (capt <> "\\tabularnewline"
$$ "\\toprule\\noalign{}"
$$ "\\endfirsthead")
(True, False) -> do
-- avoid duplicate notes in head and firsthead:
firsthead <- mkHead thead
repeated <- mkHead (walk removeNote thead)
return $ capt $$ firsthead $$ "\\endfirsthead" $$ repeated
return $ capt <> "\\tabularnewline"
$$ firsthead
$$ "\\endfirsthead"
$$ repeated
rows' <- mapM (rowToLaTeX blksToLaTeX isSimpleTable colCount BodyCell) $
mconcat (map bodyRows tbodies)
foot' <- if isEmptyFoot tfoot
then pure empty
else do
lastfoot <- mapM
(rowToLaTeX blksToLaTeX isSimpleTable colCount BodyCell) $
footRows tfoot
pure $ "\\midrule\\noalign{}" $$ vcat lastfoot
lastfoot <- mapM (rowToLaTeX blksToLaTeX isSimpleTable colCount BodyCell) $
footRows tfoot
let foot' = (if isEmptyFoot tfoot
then mempty
else "\\midrule\\noalign{}" $$ vcat lastfoot)
$$ "\\bottomrule\\noalign{}"
$$ (if hasBottomCaption
then "\\tabularnewline" $$ capt
else mempty)
modify $ \s -> s{ stTable = True }
notes <- notesToLaTeX <$> gets stNotes
beamer <- gets stBeamer
Expand All @@ -99,10 +112,8 @@ tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
(if beamer
then [ vcat rows'
, foot'
, "\\bottomrule\\noalign{}"
]
else [ foot'
, "\\bottomrule\\noalign{}"
, "\\endlastfoot"
, vcat rows'
])
Expand Down Expand Up @@ -192,7 +203,6 @@ captionToLaTeX inlnsToLaTeX caption ident = do
else "\\caption" <> captForLot <>
braces captionText
<> label
<> "\\tabularnewline"
}

type BlocksWriter m = [Block] -> LW m (Doc Text)
Expand Down

0 comments on commit 215feb4

Please sign in to comment.