Render symbols from table with kable

I have a csv I'm importing into markdown (ultimately rendering to pdf), and in one of the cells we have the appropriate latex terminology for creating a symbol when rendered. It works just fine when it's in the normal markdown text, but it will not create the same symbol when inside the table.

---
title: "Test"
output: pdf_document
---

``` {r} 
dat <- data.frame(variable = c(rep("turgor loss point", 10), rep("volume", 10)),
                    unit = c(rep("$\\pi_{tlp}$", 10), rep("cm^3", 10)),
                    value = c(round(runif(10), 2), round(runif(10,20,30),2)))

kableExtra::kable(dat, booktabs = TRUE, format = 'latex')

Printing in the table leaves the raw symbology, but if I want to discuss the use of the volume (cm^3) and the turgor loss point (\pi_{tlp}) in the text itself, the symbols render without a problem.
```
I believe the issue lies with Latex directly reading the text alone, but not when it's within a separate format like the data frame. Thus, does anyone have an idea for how to get the symbols to appear within the table itself when rendered?

Here is a snip of the output pdf

Hi @envian

Adding the escape = FALSE argument to your code should fix the issue:

kableExtra::kable(dat, booktabs = TRUE, format = 'latex', escape = FALSE)

You also need to wrap the "cm^3" in dollar signs like "$cm^3$" for it to render properly.

1 Like

Hi @mattwarkentin,

Thanks, that works! My problem now is that I'm getting a new error that's tied to latex again. The pdf won't render due to this error:

! Misplaced \noalign.
<recently read> \noalign 

Error: Failed to compile climate_sensitivity_draft.tex.

I've looked up solutions online but haven't found any that address the issue. In our document, we have the following latex commands (in this order, but not one right after the other). I'm not sure if this is should be another question (I'm not sure how to ask this without including the full document formatting).

\raggedright
\newpage
\newpage
\clearpage
\raggedright 

Hmm, I'm not sure off the top of my head what the issue could be without seeing more of your document.

Could you try and create a minimal version of the document that still produces this issue and share the code here? Or upload the file and I can try and reproduce the issue?

Sure thing. I looked at the .Rmd file again and noticed the problem is stemming from one section, which I believe is this chunk (the one represented by the reprex I put earlier). The table is here as a jpg (if it helps I can make it as a normal df but I'm assuming there's something involved with just how this chunk is written).


title: "Test"
output: pdf_document

``` {r Table 2, eval = TRUE, echo=FALSE, warning=FALSE}
library(knitr)
library(kableExtra)
table2 <- read.csv(table, stringsAsFactors = FALSE, check.names = FALSE)

kable(table2, booktabs = TRUE, caption = "**Table 2. Summary of variables**", format = 'latex', escape = FALSE) %>%
  kable_styling(latex_options = c("scale_down", "hold_position"), font_size = 12, protect_latex = TRUE) %>%
  add_header_above(c(" "," "," "," "," "," ", "observed values" = 3, " ")) %>%
  column_spec(4, width = "7cm") %>%
  pack_rows("Dependent variable", 1, 1, latex_gap_space = "1em", colnum = 1, hline_before = FALSE) %>%
  pack_rows("Independent variables", 2, 18, latex_gap_space = ".4em", colnum = 1, hline_before = FALSE) %>%
  pack_rows("tree size", 5, 6, latex_gap_space = ".4em", colnum = 1, hline_before = FALSE, bold = FALSE, italic = TRUE) %>%
  pack_rows("microhabitat", 7, 11, latex_gap_space = ".4em", colnum = 1, hline_before = FALSE, bold = FALSE, italic = TRUE) %>%
  pack_rows("species' traits", 12, 18, latex_gap_space = ".4em", colnum = 1, hline_before = FALSE, bold = FALSE, italic = TRUE) %>%
  kable_styling()

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