vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
170 stars 9 forks source link

Missing `tabular`, `tblr`, `$`, etc. in latex preamble #269

Open avehtari opened 3 weeks ago

avehtari commented 3 weeks ago

The document says

In Rmarkdown and Quarto documents, tinytable will automatically populate your LaTeX
preamble with the necessary packages and commands.

I'm rendering quarto to LaTeX with

quarto render casestudies/Nabiximols/nabiximols.R --to latex

and I don't get the necessary things in preamble.

It was also quite confusing as there is no package called tblr, and eventually learning about tabular package, it is not sufficient to load just package tabular, etc. As the LaTeX / PDF information was not visible in the tinytable web page, it took a long time to find the relevant information; it seems Google search fails to point to the PDF and using browser search in page functionality did not find anything. It would be nice to include some LaTeX specific keywords like tblr to the html pages, too, so that if someone else tries to search, they would find the information more easily.

vincentarelbundock commented 3 weeks ago

I'm sorry to hear about these difficulties, and I agree that this should be better documented. I improved the text in ?tt, which appears in the PDF version of the manual on CRAN and in the function reference on the website. I also copied the same text to the LaTeX section of the FAQ page, so the warning should appear twice on the HTML website:

https://vincentarelbundock.github.io/tinytable/man/tt.html#latex-preamble https://vincentarelbundock.github.io/tinytable/vignettes/faq.html#preamble

 ‘tinytable’ uses the ‘tabularray’ package from your LaTeX
 distribution to draw tables. ‘tabularray’, in turn, uses the
 special ‘tblr’, ‘talltblr’, and ‘longtblr’ environments.

 When rendering a document from Quarto or Rmarkdown directly to
 PDF, ‘tinytable’ will populate the LaTeX preamble automatically
 with all the required packages (except when code chunks are
 cached). For standalone LaTeX documents, these commands should be
 inserted in the preamble manually:

 \usepackage{tabularray}
 \usepackage{float}
 \usepackage{graphicx}
 \usepackage[normalem]{ulem}
 \UseTblrLibrary{booktabs}
 \UseTblrLibrary{siunitx}
 \newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
 \newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
 \NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}
avehtari commented 3 weeks ago

Thanks for improving the doc. These changes look helpful.

Do you think this should include the preamble block automatically, or is it addede automatically if rendering directly to PDF?

quarto render casestudies/Nabiximols/nabiximols.R --to latex

Related latex problem: What is your recommendation to automate escaping underscore _ in variable names when I make a table where one column includes brms variable names which have underscores? After adding manually the preamble block, I get the ERROR: Missing $ inserted. because of underscores in variable names in tables, and I would not like to manually edit the tables.

vincentarelbundock commented 3 weeks ago

Do you think this should include the preamble block automatically, or is it addede automatically if rendering directly to PDF?

quarto render casestudies/Nabiximols/nabiximols.R --to latex

Yes, on my computer this seems to work. For example, I just saved this to example.R:

#' ---
#' format: latex
#' ---

#' Some comments before a table
library(tinytable)
tt(head(iris))

Then, I called:

quarto render example.R --to latex

And the resulting .tex file does include the packages in preamble.

vincentarelbundock commented 3 weeks ago

FYI, I'm working on something that will make escaping colnames and column labels easy. Will ping you when done (very soon).

avehtari commented 3 weeks ago

Hmm, I tested with your minimal example and did not get necessary packages in preamble

In case I have wrong versions:

I have quarto version 1.4.550

Here is the rendering info

> quarto render test.R --to latex

processing file: test.R
1/3                  
2/3 [unnamed-chunk-1]
3/3                  
output file: test.knit.md

pandoc 
  to: latex
  output-file: test.tex
  standalone: true
  pdf-engine: xelatex
  variables:
    graphics: true
    tables: true
  default-image-extension: pdf

metadata
  documentclass: scrartcl
  classoption:
    - DIV=11
    - numbers=noendperiod
  papersize: letter
  header-includes:
    - '\KOMAoption{captions}{tableheading}'
  block-headings: true

Output created: test.tex

And here is the sessionInfo() (added to test.tex)

R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_DK.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=fi_FI.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Helsinki
tzcode source: system (glibc)

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

other attached packages:
[1] tinytable_0.3.0.5

loaded via a namespace (and not attached):
 [1] compiler_4.4.0    fastmap_1.1.1     cli_3.6.2         tools_4.4.0      
 [5] htmltools_0.5.8.1 yaml_2.3.8        fansi_1.0.6       rmarkdown_2.26   
 [9] knitr_1.46        jsonlite_1.8.8    xfun_0.43         digest_0.6.35    
[13] rlang_1.1.3       evaluate_0.23    
vincentarelbundock commented 3 weeks ago

With version 0.3.0.7 of tinytable you can escape every cell and column name:

library(tinytable)
dat <- data.frame("foo$bar" = c("a_b", "b_c"), "hello$world" = 2:3, check.names = FALSE)

tt(dat) |> 
  format_tt(escape = TRUE) |>
  print("latex")

> \begin{table}
> \centering
> \begin{tblr}[         %% tabularray outer open
> ]                     %% tabularray outer close
> {                     %% tabularray inner open
> colspec={Q[]Q[]},
> }                     %% tabularray inner close
> \toprule
> foo\$bar & hello\$world \\ \midrule %% TinyTableHeader
> a\_b & 2 \\
> b\_c & 3 \\
> \bottomrule
> \end{tblr}
> \end{table}

Or just the column names by specifying the i position:

tt(dat) |> 
  format_tt(i = 0, escape = TRUE) |>
  print("latex")

> \begin{table}
> \centering
> \begin{tblr}[         %% tabularray outer open
> ]                     %% tabularray outer close
> {                     %% tabularray inner open
> colspec={Q[]Q[]},
> }                     %% tabularray inner close
> \toprule
> foo\$bar & hello\$world \\ \midrule %% TinyTableHeader
> a_b & 2 \\
> b_c & 3 \\
> \bottomrule
> \end{tblr}
> \end{table}
vincentarelbundock commented 3 weeks ago

The LaTeX preamble issue is a mystery. I installed the same quarto as you and am using essentially the same versions of everything, and I do get the preamble.

Do you have knitr installed on that machine? The logic to insert stuff in the preamble is:

  1. If knitr is installed
  2. If knitr::pandoc_to() %in% c("latex", "beamer")
  3. Then, insert stuff in the preamble.

tinytable only draws table. It can't guess --- on its own --- what output format you're going for.

That's my only guess that the moment...

avehtari commented 3 weeks ago
  1. If I add

    knitr::pandoc_to() %in% c("latex", "beamer")

    to test.R it outputs TRUE, but no stuff added to the preamble. I also tested updating all installed packages, but no change. I don't expect you to be able to solve this, but at least now you know that it is possible that some people get latex errors if they don't notice that they need to manually add the preamble, and it helps that you improved the documentation

  2. Thanks for the escape!

vincentarelbundock commented 3 weeks ago

Thanks for taking the time to check and report. This is useful info.

But I'm stumped. Let's leave this issue open. I'll keep thinking about it and perhaps someone else will chime in with some ideas.