RMarkdown-使用kable或huxtable的表格中的不同字体类型,输出为pdf?

时间:2019-03-21 12:13:16

标签: fonts r-markdown kable kableextra

使用huxtable,将不同单元格/行的不同字体输出到html很容易。 pdf并不是很多。 这并不是一个新问题,而是特定版本的 RMarkdown - different font types in table using kable?Change font of Kable in Rmarkdown pdf

我已使用https://stackoverflow.com/a/54735453/4927395给出的答案在rmarkdown下的图像中(在Windows PC上)创建输出。请注意,“环境”代码将更改表(整个表)的字体,但是块后的文本采用为表指定的字体。 建议解决此问题? 另外,我无法在计算机上使用浮动示例,因此将其注释掉了。 我喜欢huxtable,但是还没有看到为表格选择的字体示例(与主要字体不同)在网络上工作。如有必要,可以开放探索其他表包。

    ---
title: "Reprex selecting font for kable table output to pdf"
output: 
  pdf_document:
    latex_engine: xelatex
header-includes:
  \usepackage{fontspec}
  \setmainfont[Path=C:/windows/fonts/]{SHOWG.TTF}
  \newfontfamily\arialfont[Path=c:/windows/fonts/]{ARIAL}
  \newenvironment{ctable}{\arialfont }{}
  \newenvironment{capctable}[1][t]{\begin{table}[#1]\centering\arialfont}{\end{table}}
---

here is some text

```{r}
library(knitr)
library(kableExtra)
#This works, though leaves the selected font active for text after the chunk
kable(head(mtcars), booktabs=TRUE, align = "c") %>% 
   kable_styling(table.envir="ctable", font_size=12) %>%
   row_spec(0, bold = T, color = "white", background = "gray")
#This next bit doesn't work
#kable(head(mtcars), booktabs=TRUE, align = "c", 
#       caption = "This table floats", table.envir = "capctable") %>% 
#   kable_styling(font_size=12) %>%
#   row_spec(0, bold = T, color = "white", background = "gray")
```


here is some more text

output

1 个答案:

答案 0 :(得分:1)

实际上,这是在huxtable中执行此操作的方法(我是程序包的所有者)。您将需要安装xelatex和LaTeX“ fontspec”软件包。您还需要huxtable 4.4.0或更高版本,当前可在github上找到:

install_github("hughjonesd/huxtable")

在您的rmarkdown标头中:

output:
   pdf_document:
     latex_engine: xelatex

在R代码块中:

library(dplyr)
library(huxtable)
options(huxtable.latex_use_fontspec = TRUE)

mtcars %>%
      head() %>%
      as_huxtable() %>% 
      set_font("Times New Roman")

相关问题