微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

rmarkdown::beamer_presentation

如何解决rmarkdown::beamer_presentation

在使用 bookdown 编译的较大报告中,我使用了几个包含 kableExtraLaTex commands 表(例如,添加斜体、项目符号以创建列表,以及在表)。

当我复制使用 LaTex beamer presentation 生成rmarkdown::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 -->

解决方法

必须为 LaTex 表加载几个 kableExtra 包,以便在其中包含 LaTex 代码。

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

tableLaTex

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。