table 单元格中的 Latex 公式或符号在 R-Markdown 中使用 knitr 和 kableExtra,
Latex Formulas or symbols in table cells using knitr and kableExtra in R-Markdown,
感谢 jaySf,我能够创建一个包含漂亮 tables 的 pdf 文件,其中脚注显示公式和符号以及 R Markdown、Latex、knitr
和 kableExtra
(在他的下方例子):
---
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)
```
这导致:
现在我正在努力将符号或公式放入 table 的实际单元格之一。有人可以举一个例子,在一个单元格中显示常规文本 and 符号 and 公式吗?最好也在table标题中相同,在其中一个列名和中一个[=32的行名=] 以及一些编号的脚注引用其中一个单元格或标题或列或行名称中的信息,我非常想要一个包含所有内容的示例!非常感谢。
当然:记下传递给 kable
的参数 escape
:
---
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)
```
另一个带有斜体列名称和附加 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)
```
感谢 jaySf,我能够创建一个包含漂亮 tables 的 pdf 文件,其中脚注显示公式和符号以及 R Markdown、Latex、knitr
和 kableExtra
(在他的下方例子):
---
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)
```
这导致:
现在我正在努力将符号或公式放入 table 的实际单元格之一。有人可以举一个例子,在一个单元格中显示常规文本 and 符号 and 公式吗?最好也在table标题中相同,在其中一个列名和中一个[=32的行名=] 以及一些编号的脚注引用其中一个单元格或标题或列或行名称中的信息,我非常想要一个包含所有内容的示例!非常感谢。
当然:记下传递给 kable
的参数 escape
:
---
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)
```
另一个带有斜体列名称和附加 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)
```