在 table 值中包含特殊字符的 RMarkdown 中将 kable 编织成 PDF

Knit a kable to PDF in RMarkdown that includes special characters in the table values

我正在尝试格式化包含 permyriad 符号“‱”的 kable。 Permyriad 表示每 10,000 个中有 1 个,所以 1‱ = 0.01%。

我可以让它与特殊字符 σ 一起工作,如下面的屏幕截图和代码所示。正在寻找将“σ”替换为“‱”的方法。

我很确定 what_should_this_beshould_i_escape_or_notid_like_to_use_booktabs 这三个变量的神奇组合可以解决问题。

我正在使用 tinytex 包在 RStudio 中执行此操作。

以下是我到目前为止所做的尝试:

  1. 变量 what_should_this_be 的确切值导致编织最终 pdf 中的 ‹ 符号。 “‱”的 Unicode 值是 U+2031。

    我试过的值:

    • "\textperthousand" 的组合,具有不同数量的转义符,有和没有括号,有和没有打开和关闭 $
    • 直接复制粘贴 符号,转义次数不等
    • "\U2031" 有不同数量的转义
  2. should_i_escape_or_not设置为TRUEFALSE的各种组合。

  3. 我想使用书签...但这可能要求有点高,所以我尝试了各种组合设置 id_like_to_use_booktabsTRUEFALSE.

  4. 在 RStudio > 工具 > Sweave 中设置“将 LaTeX 排版为 PDF 使用:”选项的各种组合

    ```{r, echo = FALSE}

    library(magrittr)

    what_should_this_be     <- "$\sigma$"
    should_escape_or_not    <- FALSE
    id_like_to_use_booktabs <- TRUE

    knitr::kable(
      head(mtcars) %>%
        dplyr::select(mpg) %>%
        tibble::rownames_to_column("car") %>%
        dplyr::mutate(mpg = paste0(mpg, what_should_this_be)),
      align     = "cc",
      escape    = should_escape_or_not,
      booktabs  = id_like_to_use_booktabs,
      caption   = "Works with character $\sigma$, but what about permyriad?"
    )

    ```

可以使用 textcomp 包和 \textpertenthousand

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{textcomp}
---

```{r, echo = FALSE}
  library(magrittr)
  what_should_this_be     <- "\textpertenthousand"

  knitr::kable(
    head(mtcars) %>%
      dplyr::select(mpg) %>%
      tibble::rownames_to_column("car") %>%
      dplyr::mutate(mpg = paste0(mpg, what_should_this_be)),
    align     = "cc",
    escape    = F,
    booktabs  = T,
    caption   = "Works with character $\sigma$, but what about permyriad?"
  )
```