rdatasculptor
rdatasculptor

Reputation: 8447

How to let bookdown render LaTeX in kableExtra table headers properly?

I use kableExtra package in rmarkdown (bookdown) to generate nice looking tables in pdf outputs. All works well except for the rendering of LaTeX code in headers. A header named like $\\alpha$ isn't rendered as the Greek alpha. The result is just a $\alpha$ shown in the pdf document.

Additional information: I use format = "latex" and escape = TRUE. If I use escape = FALSE, I get an error when rendering the document:

I was unable to find any missing LaTeX packages from the error log _main.log.
! Misplaced \noalign.
\cmidrule ->\noalign 
                     {\ifnum 0=`}\fi \@ifnextchar [{\@cmidrule }{\@cmidrule ...
l.1293 \cmidrule
                {3-7} 

I am sorry for not giving a reproducible example. I somehow hope it is a setting I missed somewhere in the kableExtra. If it is needed I will make an example though.

Many thanks in advance!

Upvotes: 0

Views: 905

Answers (1)

bttomio
bttomio

Reputation: 2306

You could try this:

---
title: "Use slashes to escape"
author: "bttomio"
date: "3/24/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r echo=F}
library(kableExtra)
x <- matrix(1:4, ncol=2)
kbl(x, col.names=c('$\\alpha$', 'B'), align = 'c', 'latex', booktabs = T, escape = F) %>%
    add_header_above(c("$\\\\alpha$" = 2), escape = F)
```

-output

enter image description here

Upvotes: 2

Related Questions