0

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!

rdatasculptor
  • 8,112
  • 14
  • 56
  • 81
  • Does this answer your question? [How to create table in rmarkdown using greek letters in column headers?](https://stackoverflow.com/questions/50046055/how-to-create-table-in-rmarkdown-using-greek-letters-in-column-headers) – bttomio Mar 24 '21 at 09:23
  • @bttomio i found the same solution, but it didn't work for my. unfortenately – rdatasculptor Mar 24 '21 at 10:22

1 Answers1

2

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

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • Interesting approach! But what if I don't want an extra header row? So the alpha symbol on the place of A – rdatasculptor Mar 24 '21 at 14:45
  • I edited my answer to also show alpha in the place of A. Thanks. – bttomio Mar 24 '21 at 20:50
  • 1
    yess, it works... in your code, not in mine. Since I didn't give a reproducible example, I will accept this answer. I will do more research in my own code. – rdatasculptor Mar 25 '21 at 06:53
  • 1
    It may be related to the set up of the column names before the `kbl` function, e.g. in a `data.frame` object. – bttomio Mar 25 '21 at 07:35
  • yes, that should be the case! Thank you for pointing me in that direction. Now I am trying to find a way to solve that. – rdatasculptor Mar 25 '21 at 07:44
  • well, it is very strange, my code doesn't recognize the $ signs as having LaTeX code in between them. – rdatasculptor Mar 25 '21 at 08:07
  • Leave the column names as the last step, just before the `kbl` function. This might fix it. – bttomio Mar 25 '21 at 08:28
  • the data frames I use already have names, obviously, names like `$\\alpha$`. If remove the names and add them later, the result is still the same. I will try to make a reproducible example. – rdatasculptor Mar 25 '21 at 10:47
  • Are they between quotes (i.e. `"$\\alpha$"`)? – bttomio Mar 25 '21 at 10:52
  • if I add `format = "pandoc"` instead of `"latex"`the LaTeX coded column names are properly rendered, but then the rest of the `kbl` parameters don't do anything. It keeps me wondering.... – rdatasculptor Mar 25 '21 at 11:04
  • I see. It's hard to guess. Maybe the error message is coming from somewhere else. – bttomio Mar 25 '21 at 12:09