library(tinytable)
options(tinytable_tt_digits = 3)
options(tinytable_theme_placement_latex_float = "H")
<- mtcars[1:4, 1:5] x
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 theme_tt()
function.
The main difference between theme_tt()
and the other options in package, is that whereas style_tt()
and format_tt()
aim to be output agnostic, theme_tt()
supplies transformations that can be output-specific, and which can have their own sets of distinct arguments. See below for a few examples.
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_tt("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 = "bootstrap")
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 = "spacing")
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:
<- function(x, ...) {
theme_vincent <- x |>
out style_tt(color = "teal")
@caption <- "Always use the same caption."
out@width <- .5
outreturn(out)
}
options(tinytable_tt_theme = theme_vincent)
tt(mtcars[1:2, 1:2])
mpg | cyl |
---|---|
21 | 6 |
21 | 6 |
tt(mtcars[1:3, 1:3])
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:
<- function(x, ...) {
theme_slides <- function(table) {
fn if (isTRUE(table@output == "typst")) {
@table_string <- paste0("#figure([\n", table@table_string, "\n])")
table
}return(table)
}<- style_tt(x, finalize = fn)
x return(x)
}
tt(head(iris), theme = theme_slides)
Tabular
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_tt("tabular") |>
print("latex")
\begin{tabular}{lllll}
\hline
mpg & cyl & disp & hp & drat \\ \hline
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 \\
\hline
\end{tabular}
Resize
LaTeX only.
Placement
LaTeX only.
Multipage
LaTeX only.
User-written themes
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.
<- function(x, ...) {
theme_mitex <- function(table) {
fn if (isTRUE(table@output == "typst")) {
@table_string <- gsub(
table"\\$(.*?)\\$",
"#mitex(`\\1`)",
@table_string)
table
}return(table)
}<- style_tt(x, finalize = fn)
x return(x)
}