Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX table with lot have duplicate List of Table entry #534

Closed
cderv opened this issue Apr 5, 2023 · 11 comments
Closed

LaTeX table with lot have duplicate List of Table entry #534

cderv opened this issue Apr 5, 2023 · 11 comments
Labels

Comments

@cderv
Copy link
Contributor

cderv commented Apr 5, 2023

Just opening an issue to document and track the "issue". This was first reported at quarto-dev/quarto-cli#5093

Example with rmarkdown

---
title: test
output: 
  pdf_document:
    keep_md: true
    keep_tex: true
lot: true
---

# Chapter 01

```{r}
library(flextable)
data.frame(c(rep(1:9,5)),
           c(paste0(rep(1:9,5),"x"))) |> 
  flextable() |> 
  flextable::set_caption(caption = "A Table")
```

As explained in quarto-dev/quarto-cli#5093 (comment) this is a LaTeX thing with longtable splitting table, and having \caption duplicated on each page. They are all picked up by \listoftables

See both

Solution for this seems to be something like using \caption[]{caption} in the right place, in addition of the first header caption so that other header caption are not picked up. (\caption*{} for unnumbered).

Not sure if this should be implemented, but could be an option. I believe this is an issue with any R package using longtable though.

Note the Pandoc specifically deactivated the repetition of caption in the split table - only one at the first table, and so only one table link in the LOT.
https://github.com/jgm/pandoc/blob/ce4e4d2c84f0c5c9b92f67b087fed58b760f0b05/src/Text/Pandoc/Writers/LaTeX/Table.hs#L53-L57

@faaa6
Copy link

faaa6 commented Apr 19, 2023

Are there any updates on this issue?
Here is the Repo where I found this issue:
https://gitup.uni-potsdam.de/farntz/repo

@davidgohel
Copy link
Owner

@faaa6 No, otherwise the issue would have been updated

@njbart
Copy link

njbart commented Apr 26, 2023

The accepted solution for this problem when using LaTeX files, described in multiple SE posts, e.g., here, is to provide separate sets of code for the header of the first page of a longtable and for the header of the subsequent pages, using, for the subsequent pages, a \caption command with an empty optional argument, i.e., \caption[]{A table caption}.

MWE in LaTeX that demonstrates the problem:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{longtable}
\begin{document}
\listoftables
\newpage
\begin{longtable}{l}
  \caption{A table caption}\\
  Column one\\
  \hline
%\endfirsthead
%  \caption[]{A table caption}\\
%  Column one\\
%  \hline
\endhead
  1\\2\\3\\4\\5\\6\\7\\8\\9\\10\\
  11\\12\\13\\14\\15\\16\\17\\18\\19\\20\\
  21\\22\\23\\24\\25\\26\\27\\28\\29\\30\\
\end{longtable}
\end{document}

Result: Duplicate captions in the document’s List of Tables.

Solution: Uncommenting the four commented-out lines removes the duplicate(s).

It would be very welcome if this solution could be applied to the mechanism for generating LaTeX longtables in flextable.

@davidgohel
Copy link
Owner

it should be fixed now

thank you @cderv and @njbart for your inputs (I didn't even have to think, I just read, applied and it worked.)

@njbart
Copy link

njbart commented May 6, 2023

Many thanks for looking into this. The duplicate entries in the list of ’List of Tables’ of a latex/pdf document have indeed disappeared.

However, it seems that you chose not to include \caption[]{A table caption} (with an empty optional argument) in the \endhead part, with the result that now only the first page of a multi-page table has a caption at all. Was this intentional?

I would have preferred to leave everything as it is – with the exception of the erroneously repeated entries in the ’List of Tables’, of course –, i.e., keep repeating the caption on each page of the table.

One could certainly think about making captions even more flexible, i.e. providing an option to activate or deactivate them on subsequent pages, or adding a string like "(continued)" to a caption on pages after the first - but that wasn't my main concern at the moment.

@njbart
Copy link

njbart commented May 6, 2023

What’s more: When using separate_header() to generate multi-row table headers, after updating to the github dev version, only the last header row is now shown on subsequent pages.

MWE: .Rmd file using slightly modified example from https://davidgohel.github.io/flextable/reference/separate_header.html

---
output:
  pdf_document: 
    latex_engine: xelatex
    keep_tex: true
papersize: a5
classoption: landscape
---

```{r test, echo=FALSE, warning=FALSE, message=FALSE, tab.cap="THE CAPTION"}
library(flextable)

x <- data.frame(
  Species = as.factor(c("setosa", "versicolor", "virginica")),
  Sepal.Length_mean = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Sepal.Length_sd = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Sepal.Width_mean = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Sepal.Width_sd = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Petal.Length_mean = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Petal.Length_sd = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Petal.Width_mean = round(runif(n = 24, min = 1, max = 10), digits = 2),
  Petal.Width_sd = round(runif(n = 24, min = 1, max = 10), digits = 2)
)

ft_1 <- flextable(x)
ft_1 <- colformat_double(ft_1, digits = 2)
ft_1 <- theme_box(ft_1)
ft_1 <- separate_header(
  x = ft_1,
  opts = c("span-top", "bottom-vspan")
)
ft_1

As far as the intermediate latex file is concerned, the problem seems to lie in repeated \endhead commands of which of course only the last one wins.

Excerpt (only from the [unique] \endfirsthead to the last \endhead):

\ascline{0.75pt}{666666}{1-9}\endfirsthead

\ascline{0.75pt}{666666}{1-9}

\multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedright}b{\dimexpr 0.75in+0\tabcolsep}}{} & \multicolumn{4}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 3in+6\tabcolsep+2.25pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Sepal}}}}} & \multicolumn{4}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 3in+6\tabcolsep+2.25pt}!{\color[HTML]{666666}\vrule width 0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Petal}}}}} \\

\ascline{0.75pt}{666666}{2-9}\endhead



\multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedright}b{\dimexpr 0.75in+0\tabcolsep}}{} & \multicolumn{2}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 1.5in+2\tabcolsep+0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Length}}}}} & \multicolumn{2}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 1.5in+2\tabcolsep+0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Width}}}}} & \multicolumn{2}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 1.5in+2\tabcolsep+0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Length}}}}} & \multicolumn{2}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 1.5in+2\tabcolsep+0.75pt}!{\color[HTML]{666666}\vrule width 0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Width}}}}} \\

\ascline{0.75pt}{666666}{2-9}\endhead



\multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedright}b{\dimexpr 0.75in+0\tabcolsep}}{\multirow[b]{-3}{*}{\parbox{0.75in}{\raggedright \textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{Species}}}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{mean}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{sd}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{mean}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{sd}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{mean}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{sd}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{mean}}}}} & \multicolumn{1}{!{\color[HTML]{666666}\vrule width 0.75pt}>{\raggedleft}m{\dimexpr 0.75in+0\tabcolsep}!{\color[HTML]{666666}\vrule width 0.75pt}}{\textcolor[HTML]{000000}{\fontsize{11}{11}\selectfont{\global\setmainfont{Helvetica}{\textbf{sd}}}}} \\

\ascline{0.75pt}{666666}{1-9}\endhead

@davidgohel davidgohel reopened this May 6, 2023
davidgohel added a commit that referenced this issue May 7, 2023
@davidgohel
Copy link
Owner

thanks @njbart, apologies, I should have test... The bug is fixed now.

However, it seems that you chose not to include \caption[]{A table caption} (with an empty optional argument) in the \endhead part, with the result that now only the first page of a multi-page table has a caption at all. Was this intentional?
I would have preferred to leave everything as it is – with the exception of the erroneously repeated entries in the ’List of Tables’, of course –, i.e., keep repeating the caption on each page of the table.

OK, I did not realize that captions were repeated on each PDF page - I understand this is a change and should be fixed

One could certainly think about making captions even more flexible, i.e. providing an option to activate or deactivate them on subsequent pages, or adding a string like "(continued)" to a caption on pages after the first - but that wasn't my main concern at the moment.

probably related to #394. I can't figure out how the user should specify such an option, any suggestion is welcome

@njbart
Copy link

njbart commented May 7, 2023

Thanks, no problem. I can confirm that multi-row table headers generated by separate_header() work as expected again now.

@njbart
Copy link

njbart commented May 7, 2023

For enabling or disabling captions on subsequent pages I could imagine a new argument for set_caption(), e.g. repeat_caption = TRUE / FALSE, where TRUE is the default value.

@davidgohel
Copy link
Owner

davidgohel commented May 9, 2023

@njbart it should be fixed now - again!

Copy link

github-actions bot commented Nov 9, 2023

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants