Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex math expression in RMarkdown table column name

I want to display a table in my markdown document and set the column names to be Latex mathematical formulas such as $\dot(m)_1$.

I tried this:

knitr::kable(my.df[, c("Time", "MassFlowRate")],
             row.names = FALSE,
             col.names = c("Time", "$\dot{m}_1$"))

But it doesn't work.

I don't generate a PDF, but a Word document in the end. So directly coding a Latex table is not an option.

like image 446
Ben Avatar asked Feb 20 '17 15:02

Ben


2 Answers

You need to escape \ passed into R code so \dots should be \\dots:

```{r}
my.df <- data.frame(Time=rnorm(10), MassFlowRate = rnorm(10))
knitr::kable(my.df[, c("Time", "MassFlowRate")],
             row.names = FALSE,
             col.names = c("Time", "$\\dot{m}_1$"))
```
like image 111
scoa Avatar answered Oct 15 '22 14:10

scoa


To get the current answer to work, you need to add , escape = FALSE in kable().

Also, I prefer using a tibble to a data.frame. That way I can specify the LaTeX code in the column names directly. No need to use col.names.

```{r}
library(tibble)
my_df <- tibble(Time=rnorm(10), `$\\dot{m}_1$` = rnorm(10))
knitr::kable(my_df, escape = FALSE)
```
like image 34
lowndrul Avatar answered Oct 15 '22 14:10

lowndrul