假设我有一个 .Rmd 文件,如下所示:
---
title: "Untitled"
output:
officedown::rdocx_document:
default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
md5-4caffc4710057148fdad652000287a10
# Chapter1 {#ch1}
# Chapter2 {#ch2}
Refer to \@ref(ch1).
当\@ref (ch1) 被多字节字符串(例如,汉字)包围时,它可能会遇到错误。
纯多字节 + ref
上下\@ref(ch1)
混合多字节/单字节 + ref
上a下\@ref(ch1)
参考 + 多字节
\@ref(ch1)。
nchar(u, itype) 中的错误:无效的多字节字符串,元素 1
来电:... regmatches<- -> regmatches -> Map -> mapply ->
你能看看这个问题吗? 谢谢。
会话信息()
R 版本 4.0.2 (2020-06-22)
平台:x86_64-w64-mingw32/x64(64位)
运行于:Windows 10 x64(内部版本 20180)
矩阵产品:默认
语言环境:
[1] LC_COLLATE=中文(简体)_China.936
[2] LC_CTYPE=中文(简体)_China.936
[3] LC_MONETARY=中文(简体)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=中文(简体)_China.936
附加的基础包:
[1] stats graphics grDevices utils datasets 方法
[7] 基地
其他附加包:
[1] 官员_0.3.12 officedown_0.2.0 flextable_0.5.10
[4] ggplot2_3.3.2 tidyr_1.1.1 knitr_1.29
[7] dplyr_1.0.2 reticulate_1.16
通过命名空间加载(未附加):
[1] Rcpp_1.0.5 lattice_0.20-41 prettyunits_1.1.1
[4] sysfonts_0.8.1 ps_1.3.4 utf8_1.1.4
[7] rprojroot_1.3-2 assertthat_0.2.1 digest_0.6.25
[10] R6_2.4.1 反向端口_1.1.9 评估_0.14
[13] 支柱_1.4.6 gdtools_0.2.2 rlang_0.4.7
[16] curl_4.3 uuid_0.1-4 data.table_1.13.0
[19] callr_3.4.3 Matrix_1.2-18 rmarkdown_2.3
[22] desc_1.2.0 labeling_0.3 devtools_2.3.1
[25] stringr_1.4.0 munsell_0.5.0 tinytex_0.25
[28] 编译器_4.0.2 xfun_0.16 pkgconfig_2.0.3
[31] systemfonts_0.2.3 base64enc_0.1-3 pkgbuild_1.1.0
[34] rvg_0.2.5 htmltools_0.5.0 tidyselect_1.1.0
[37] tibble_3.0.3 bookdown_0.20 fansi_0.4.1
[40] 蜡笔_1.3.4 showtextdb_3.0 withr_2.2.0
[43] grid_4.0.2 jsonlite_1.7.0 gtable_0.3.0
[46] 生命周期_0.2.0 magrittr_1.5 scales_1.1.1
[49] zip_2.1.0 cli_2.0.2 stringi_1.4.6
[52] farver_2.0.3 fs_1.5.0 遥控器_2.2.0
[55] testthat_2.3.2 xml2_1.3.2 ellipsis_0.3.1
[58] 泛型_0.0.2 vctrs_0.3.2 工具_4.0.2
[61] showtext_0.9 胶水_1.4.1 purrr_0.3.4
[64] 进程x_3.4.3 pkgload_1.1.0 yaml_2.2.1
[67] colorspace_1.4-1 sessioninfo_1.1.1 memoise_1.1.0
[70] 使用this_1.6.1
是的, @davidgohel ,你是对的。 尽管 .Rmd 文件是 UTF-8 格式,但操作系统在 GBK 编码上运行。 当我更改为bookdown::word_document2
时,knitr 引擎设法编译文件。 但我还是得到了?? 书签应该出现的位置。
您无需尝试新的输出格式功能。
下面显示的结果是使用法语语言环境的 Windows 生成的。 但我确保文件被编码为 UTF-8(我使用的是readr::guess_encoding()
,如果不是 UTF-8 编码,我可以使用fpeek::peek_iconv()
将其更改为 UTF8)。
你能显示结果吗
readr::guess_encoding("your/rmd/file")
结果是
没有 | 编码 | 信心
---|-------------|------------:
1 | UTF-8 | 1
2 | 窗户-1252 | 0.28
嗨@madlogos ,
我也是中国用户。 多字节问题也困扰了我很久。 这是我的窍门:
@ref
;readr::read_lines
它;"\\\\@ref\\([^\\)]+\\)"
模式的字符串;"\\\\@ref\\([^\\)]+\\)"
在单行上;例如, 请参考表\@ref(tab: coco)中的数据
应拆分为
[第 1 行] 请参考表
[第 2 行] \@ref(tab: coco)
[第 3 行] 中的数据
好吧,我不确定这是否是一个有效的解决方案,但它对我有用。 😄
@bishun945感谢您的转机。 好东西。
@madlogos我尝试了另一种解决方案:只需将您的系统和 MS Word 语言切换为英语。