简体   繁体   中英

kable unable to output unicode character in pdf_book (bookdown)

I am trying to use the checkmark unicode character ( \✓ ) in a table rendered by kable in a bookdown project. A MWE consists in the following 2 files (index.Rmd, preamble.tex) part of the generated minimal bookdown project.

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{✓}}}

By compiling in PDF with rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8') one can notice that ✓ is correctly rendered in the text, while it is replaced by <U+2713> in the generated table. I also tried to use \✓ in the table without more success.

What am I doing wrong ? Please note that, even if the prefered output is PDF, I would also like to compile as gitbook (so Rmd files need to stay independent from the output format).

Many thanks.

After having a moment of clarity, the solution simply lies in using text references in bookdown, ie (ref:check) ✓ and use the reference within the table.

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')
    ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM