Problem with Quarto & {tinytable} with one specific data set

When I render a Quarto file using {tinytable} with one specific data set it crashes. If I use {flextable] it renders.

There is no problem running the same code in a normal .r file.

Raw Code

library(tinytable)

dat1 <- structure(list(Miel = c("Dactylurina", "Plebeina", "HypoLiotrigona", 
"Meliponula_togoensis", "Meliponula_bocandei"), Nourriture = c(21L, 
3L, 55L, 38L, 33L), Sucrant = c(21L, 3L, 55L, 38L, 33L), Médicament = c(25L, 
4L, 40L, 49L, 32L), Valeurs.spirituelles = c(1L, 1L, 3L, 1L, 
32L), Rituels = c(1L, 0L, 2L, 1L, 0L), Usuel = c(0L, 0L, 0L, 
0L, 1L)), row.names = c(NA, -5L), class = "data.frame")

tt(dat1)

Quarto Code

---
title: "Blasted Table"
author: "jrkrideau"
date: today
date-format: iso
number-sections: false
format: pdf
header-includes:
  - \usepackage{lipsum}
  - \usepackage{siunitx}
  - \usepackage{float}
  - \floatplacement{table}{H}
  - \usepackage{fancyhdr}
  - \pagestyle{fancy} 
---

It was a dark and stormy night.

```{r  library}
#| label: library
#| echo: false

library(tinytable)
```


```{r data}
#| label: data
#| echo: false

dat1 <- structure(list(Miel = c("Dactylurina", "Plebeina", "HypoLiotrigona", 
"Meliponula_togoensis", "Meliponula_bocandei"), Nourriture = c(21L, 
3L, 55L, 38L, 33L), Sucrant = c(21L, 3L, 55L, 38L, 33L), Médicament = c(25L, 
4L, 40L, 49L, 32L), Valeurs.spirituelles = c(1L, 1L, 3L, 1L, 
32L), Rituels = c(1L, 0L, 2L, 1L, 0L), Usuel = c(0L, 0L, 0L, 
0L, 1L)), row.names = c(NA, -5L), class = "data.frame")

dat2 <- data.table(aa = 1:5, bb = 5:1)
```

```{r  plot}
#| label: plot
#| echo: false

tt(dat1)

```

Usually, it is helpful to share how it crashes. Any message or log can help understand. Here I can reproduce and I get:

ERROR: 
compilation failed- error
Missing $ inserted.
<inserted text> 
                $
l.196 \end
          {tblr} 

You may need to $ $ around an expression in this file.

So this is something with how tinytable is outputing LaTeX code in Quarto.

Usually this could come from misformatted LaTeX Code. you can use keep-md: true and keep-tex: true in config to get the intermediate content.

First thing to check is the presence of special LaTeX that would need escaping. Like _ which you get in your Miel column.

When outputting to LaTeX, you need to take care of those escaping, especially if the tool you use to produce the table is not doing that for you.

For example, here you need to escape the underscores.

dat1 <- structure(list(Miel = c("Dactylurina", "Plebeina", "HypoLiotrigona", 
"Meliponula\\_togoensis", "Meliponula\\_bocandei"), Nourriture = c(21L, 
3L, 55L, 38L, 33L), Sucrant = c(21L, 3L, 55L, 38L, 33L), Médicament = c(25L, 
4L, 40L, 49L, 32L), Valeurs.spirituelles = c(1L, 1L, 3L, 1L, 
32L), Rituels = c(1L, 0L, 2L, 1L, 0L), Usuel = c(0L, 0L, 0L, 
0L, 1L)), row.names = c(NA, -5L), class = "data.frame")

look at the \\_ so that \_ is in the .tex and not _ only which is a special LaTeX character.

Though usually the tool you are using provides this for you. This is here in the doc: format – tinytable

```{r  plot}
#| label: plot
#| echo: false

tt(dat1) |> format_tt(escape = TRUE)

```

I hope this will help you know what to look for next time you encounter an issue.

Hope it helps.

Ah thank you. I tried escaping several things but it never occurred to me I needed to escape something in a name of an element. I'm rather embarrassed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.