【问题标题】:kable unable to output unicode character in pdf_book (bookdown)kable 无法在 pdf_book (bookdown) 中输出 unicode 字符
【发布时间】:2020-11-28 11:02:53
【问题描述】:

我正在尝试在 bookdown 项目中 kable 呈现的表格中使用复选标记 unicode 字符 (\u2713)。一个 MWE 由以下 2 个文件(index.Rmd、preamble.tex)组成,是生成的最小 bookdown 项目的一部分。

index.Rmd

    --- 
    title: "A Minimal Book Example"
    subtitle: "subtitle"
    lang: fr
    author: "Yihui Xie"
    date: "`r Sys.Date()`"
    site: bookdown::bookdown_site
    documentclass: book
    mainfont: Arial
    mathfont: Arial
    bibliography: [book.bib, packages.bib]
    biblio-style: apalike
    link-citations: yes
    subparagraph: yes
    description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
    ---
    
    ```{r setup, include=FALSE}
    # Set locale to french for month and day names (calendar plots)
    Sys.setlocale(locale = 'French')
    
    # Load all needed libraries
    if(!require('pacman'))install.packages('pacman')
    pacman::p_load(kableExtra)
    ```
    
    (ref:O3) O~3~
    
    # Prerequisites
    
    ✓
    
    ```{r stations-polluants, echo=FALSE}
    stations_polluants_table <- data.frame(
      stations = c("41B001", "41B004", "41B011", "41MEU1", "41N043", "41R001", "41R002", "41R012", "41WOL1", "Total / polluant"),
      O3   = c("", rep("✓", 5), "", rep("\u2713", 2), "7")
    )
    kable(stations_polluants_table, col.names = c("Station", "(ref:O3)"), booktabs = T, align = "c", caption = "Caption text") %>%
      row_spec(0, bold = T) %>%
      row_spec(10, bold = T) %>%
      collapse_rows(columns = 1, latex_hline = "major", valign = "top")
    ```
    
    
    ```{r include=FALSE}
    # automatically create a bib database for R packages
    knitr::write_bib(c(
      .packages(), 'bookdown', 'knitr', 'rmarkdown'
    ), 'packages.bib')
    ```

preamble.tex

\usepackage{booktabs}

\newfontfamily{\unicodefont}{Arial Unicode MS}
\usepackage{newunicodechar}
\newunicodechar{✓}{{\unicodefont{✓}}}

通过使用rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8') 编译PDF,可以注意到✓ 在文本中正确呈现,而在生成的表格中它被替换为。我还尝试在表格中使用\u2713,但没有取得更多成功。

我做错了什么?请注意,即使首选输出是 PDF,我也想编译为 gitbook(因此 Rmd 文件需要保持独立于输出格式)。

非常感谢。

【问题讨论】:

    标签: pdf unicode bookdown kable kableextra


    【解决方案1】:

    在明确了片刻之后,解决方案只是在 bookdown 中使用文本引用,即(ref:check) ✓ 并使用表格中的引用。

    index.Rmd

        ```{r setup, include=FALSE}
        # Set locale to french for month and day names (calendar plots)
        Sys.setlocale(locale = 'French')
        
        # Load all needed libraries
        if(!require('pacman'))install.packages('pacman')
        pacman::p_load(kableExtra)
        ```
        
        (ref:O3) O~3~
        
        # Prerequisites
        
        ✓
    
        (ref:check) ✓
        
        ```{r stations-polluants, echo=FALSE}
        stations_polluants_table <- data.frame(
          stations = c("41B001", "41B004", "41B011", "41MEU1", "41N043", "41R001", "41R002", "41R012", "41WOL1", "Total / polluant"),
          O3   = c("", rep("(ref:check)", 5), "", rep("(ref:check)", 2), "7")
        )
        kable(stations_polluants_table, col.names = c("Station", "(ref:O3)"), booktabs = T, align = "c", caption = "Caption text") %>%
          row_spec(0, bold = T) %>%
          row_spec(10, bold = T) %>%
          collapse_rows(columns = 1, latex_hline = "major", valign = "top")
        ```
        
        
        ```{r include=FALSE}
        # automatically create a bib database for R packages
        knitr::write_bib(c(
          .packages(), 'bookdown', 'knitr', 'rmarkdown'
        ), 'packages.bib')
        ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 2023-02-01
      • 2018-09-09
      • 2020-08-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多