Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex Formulas or symbols in table cells using knitr and kableExtra in R-Markdown,

Thanks to jaySf I was able to create a pdf file containing beautiful tables with footnotes showing formulas and symbols with R Markdown, Latex, knitr and kableExtra (below his example):

---
title: "Untitled"
output: pdf_document
---

```{r tab}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33), 
             row.names=LETTERS[1:6])
kable(df, "latex", align="c", booktabs=TRUE) %>%
footnote(general=c("$a^2+b^2=c^2,$",     
                   "$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$", 
                   "1,000 \\\\$;", "100\\\\%."),
         number=c("Hello\ there! \\\\textit{Hello\ there!}"),
         footnote_as_chunk=TRUE, 
         escape=FALSE)
```

which results in:

enter image description here

Now I'm struggling with putting symbols or formulas within one of the actual cells of the table. Could someone make an example which shows regular text and symbols and formulas within one cell? Preferably also the same in table title, in one of the column names and one in one the row names of the table and with some numbered footnotes referring to information in one of the cells or title or col or row names, I'm dying for an example which has it all! Many many thanks.

like image 430
Benjamin Telkamp Avatar asked Mar 21 '18 21:03

Benjamin Telkamp


People also ask

How do you use knitr in R?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

Can you insert a table into R markdown?

To use it, open a Rmd or R document and select “Addins –> Insert Table”.


1 Answers

Sure: take note of the argument escape passed to kable:

---
title: "Untitled"
output: pdf_document
---

```{r tab}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33), 
             row.names=LETTERS[1:6])

df$v4 <- c('My formula $\\sum_{i=1}^9$')
kable(df, "latex", align="c", booktabs=TRUE, escape = F, caption = '$\\Gamma$') %>%
footnote(general=c("$a^2+b^2=c^2,$",     
                   "$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$", 
                   "1,000 \\\\$;", "100\\\\%."),
         number=c("Hello\ there! \\\\textit{Hello\ there!}"),
         footnote_as_chunk=TRUE, 
         escape=FALSE)
```

enter image description here

Another example with italic column names and an additional header:

```{r, echo = F}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33), 
             row.names=LETTERS[1:6])
df$v4 <- c('My formula $\\sum_{i=1}^9$')

# italic column headers
colnames(df) <- paste0("\\textit{", colnames(df),"}")

kable(df, "latex", align="c", booktabs=TRUE, escape = F, caption = '$\\Gamma$') %>%
footnote(general=c("$a^2+b^2=c^2,$",     
                   "$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$", 
                   "1,000 \\\\$;", "100\\\\%."),
         number=c("Hello\ there! \\\\textit{Hello\ there!}"),
         footnote_as_chunk=TRUE, 
         escape=FALSE) %>%
  add_header_above(header = c("\\\\textbf{Results} $\\\\Delta$" = 5), escape = F)
```
like image 113
Martin Schmelzer Avatar answered Oct 09 '22 04:10

Martin Schmelzer