kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Inconsistencies in kable_styling() make full-with tables difficult with Quarto

Open EricMarcon opened this issue 1 year ago • 0 comments

The default behavior of kable_styling() in \LaTeX is to make the table float. Yet, the argument full_width = "TRUE"changes this behavior, preventing its use in Quarto documents.

The code below shows that in details. First, kable produces a non-floating table:

library("tidyverse")
# kable's output
iris[1:2,] %>% knitr::kable(format = "latex") 

returns

\begin{tabular}{r|r|r|r|l} \hline Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\ \hline 5.1 & 3.5 & 1.4 & 0.2 & setosa\ \hline \end{tabular}

kable_styling() makes it float:

library("kableExtra")
iris[1,] %>% knitr::kable(format = "latex") %>% kable_styling()

\begin{table} \centering \begin{tabular}{r|r|r|r|l} \hline Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\ \hline 5.1 & 3.5 & 1.4 & 0.2 & setosa\ \hline \end{tabular} \end{table}

Argument full_width = TRUE replaces "tabular" by "tabu" as expected but does not make the table float:

iris[1,] %>% knitr::kable(format = "latex") %>% kable_styling(full_width = TRUE)

\begin{tabu} to \linewidth {>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedright}X} \hline Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\ \hline 5.1 & 3.5 & 1.4 & 0.2 & setosa\ \hline \end{tabu}

Unless kable provides a caption:

iris[1,] %>% knitr::kable(format = "latex", caption = "A table") %>% kable_styling(full_width = TRUE)

\begin{table} \caption{A table} \centering \begin{tabu} to \linewidth {>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedright}X} \hline Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\ \hline 5.1 & 3.5 & 1.4 & 0.2 & setosa\ \hline \end{tabu} \end{table}

because the "table" environment is in the output of kable().

This is not consistent.

A direct issue is that code that compiles well with knitr does not with Quarto. With Quarto, the argument caption of kable is not used (it actually produces invalid LaTeX). The table caption is in the code chunk option #| tbl-cap: and is added after \begin{table}. Since kable_styling(full_width = TRUE) does not produce a "table" environment, the caption of the table is just not inserted into the LaTeX output.

I suggest to have a consistent behavior of kable_styling() that should not add a table environment by default but allow it with an argument or more simply document that kable(..., table.envir = "table") does it if necessary. This code chunk produces a correct full-width table in Quarto:

iris[1,] %>% knitr::kable(format = "latex", table.envir = "table") %>% kable_styling(full_width = TRUE) 

The alternative is to always add the "table" environment, but this is not a good idea, e.g. when long tables are sliced along several pages by the LaTeX package "longtable".

EricMarcon avatar Mar 06 '23 14:03 EricMarcon