From 81e31ee637935ce7ba076bdd5a648c2489698d2d Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert@zeitkraut.de>
Date: Wed, 17 Aug 2022 12:21:47 +0200
Subject: [PATCH] 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
---
 src/Text/Pandoc/Writers/LaTeX/Table.hs | 19 ++++++++++++-------
 test/command/8219.md                   | 14 ++++++++++++++
 test/tables/nordics.latex              |  2 +-
 test/tables/students.latex             |  2 +-
 4 files changed, 28 insertions(+), 9 deletions(-)
 create mode 100644 test/command/8219.md

diff --git a/src/Text/Pandoc/Writers/LaTeX/Table.hs b/src/Text/Pandoc/Writers/LaTeX/Table.hs
index 38ba60764..4fbebd1be 100644
--- a/src/Text/Pandoc/Writers/LaTeX/Table.hs
+++ b/src/Text/Pandoc/Writers/LaTeX/Table.hs
@@ -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)
diff --git a/test/command/8219.md b/test/command/8219.md
new file mode 100644
index 000000000..52279cf42
--- /dev/null
+++ b/test/command/8219.md
@@ -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}
+```
diff --git a/test/tables/nordics.latex b/test/tables/nordics.latex
index a5e7bc1be..943f70fbf 100644
--- a/test/tables/nordics.latex
+++ b/test/tables/nordics.latex
@@ -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
diff --git a/test/tables/students.latex b/test/tables/students.latex
index 8a9ae6e6b..c93b733c3 100644
--- a/test/tables/students.latex
+++ b/test/tables/students.latex
@@ -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