Skip to contents

LaTeX

Preamble

tinytable uses the tabularray package from your LaTeX distribution to draw tables. tabularray, in turn, provides special tblr, talltblr, and longtblr environments to display tabular data.

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{rotating}
\usepackage[normalem]{ulem}
\UseTblrLibrary{siunitx}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}

setspace

Some users have encountered unexpected spacing behavior when generating tables that are not wrapped in a \begin{table} environment (ex: multipage or raw tblr).

One issue stems from the fact that the \begin{table} environment resets any spacing commands in the preamble or body by default, such as:

\usepackage{setspace}
\doublespacing

This means that when using theme_latex(environment="tabular") —which does not wrap the table in a table environment— the spacing is not reset, and tables are double spaced. This is not a bug, since double-spacing is in fact what the user requested. Nevertheless, the behavior can seem surprising for those used to the automagical table environment spacing reset.

One workaround is to add the following to the document preamble when using multipage/longtblr:

\usepackage{etoolbox}
\AtBeginEnvironment{longtblr}{\begin{singlespacing}}
\AtEndEnvironment{longtblr}{\end{singlespacing}}

Example Quarto doc:

---
title: longtblr and setspacing
format:
  pdf:
    include-in-header:
      - text: |
         % Tinytable preamble
         \usepackage{tabularray}
         \usepackage{float}
         \usepackage{graphicx}
         \usepackage{codehigh}
         \usepackage[normalem]{ulem}
         \UseTblrLibrary{booktabs}
         \newcommand{\tinytableTabularrayUnderline}[1]{\underline
         {#1}}
         \newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
         \NewTableCommand{\tinytableDefineColor}[3]{\definecolor{
         #1}{#2}{#3}}
         % Spacing Commands
         \usepackage{setspace}
         \doublespacing
         % Fix Spacing in longtblr
         \usepackage{etoolbox}
         \AtBeginEnvironment{longtblr}{\begin{singlespacing}}
         \AtEndEnvironment{longtblr}{\end{singlespacing}}
---

```{=latex}
\begin{longtblr}[         %% tabularray outer open
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]},
}                     %% tabularray inner close
\toprule
foo & bar & baz \\
foo & bar & baz \\
foo & bar & baz \\
\bottomrule
\end{longtblr}
```

Multi-line cells with minipage

In some contexts, users may want create cells with LaTeX or markdown code that spans multiple lines. This usually works well for HTML tables. But sometimes, in LaTeX, multi-line content with special environments must be wrapped in a minipage environment.

In the example that follows, we create a Markdown list using asterisks. Then, we call litedown::mark() to render that list as bullet points (an itemize environment in LaTeX). Finally, we define a custom function called minipage to wrap the bullet point in an environment.

library(tinytable)
library(litedown)

dat <- data.frame(
  A = c("Blah *blah* blah", "- Thing 1\n- Thing 2"),
  B = c("6%", "$5.29")
)

minipage <- function(x) {
  sprintf("\\begin{minipage}{\\textwidth}%s\\end{minipage}", mark(x, "latex"))
}
tab <- tt(dat, width = c(0.3, 0.2)) |>
  style_tt(j = 2, align = "c") |>
  format_tt(escape = TRUE) |>
  format_tt(j = 1, fn = mark, output = "html") |>
  format_tt(j = 1, fn = minipage, output = "latex")

tab
A B

Blah blah blah

  • Thing 1
  • Thing 2
6%

Blah blah blah

  • Thing 1
  • Thing 2
$5.29

Global styles

tabularray allows very powerful styling and themeing options. See the reference manual for more information.

For example, you can change the size of footnotes in all tables of a document with:

---
format: 
    pdf:
        keep-tex: true
        header-includes: |
            \SetTblrStyle{foot}{font=\LARGE}
---

```{r}
library(tinytable)
library(magrittr)
tt(head(iris), notes = "Blah blah")
```

Beamer

Due to a bug in the upstream package rmarkdown, Quarto or Rmarkdown presentations compiled to Beamer cannot include adequate package loading commands in the preamble automatically. This bug prevents tinytable::usepackage_latex() from modifying the preamble. Here’s a workaround.

Save this LaTeX code as preamble.tex:

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

Then, load preamble.tex in your YAML header:

---
output: 
  beamer_presentation:
    includes:
      in_header: preamble.tex
---

With these changes, the table should appear with colors as expected.

Label and caption position

In LaTeX, we can use tabularray options in the preamble or the table to change the location of the label and caption. The example below shows a Quarto document with caption at the bottom.

---
output: 
    pdf_document:
header-includes:
  - \usepackage{tabularray}
 ---

```{=latex}
\DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}
\DefTblrTemplate{firstfoot,middlefoot}{default}{}
\DefTblrTemplate{lastfoot}{default}% 
{
  \UseTblrTemplate{caption}{default}
}
```

```{r, echo=FALSE}
library(modelsummary)
library(tinytable)
mod <- list()
mod[['One variable']] <- lm(mpg ~ hp, mtcars)
mod[['Two variables']] <- lm(mpg ~ hp + drat, mtcars)

modelsummary(mod,
             title = "Regression Models")|>
  theme_latex(outer = "label={tblr:test}")
```

Table \ref{tblr:test}

Placement

The theme_latex() function includes placement control over the positioning of the table in LaTeX documents, using floating parameters like H (from the float LaTeX package) to specify where the table should appear.

options(tinytable_latex_placement = NULL)
x <- head(mtcars)
tt(x) |>
  theme_latex(placement = "H") |>
  print(output = "latex")
\begin{table}[H]
\centering
\begin{tblr}[         %% tabularray outer open
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]},
hline{2}={1-11}{solid, black, 0.05em},
hline{1}={1-11}{solid, black, 0.08em},
hline{8}={1-11}{solid, black, 0.08em},
}                     %% tabularray inner close
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\
21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\
21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\
22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\
21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\
18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\
18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1 \\
\end{tblr}
\end{table} 

Resize

Use resize_direction = "down" to scale tables that are wider than the available line width.

tmp <- cbind(head(mtcars), head(mtcars))
tt(tmp) |>
  theme_latex(resize_direction = "down", placement = "H")
mpg cyl disp hp drat wt qsec vs am gear carb mpg cyl disp hp drat wt qsec vs am gear carb
21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Rotate

tt(head(iris), caption = "Rotated table.") |>
  theme_rotate(angle = 45)
Rotated table.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Multipage

The multipage theme is designed for LaTeX documents to allow long tables to continue across multiple pages. This theme ensures that tables are not truncated and that all data is presented clearly.

tmp <- rbind(mtcars, mtcars)[, 1:6]

cap <- "A long 80\\% width table with repeating headers."

tt(tmp, width = .8, caption = cap) |>
  theme_latex(multipage = TRUE, rowhead = 1)
A long 80\% width table with repeating headers.
mpg cyl disp hp drat wt
21.0 6 160.0 110 3.90 2.620
21.0 6 160.0 110 3.90 2.875
22.8 4 108.0 93 3.85 2.320
21.4 6 258.0 110 3.08 3.215
18.7 8 360.0 175 3.15 3.440
18.1 6 225.0 105 2.76 3.460
14.3 8 360.0 245 3.21 3.570
24.4 4 146.7 62 3.69 3.190
22.8 4 140.8 95 3.92 3.150
19.2 6 167.6 123 3.92 3.440
17.8 6 167.6 123 3.92 3.440
16.4 8 275.8 180 3.07 4.070
17.3 8 275.8 180 3.07 3.730
15.2 8 275.8 180 3.07 3.780
10.4 8 472.0 205 2.93 5.250
10.4 8 460.0 215 3.00 5.424
14.7 8 440.0 230 3.23 5.345
32.4 4 78.7 66 4.08 2.200
30.4 4 75.7 52 4.93 1.615
33.9 4 71.1 65 4.22 1.835
21.5 4 120.1 97 3.70 2.465
15.5 8 318.0 150 2.76 3.520
15.2 8 304.0 150 3.15 3.435
13.3 8 350.0 245 3.73 3.840
19.2 8 400.0 175 3.08 3.845
27.3 4 79.0 66 4.08 1.935
26.0 4 120.3 91 4.43 2.140
30.4 4 95.1 113 3.77 1.513
15.8 8 351.0 264 4.22 3.170
19.7 6 145.0 175 3.62 2.770
15.0 8 301.0 335 3.54 3.570
21.4 4 121.0 109 4.11 2.780
21.0 6 160.0 110 3.90 2.620
21.0 6 160.0 110 3.90 2.875
22.8 4 108.0 93 3.85 2.320
21.4 6 258.0 110 3.08 3.215
18.7 8 360.0 175 3.15 3.440
18.1 6 225.0 105 2.76 3.460
14.3 8 360.0 245 3.21 3.570
24.4 4 146.7 62 3.69 3.190
22.8 4 140.8 95 3.92 3.150
19.2 6 167.6 123 3.92 3.440
17.8 6 167.6 123 3.92 3.440
16.4 8 275.8 180 3.07 4.070
17.3 8 275.8 180 3.07 3.730
15.2 8 275.8 180 3.07 3.780
10.4 8 472.0 205 2.93 5.250
10.4 8 460.0 215 3.00 5.424
14.7 8 440.0 230 3.23 5.345
32.4 4 78.7 66 4.08 2.200
30.4 4 75.7 52 4.93 1.615
33.9 4 71.1 65 4.22 1.835
21.5 4 120.1 97 3.70 2.465
15.5 8 318.0 150 2.76 3.520
15.2 8 304.0 150 3.15 3.435
13.3 8 350.0 245 3.73 3.840
19.2 8 400.0 175 3.08 3.845
27.3 4 79.0 66 4.08 1.935
26.0 4 120.3 91 4.43 2.140
30.4 4 95.1 113 3.77 1.513
15.8 8 351.0 264 4.22 3.170
19.7 6 145.0 175 3.62 2.770
15.0 8 301.0 335 3.54 3.570
21.4 4 121.0 109 4.11 2.780