rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.03k stars 209 forks source link

Ensure that LaTeX output does not use `\caption` for the table heading since that should be reserved for a functional caption #956

Open rich-iannone opened 2 years ago

rich-iannone commented 2 years ago

If I generate a LaTeX table with a table heading (using tab_header()) the resulting LaTeX code generated contains the use of a \caption command. For example, I could use

exibble %>% gt() %>% tab_header("title") %>% as_latex() %>% as.character() %>% cat()

and get

\captionsetup[table]{labelformat=empty,skip=1pt}
\begin{longtable}{rlclllrll}
\caption*{
{\large title}
} \\ 
\toprule
num & char & fctr & date & time & datetime & currency & row & group \\ 
\midrule
1.111e-01 & apricot & one & 2015-01-15 & 13:35 & 2018-01-01 02:22 & 49.950 & row\_1 & grp\_a \\ 
2.222e+00 & banana & two & 2015-02-15 & 14:40 & 2018-02-02 14:33 & 17.950 & row\_2 & grp\_a \\ 
3.333e+01 & coconut & three & 2015-03-15 & 15:45 & 2018-03-03 03:44 & 1.390 & row\_3 & grp\_a \\ 
4.444e+02 & durian & four & 2015-04-15 & 16:50 & 2018-04-04 15:55 & 65100.000 & row\_4 & grp\_a \\ 
5.550e+03 & NA & five & 2015-05-15 & 17:55 & 2018-05-05 04:00 & 1325.810 & row\_5 & grp\_b \\ 
NA & fig & six & 2015-06-15 & NA & 2018-06-06 16:11 & 13.255 & row\_6 & grp\_b \\ 
7.770e+05 & grapefruit & seven & NA & 19:10 & 2018-07-07 05:22 & NA & row\_7 & grp\_b \\ 
8.880e+06 & honeydew & eight & 2015-08-15 & 20:20 & NA & 0.440 & row\_8 & grp\_b \\ 
\bottomrule
\end{longtable}

in the console. The issue is that this use of a caption interferes with the use of an actual figure/table caption used in R Markdown / bookdown / Quarto documents. We ought to use anything other than a caption to typeset a table heading. Then work can be done in Quarto to properly generate a caption based on a number of ways to specify a table caption.

cderv commented 2 years ago

I think it is also part of this same issue but this

\captionsetup[table]{labelformat=empty,skip=1pt}

would need to change as this will cause table caption in quarto to have no label (labelformat=empty) image

This above is missing the Table 1: that is expected as documented in Quarto: https://quarto.org/docs/authoring/tables.html#computations

dan-cloney commented 2 years ago

I am experiencing the same issue with tab_header. It breaks the captioning and cross refs in Quarto. No table is produced in the output.

---
title: "quarto gt"
format: html
---

## Quarto

A table @tbl-gt.

```{r}
#| label: tbl-gt
#| tbl-cap: A Caption
#| echo: false

library(gt)

myTable <- mtcars |> 
  head() |>
  gt() |>  
  tab_header(title = "mtcars") 

## Output
results:

WARNING: Unable to resolve crossref @tbl-gt


and html output:

<section id="quarto" class="level2">
<h2 class="anchored" data-anchor-id="quarto">Quarto</h2>
<p>A table <strong>?@tbl-gt</strong>.</p>
<div id="tbl-gt" class="cell tbl-parent quarto-layout-panel anchored" data-tbl-cap="A Caption">

</div>
</section>

## Session info

packageVersion("gt") [1] ‘0.6.0.9000’

sessionInfo() R version 4.2.1 (2022-06-23) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.4

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods
[7] base

loaded via a namespace (and not attached): [1] compiler_4.2.1 fastmap_1.1.0 cli_3.3.0
[4] htmltools_0.5.3 tools_4.2.1 rstudioapi_0.13 [7] yaml_2.3.5 rmarkdown_2.14.2 knitr_1.39
[10] xfun_0.31 digest_0.6.29 rlang_1.0.4
[13] evaluate_0.15

% quarto --version
1.0.37 % quarto pandoc --version pandoc 2.18

kendonB commented 9 months ago

I believe that the normal usage here when combining quarto and gt should be that gt only generates the interior of the table and the qmd file does the rest. It's currently hacky to be able to do this, but at least you get control over the float, title, caption, notes, and column widths.

\begin{table}[h]
\caption{Table title with Table 1}
\begin{center}
\begin{tabular}{rlcrrrrll}
`r library(gt); tmp <- exibble |> 
    gt::gt() |>
    gt::as_latex() 
  tmp[1] <- tmp[1] |>
    # remove first and last lines
    stringr::str_remove("^.*\n") |> 
    stringr::str_remove("\\\\end\\{longtable\\}.*$")
  tmp
`
\end{tabular}
\end{center}
\tiny These are notes
\label{table:exampletable}
\end{table}

The relevance to this issue is that if, gt decides this should be the standard latex behaviour, any captions, titles, or notes should just be stripped with a warning that potentially gives some content for the user to copy-paste.

olivroy commented 2 months ago

Does this conflict with #1800?