【问题标题】:Latex Formulas or symbols in table cells using knitr and kableExtra in R-Markdown,在 R-Markdown 中使用 knitr 和 kableExtra 的表格单元格中的乳胶公式或符号,
【发布时间】:2018-08-31 04:08:37
【问题描述】:

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

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

导致:

现在我正在努力将符号或公式放入表格的实际单元格之一。有人可以举一个例子,在一个单元格中显示常规文本符号公式吗?最好在表格标题中相同,在列名之一中在表格的行名中一一对应,并带有一些编号的脚注,以引用其中之一中的信息单元格或标题或列或行名称,我渴望一个拥有一切的例子!非常感谢。

【问题讨论】:

    标签: r latex r-markdown symbols tabular


    【解决方案1】:

    当然:记下escape 传递给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)
    ```
    

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

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

    【讨论】:

    • 美丽。这绝对可以帮助很多在桌子上苦苦挣扎的人!
    • 亲爱的 Martin,您能否提供一个示例,其中列名(甚至标题上方的标题)为斜体、符号或公式?非常感谢,它将使此页面完整!
    • 完成。总是一样。只需在需要的地方添加原始 tex。
    • 尤伯盖尔!非常感谢!
    • 谢谢马丁!!这太有帮助了!我在表格方面遇到了很多困难,而且我在其他地方找到的其他信息都不适用于 PDF!
    猜你喜欢
    • 2018-10-24
    • 2018-08-24
    • 2018-10-11
    • 2017-07-18
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多