在R-Markdown中使用knitr和kableExtra的表格单元格中的乳胶配方或符号, [英] Latex Formulas or symbols in table cells using knitr and kableExtra in R-Markdown,

查看:344
本文介绍了在R-Markdown中使用knitr和kableExtra的表格单元格中的乳胶配方或符号,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢jaySf,我能够创建一个pdf文件,其中包含带注脚的漂亮表格,这些注脚显示了带有R Markdown,Latex,knitrkableExtra的公式和符号(在他的示例下面):

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)
```

其结果是:

现在,我正在努力将符号或公式放在表的实际单元格之一中.有人可以举一个在一个单元格中显示常规文本符号公式的示例吗?最好 在表标题中也是一样的,在列名之一中,表的行名是一对一的,其中一些带编号的脚注指的是其中之一的信息.单元格,标题,列名或行名,我非常想拥有一个全部的示例!非常感谢.

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.

推荐答案

当然:请注意传递给kable的参数escape:

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)
```

另一个带有斜体列名和附加标题的示例:

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)
```

这篇关于在R-Markdown中使用knitr和kableExtra的表格单元格中的乳胶配方或符号,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆