【问题标题】:Rmarkdown pdf kable with accents带有重音符号的 Rmarkdown pdf kable
【发布时间】:2018-07-29 15:34:31
【问题描述】:

我正在尝试创建一个表格,其中包含一些当前无法识别的字符。我已经包括:

- \usepackage[utf8]{inputenc}

和我的代码:

kable(t(tabela.nIncendiosUrbanos),
      caption="\\label{tab:nIncendiosUrbanos} Adaptado de Anuário de Ocorrências de Protecção Civil - ANPC.",
      "latex", align="c", booktabs=TRUE, escape=FALSE) %>%
  kable_styling(latex_options = "striped", full_width = F)

在哪里,

tabela.nIncendiosUrbanos <- data.frame(
      'Edifício de habitação'=c(7000, 7300, 7200, 7200, 7439),
      "Estacionamento"=c(65, 60, 80, 60, 55),
      "Edifício de Serviço"=c(270, 250, 167, 180, 235),
      "Equipamento Escolar"=c(120, 130, 130, 150, 161),
      "Equipamento Hospitalar e Lar de Idosos"=c(80, 95, 65, 100, 88),
      "Edifício de Espetáculos, Lazer e Culto Religioso"=c(70, 80, 65, 75, 69),
      "Hotelaria e Similares"=c(450, 490, 470, 430, 448),
      "Edifício Comercial"=c(430, 350, 300, 290, 290),
      "Edifício Cultural"=c(20, 25, 20, 30, 23),
      "Indústria, Oficina e Armazém"=c(1000, 1230, 1100, 1100, 1237)
    )

我得到的输出是

有人可以帮我解决一下吗?

【问题讨论】:

  • 这可能有助于在表格中保留重音符号:stackoverflow.com/a/46220592/7954106
  • 感谢您的快速回复。我试过了,但没有任何改变。 output: pdf_document: latex_engine: xelatex

标签: r pdf latex kable


【解决方案1】:

概述

在阅读了@user2554330Producing pdf with knitr and Rmarkdown: accents in text show up but not in figures. 的回答后,我使用了tikzDevice 包的GitHub 版本来保留.rmd 输出生成的数字中的重音符号。我还在使用data.frame() 时设置了check.names = FALSE,以确保列名不是coerced to be syntactically valid names

.rmd 代码

---
title: 'Keeping Accent Marks in PDF Output'
author: 'Cowboy Bebop'
date: '`r format(Sys.Date(), "%B %Y")`'
output: pdf_document
---


```{r global options, echo = FALSE, message = FALSE}
# install this particular package from GitHub
# using the 'devtools' package
# install.packages( pkgs = 'devtools' )
devtools::install_github( repo = "daqana/tikzDevice" )

# load necessary packages
library( kableExtra )
library( knitr )
library( magrittr )
library( tikzDevice )

# requesting xelatex instead of the default LaTeX engine does seem to work
# thank you to SO for this answer 
# https://stackoverflow.com/a/46220592/7954106
options( tikzDefaultEngine = "xetex" )

# load neccesary data
tabela.nIncendiosUrbanos <- data.frame(
      'Edifício de habitação'        = c(7000, 7300, 7200, 7200, 7439),
      "Estacionamento"               = c(65, 60, 80, 60, 55),
      "Edifício de Serviço"          = c(270, 250, 167, 180, 235),
      "Equipamento Escolar"          = c(120, 130, 130, 150, 161),
      "Equipamento Hospitalar e Lar de Idosos"           = c(80, 95, 65, 100, 88),
      "Edifício de Espetáculos, Lazer e Culto Religioso" = c(70, 80, 65, 75, 69),
      "Hotelaria e Similares"        = c(450, 490, 470, 430, 448),
      "Edifício Comercial"           = c(430, 350, 300, 290, 290),
      "Edifício Cultural"            = c(20, 25, 20, 30, 23),
      "Indústria, Oficina e Armazém" = c(1000, 1230, 1100, 1100, 1237),
      # setting check.names equal to FALSE
      # to avoid coercing the column names
      # to be synatactically valid
      # for more info, see ?make.names
      check.names = FALSE )
```

# Produce a table using `knitr::kable()`

```{r Produce table}
kable( x = t( x = tabela.nIncendiosUrbanos)
       , caption = "\\label{tab:nIncendiosUrbanos} Adaptado de Anuário de Ocorrências de Protecção Civil - ANPC."
       , format = "latex"
       , align = "c"
       , booktabs = TRUE
       , escape = FALSE ) %>%
  kable_styling( latex_options = "striped"
                 , full_width = FALSE )
```

会话信息

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_1.13.6  tikzDevice_0.12  magrittr_1.5     knitr_1.20      
[5] kableExtra_0.9.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18      rstudioapi_0.7    xml2_1.2.0        hms_0.4.2        
 [5] munsell_0.5.0     rvest_0.3.2       viridisLite_0.3.0 colorspace_1.3-2 
 [9] R6_2.2.2          rlang_0.2.1       filehash_2.4-1    plyr_1.8.4       
[13] stringr_1.3.1     httr_1.3.1        tools_3.5.1       grid_3.5.1       
[17] withr_2.1.2       htmltools_0.3.6   yaml_2.1.19       digest_0.6.15    
[21] rprojroot_1.3-2   tibble_1.4.2      crayon_1.3.4      readr_1.1.1      
[25] memoise_1.1.0     evaluate_0.11     rmarkdown_1.10    stringi_1.2.4    
[29] compiler_3.5.1    pillar_1.3.0      scales_0.5.0      backports_1.1.2  
[33] pkgconfig_2.0.1 

【讨论】:

  • 我已将您的建议复制并粘贴到一个新文件中,但输出结果相同。我仍然有同样奇怪的输出:Edif&lt;U+00ED&gt;cio de habita&lt;U+00E7&gt;&lt;U+00E3&gt;o我错过了什么吗?
  • @SergioCavaleiroCosta 我们的软件包版本或R 本身可能不同。请将sessionInfo() 的输出与我添加到此答案的输出交叉引用。
  • 知道我的 RStudio 版本是 1.1.423 也可能会有所帮助。
  • 非常感谢! :D 我已经更新了 R 和 Rstudio,它开始工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2023-04-11
  • 2019-12-08
  • 2016-11-25
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
相关资源
最近更新 更多