LaTeX writer: add label to tables that have an identifier

Tables with an identifier are marked with a `\label`. A caption is
always included in this case, even if the caption is empty.

Closes: #8219
This commit is contained in:
Albert Krewinkel 2022-08-17 12:21:47 +02:00
parent c5f8a38f38
commit 81e31ee637
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
4 changed files with 28 additions and 9 deletions

View file

@ -33,6 +33,7 @@ import Text.Pandoc.Writers.LaTeX.Notes (notesToLaTeX)
import Text.Pandoc.Writers.LaTeX.Types
( LW, WriterState (stBeamer, stExternalNotes, stInMinipage, stMultiRow
, stNotes, stTable) )
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
@ -43,8 +44,8 @@ tableToLaTeX :: PandocMonad m
-> Ann.Table
-> LW m (Doc Text)
tableToLaTeX inlnsToLaTeX blksToLaTeX tbl = do
let (Ann.Table _attr caption specs thead tbodies tfoot) = tbl
CaptionDocs capt captNotes <- captionToLaTeX inlnsToLaTeX caption
let (Ann.Table (ident, _, _) caption specs thead tbodies tfoot) = tbl
CaptionDocs capt captNotes <- captionToLaTeX inlnsToLaTeX caption ident
let removeNote (Note _) = Span ("", [], []) []
removeNote x = x
let colCount = ColumnCount $ length specs
@ -144,16 +145,20 @@ data CaptionDocs =
captionToLaTeX :: PandocMonad m
=> ([Inline] -> LW m (Doc Text))
-> Caption
-> Text -- ^ table identifier (label)
-> LW m CaptionDocs
captionToLaTeX inlnsToLaTeX (Caption _maybeShort longCaption) = do
captionToLaTeX inlnsToLaTeX (Caption _maybeShort longCaption) ident = do
let caption = blocksToInlines longCaption
(captionText, captForLof, captNotes) <- getCaption inlnsToLaTeX False caption
(captionText, captForLot, captNotes) <- getCaption inlnsToLaTeX False caption
label <- labelFor ident
return $ CaptionDocs
{ captionNotes = captNotes
, captionCommand = if isEmpty captionText
, captionCommand = if isEmpty captionText && isEmpty label
then empty
else "\\caption" <> captForLof <>
braces captionText <> "\\tabularnewline"
else "\\caption" <> captForLot <>
braces captionText
<> label
<> "\\tabularnewline"
}
type BlocksWriter m = [Block] -> LW m (Doc Text)

14
test/command/8219.md Normal file
View file

@ -0,0 +1,14 @@
```
% pandoc -f html -t latex
<table id="test">
<tr><td>one</td><td>two</td></tr>
</table>
^D
\begin{longtable}[]{@{}ll@{}}
\caption{}\label{test}\tabularnewline
\toprule()
\endhead
one & two \\
\bottomrule()
\end{longtable}
```

View file

@ -3,7 +3,7 @@
>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3000}}
>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2000}}
>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2000}}@{}}
\caption{States belonging to the \emph{Nordics.}}\tabularnewline
\caption{States belonging to the \emph{Nordics.}}\label{nordics}\tabularnewline
\toprule()
\begin{minipage}[b]{\linewidth}\centering
Name

View file

@ -1,7 +1,7 @@
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5000}}
>{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5000}}@{}}
\caption{List of Students}\tabularnewline
\caption{List of Students}\label{students}\tabularnewline
\toprule()
\begin{minipage}[b]{\linewidth}\centering
Student ID