I'm trying to render what I think should be a very simple table using kable()
within papaja()
. One cell in the table includes mathematical symbols, and I get an error message when trying to render the document to pdf.
# some data
subj <- c(1, 2, 3, 4, 5)
var1 <- c(1, 1, 2, 1, 2)
var2 <- c("hfg", "hrfg", "thflk", "plht", "sdrpv")
sum_sq_diffs <- c("Σ√(total))", NA, NA, NA, NA)
df <- data.frame(subj, var1, var2, sum_sq_diffs)
When I try to render this in papaja()
, the Σ and √ will not render. I have tried to re-code these using equation notation, e.g. $sum$ $sqrt$(total)
but I always get error messages:
! Missing { inserted. <to be read again> $ l.129 (Σ √ (total)) & NA & NA & NA...
I've also tried $\\sum$ $\\sqrt$(total)
in my code, and have changed the front matter to the following:
latex_engine: xelatex
header-includes:
- \usepackage{newunicodechar}
- \newunicodechar{Σ}{\ensuremath{\sum}}
- \newunicodechar{√}{\ensuremath{\sqrt}}
Note that the equation won't render properly when I view it as a dataframe either (though the original symbols do work in this case), it just shows the string as is specified in the code, with $$s all in place.
I would recommend using LaTeX math directly; the trick here is to use escape = FALSE
in kable()
and to ensure that the column names are valid LaTeX expressions (e.g., no _
). Consider the following example:
# some data
subj <- c(1, 2, 3, 4, 5)
var1 <- c(1, 1, 2, 1, 2)
var2 <- c("hfg", "hrfg", "thflk", "plht", "sdrpv")
sum_sq_diffs <- c("$\\sum\\sqrt\\text{total}$", NA, NA, NA, NA)
data.frame(subj, var1, var2, sum_sq_diffs) |>
knitr::kable(
col.names = c("Subject", "Var 1", "Var 2", "Sum of sqaured differences")
, escape = FALSE
)