Themes

tinytable offers a very flexible theming framwork, which includes a few basic visual looks, as well as other functions to apply collections of transformations to tinytable objects in a repeatable way. These themes can be applied by supplying a string or function to the theme argument in tt(). Alternatively, users can call the specific theme functions like theme_striped(), theme_grid(), etc.

The main difference between theme functions and the other options in package, is that whereas style_tt() and format_tt() aim to be output agnostic, theme functions supply transformations that can be output-specific, and which can have their own sets of distinct arguments. See below for a few examples.

library(tinytable)
options(tinytable_tt_digits = 3)
options(tinytable_latex_placement = "H")
x <- mtcars[1:4, 1:5]

Visual themes

To begin, let’s explore a few of the basic looks supplied by themes:

tt(x, theme = "striped")
mpg cyl disp hp drat
21 6 160 110 3.9
21 6 160 110 3.9
22.8 4 108 93 3.85
21.4 6 258 110 3.08
tt(x) |> theme_striped()
mpg cyl disp hp drat
21 6 160 110 3.9
21 6 160 110 3.9
22.8 4 108 93 3.85
21.4 6 258 110 3.08
tt(x, theme = "grid")
mpg cyl disp hp drat
21 6 160 110 3.9
21 6 160 110 3.9
22.8 4 108 93 3.85
21.4 6 258 110 3.08
tt(x, theme = "empty")
mpg cyl disp hp drat
21 6 160 110 3.9
21 6 160 110 3.9
22.8 4 108 93 3.85
21.4 6 258 110 3.08

Custom themes

Users can also define their own themes to apply consistent visual tweaks to tables. For example, this defines a themeing function and sets a global option to apply it to all tables consistently:1

theme_vincent <- function(x, ...) {
  out <- x |> 
    style_tt(color = "teal") |>
    theme_default()
  out@caption <- "Always use the same caption."
  out@width <- .5
  return(out)
}

options(tinytable_tt_theme = theme_vincent)

tt(mtcars[1:2, 1:2])
Always use the same caption.
mpg cyl
21 6
21 6
tt(mtcars[1:3, 1:3])
Always use the same caption.
mpg cyl disp
21 6 160
21 6 160
22.8 4 108
options(tinytable_tt_theme = NULL)

Here is a slightly more complex example. The benefit of this approach is that we apply a function via the style_tt() function and its finalize argument, so we can leverage some of the object components that are only available at the printing stage:

theme_slides <- function(x, ...) {
  fn <- function(table) {
    if (isTRUE(table@output == "typst")) {
      table@table_string <- paste0("#figure([\n", table@table_string, "\n])")
    }
    return(table)
  }
  x <- style_tt(x, finalize = fn)
  return(x)
}

tt(head(iris), theme = theme_slides)

Note: the code above is not evaluated because it only applies to Typst output.

Tabular (LaTeX and HTML)

The tabular theme is designed to provide a more “raw” table, without a floating table environment in LaTeX, and without CSS or Javascript in HTML.

tt(x) |>
  theme_latex(environment = "tabular", table = FALSE) |>
  print("latex")
\begin{table}[H]
\centering
\begin{tabular}{lllll}
mpg & cyl & disp & hp & drat \\
21 & 6 & 160 & 110 & 3.9 \\
21 & 6 & 160 & 110 & 3.9 \\
22.8 & 4 & 108 & 93 & 3.85 \\
21.4 & 6 & 258 & 110 & 3.08 \\
\end{tabular}
\end{table} 

Combining themes

Themes are just functions that apply a set of transformations to a tinytable object. This means that users can combine themes to create new looks. For example, when we call tt(x) without specifying the theme argument, the theme_default() is applied automatically.

x <- head(iris, 3)
tt(x)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa

If we add a call to theme_striped(), we add grey background stripes, but keep the other default stylings (ex: top and bottom horizontal rules).

tt(x) |> theme_striped()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa

Alternatively, we could use theme_empty() to remove the default theme, and theme_striped() to get a very minimal look with just the stripes.

tt(x) |> theme_empty() |> theme_striped()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa

Or use the theme argument to get the same effect.

tt(x, theme = "empty") |> theme_striped()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa

User-written themes

This section provides a few user-written themes that can be used to extend the functionality of tinytable. These themes are not included in the package by default, but they can be easily added to your workflow. If you would like your own custom theme to appear here, please open an issue on the tinytable GitHub repository or submit a pull request.

theme_mitex()

This theme was written by Kazuharu Yanagimoto. Thanks for your contribution!

The MiTeX project aims to bring LaTeX support to Typst documents. This theme replace every instance of matching pairs of dollars signs $..$ by a MiTeX function call: #mitex(...). This allows you to use LaTeX math in Typst documents.

Warning: The substitution code is very simple and it may not work properly when there are unmatched $ symbols in the document.

theme_mitex <- function(x, ...) {
    fn <- function(table) {
        if (isTRUE(table@output == "typst")) {
          table@table_string <- gsub(
            "\\$(.*?)\\$",
            "#mitex(`\\1`)",
            table@table_string)
        }
        return(table)
    }
    x <- style_tt(x, finalize = fn)
    return(x)
}

Footnotes

  1. Note: Captions must be defined in Quarto chunks for Typst output, which explains why they are not displayed in the Typst version of this document.↩︎