【问题标题】:LaTex code inside a kable table in an rmarkdown::beamer_presentationrmarkdown::beamer_presentation 中 kable 表中的 LaTex 代码
【发布时间】:2021-05-23 07:45:28
【问题描述】:

在使用bookdown 编制的较大报告中,我使用了几个kableExtra 表格,其中包括LaTex commands(例如,添加斜体字、项目符号以创建列表,以及在表格中手动添加脚注)。

当我复制使用rmarkdown::beamer_presentation 生成的LaTex beamer presentation 中的表时,不幸的是编译失败了。

如何扭曲kableExtra 表以包含LaTex commands

MWE

---
title: "MWE"
subtitle: "Beamer presentation with R-markdown"
author: "Donald Duck"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    theme: "THEMENAME"
    latex_engine: xelatex
    toc: false
    slide_level: 2
---

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='66%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \\emph{special foo}$^{a}$", 
    "bar and \n $\\boldsymbol{\\cdot}$ \\emph{random bar}$^{a}$\n $\\boldsymbol{\\cdot}$ \\emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    # escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
    caption = "Caption Table with LaTex" # short caption for LoT
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  column_spec(1, width = "11em") %>% # fix width column 1
  column_spec(2, width = "27em") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->

【问题讨论】:

  • escape = FALSE 被禁用了吗?
  • @bttomio:以前在bookdown 中没有被禁用。但是对于 beamer 必须禁用它 - 否则将无法编译。

标签: r latex r-markdown beamer xelatex


【解决方案1】:

必须为kableExtra 表加载几个LaTex 包,这样LaTex 代码才能很好地包含在其中。

要固定列宽,最好在cm 中指定column_spec(1, width = "3cm") 而不是em(对于之前的规范,表格超出了幻灯片)。

---
title: "LaTex code inside a kable table in an rmarkdown::beamer_presentation"
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    latex_engine: xelatex
    toc: false
    slide_level: 2
header-includes:
  - \usepackage{booktabs} 
  - \usepackage{longtable} 
  - \usepackage{array}          
  - \usepackage{multirow}   
  - \usepackage{wrapfig}    
  - \usepackage{float}
  - \usepackage{colortbl}    
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable} 
  - \usepackage{threeparttablex}
  - \usepackage[normalem]{ulem}
  - \usepackage{makecell}
  # - \usepackage[usenames,dvipsnames]{xcolor} # clashes, as loaded by markdown anyway
---

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='30%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \\emph{special foo}$^{a}$", 
    "bar and \n $\\boldsymbol{\\cdot}$ \\emph{random bar}$^{a}$\n $\\boldsymbol{\\cdot}$ \\emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
    caption = "Caption Table with LaTex" # short caption for LoT
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  kable_styling( # for wide tables: scale them down to fit
    full_width = F,
    latex_options = c(
      "scale_down"
    )) %>% 
  column_spec(1, width = "3cm") %>% # fix width column 1
  column_spec(2, width = "5cm") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 2017-07-07
    • 2020-11-30
    • 1970-01-01
    • 2021-05-19
    • 2021-08-10
    • 2021-05-23
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多