RMarkdown - 表中使用 kable 的不同字体类型? [英] RMarkdown - different font types in table using kable?

查看:50
本文介绍了RMarkdown - 表中使用 kable 的不同字体类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RMarkdown 生成 pdf 文档.是否可以使用 kable_styling 更改表格中的字体类型?如果没有,你能推荐任何其他的包吗?

I am using RMarkdown to produce pdf document. Is it possible to change font type in tables using kable_styling? If not, could you suggest any other package?

library(dplyr)
library(kableExtra)

kable(mtcars, align = "c", booktabs = TRUE) %>% 
  kable_styling(font_size = 12) %>% 
  row_spec(0, bold = T, color = "white", background = "gray")

推荐答案

这有点棘手,因为在 LaTeX 中更改字体很棘手.我没有 Segoe UI 字体(那是 Windows 字体,对吗?),但这里有一些对我有用的东西,在 MacOS 中更改了不同的字体.

This is somewhat tricky, because changing fonts in LaTeX is tricky. I don't have the Segoe UI font (that's a Windows font, right?), but here's something that works for me with a different font change in MacOS.

首先,您需要使用 xelatex LaTeX 引擎.(您可能可以使用 pdflatex 执行此操作,但命令会有所不同,我不知道它们.)

First, you need to use the xelatex LaTeX engine. (You can probably do this using pdflatex, but the commands would be different, and I don't know them.)

其次,你需要定义一个命令来切换到你想要的字体.在下面的代码中,我将其命名为 \comicfont 并将其设置为切换到 Comic Sans MS.

Second, you need to define a command to switch to the font you want. In the code below I called it \comicfont and set it to switch to Comic Sans MS.

第三和第四,您需要定义环境以生成此字体的表格.您需要两种环境,具体取决于您希望表格内联 (ctable) 还是浮动标题 (capctable).

Third and fourth, you need to define environments to produce tables in this font. You need two environments, depending on whether you want the table inline (ctable) or floating with a caption (capctable).

然后,当您希望表格采用新字体时,请将 table.envir 设置为适当环境的名称.它在 kable_styling() 中为内联表设置,在 kable 中为浮动表设置.这是一个对我有用的例子:

Then when you want your table in the new font, you set table.envir to the name of the appropriate environment. It gets set in kable_styling() for inline tables and in kable for floating tables. Here's an example that works for me:

---
title: 'Untitled'
output: 
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \newfontfamily\comicfont[Path=/Library/Fonts/]{Comic Sans MS}
  - \newenvironment{ctable}{\comicfont }{}
  - \newenvironment{capctable}[1][t]{\begin{table}[#1]\centering\comicfont}{\end{table}}
---

```{r}
library(knitr)
library(kableExtra)
kable(head(mtcars), booktabs=TRUE, align = "c") %>% 
  kable_styling(table.envir="ctable", font_size=12) %>%
  row_spec(0, bold = T, color = "white", background = "gray")
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")
```

这篇文章https://tex.stackexchange.com/a/63975 给出了一个 Windows 上的例子,它可能是很有帮助.

This post https://tex.stackexchange.com/a/63975 gives an example on Windows which might be helpful.

编辑添加:kable_stylingtable.envir 参数是一个非常新的添加;您应该确保安装了最新版本的 kableExtra.

Edited to add: the table.envir parameter to kable_styling is a pretty new addition; you should make sure you have the latest version of kableExtra installed.

这篇关于RMarkdown - 表中使用 kable 的不同字体类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆