earclimate
记得两年前在RStudio中玩过一次markdown+knitr到pdf文档(不记得用的什么tex,应该是谢大神文档中提到过的,好像不是MikTex),没费多大劲,中文字符等一切正常。
最近到了RStudio:1.0.153版本,按照官方推荐,下载了最新的MikTex(2.9),刚开始测试连英文文档都出不来,找到谢大神相关的帖子,试了下面两个命令,
system('pdflatex --version')
system('latexmk --version')
发现可能是因为perl没装(官方文档里似乎没有明确说要单独安装perl),安装后折腾了下可以导出pdf了,可是换了含中文的文档再次报错,网上搜到说要用WINDOWS-1252编码,于是换到WINDOWS-1252编码,可以出文档了,但是中文是乱码!! 现在不知道如何解决了……
前两年不少帖子讨论过中文编码的问题,谢大神一直鼓励使用UTF-8,感觉到近两年官方推荐的标配和之前有了些变化,请问大家,目前windows环境下中文文档的编译推荐用什么配置(包括Tex版本的选择)?
sessionInfo()
R version 3.4.0 Patched (2017-04-28 r72639)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)
People's Republic of China.936
[2] LC_CTYPE=Chinese (Simplified)
People's Republic of China.936
[3] LC_MONETARY=Chinese (Simplified)
People's Republic of China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)
People's Republic of China.936
attached base packages:
[1] graphics grDevices utils datasets stats methods base
other attached packages:
[1] markdown_0.7.7 rmarkdown_1.4 knitr_1.15.1 tibble_1.2 purrr_0.2.2
[6] dplyr_0.5.0 plyr_1.8.4 magrittr_1.5 reshape2_1.4.2 raster_2.5-8
[11] sp_1.2-4 maps_3.1.0 my_3.0.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 lattice_0.20-35 R6_2.2.0 stringr_1.2.0
[5] tools_3.4.0 grid_3.4.0 data.table_1.10.0 DBI_0.6
[9] htmltools_0.3.5 assertthat_0.2.0 digest_0.6.12 rprojroot_1.2
[13] evaluate_0.10 stringi_1.1.3 compiler_3.4.0 backports_1.0.2
kpathsea version 6.2.3
There is NO warranty. Redistribution of this software is
covered by the terms of both the TeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the TeX source.
Primary author of TeX: D.E. Knuth.
这是
xelatex
情况
> system("xelatex -v")
XeTeX 3.14159265-2.6-0.99998 (TeX Live 2017/W32TeX)
kpathsea version 6.2.3
There is NO warranty. Redistribution of this software is
covered by the terms of both the XeTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the XeTeX source.
Primary author of XeTeX: Jonathan Kew.
Compiled with ICU version 58.2; using 58.2
Compiled with zlib version 1.2.11; using 1.2.11
Compiled with FreeType2 version 2.7.1; using 2.7.1
Compiled with Graphite2 version 1.3.9; using 1.3.9
Compiled with HarfBuzz version 1.4.6; using 1.4.6
Compiled with libpng version 1.6.29; using 1.6.29
Compiled with poppler version 0.52.0
Compiled with fontconfig version 2.12.1; using 2.12.1
我的R版本情况
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936
[2] LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1
yihui
earclimate
你可以不必装 Perl,当 latexmk 不可用的时候,rmarkdown 会自动切换另一种编译方式。编码最好是用 UTF-8,并用 xelatex 编译。我看你的错误消息,可能是你的 MikTeX 需要彻底刷新,参见我的八字箴言日志“
遇到问题,先试升级
”。
你这个帖子又给本骆驼背上
加了一根稻草
,再过两个月如果还没人来解决它,我可能就要出手了。
Cloud2016
谢谢给了这么详细的案例。以上设置在我的配置下仍然报错,主要信息如下:
! Undefined control sequence.
<argument> \LaTeX3 error:
Erroneous variable \c__fontspec_shape_n_n_tl used!
l.3806 \emfontdeclare{ \emshape, \eminnershape }
其实这个报错信息在Rstudio中使用rticles包的CTeX Documents案例(谢大神的中文解决方案)中也出现了。Google后没找到上述报错信息的可能原因。
另外还有一个疑问,您装的是TexLive? xelatex和MiTex, CTex, TexLive是什么关系?
您使用了latex_engine: xelatex引擎,我前面提出的问题是基于MiTex,网络上还有一种方案是基于CTex的,采用的也是latex_engine: xelatex,并且在当前目录下新增header.tex文件,用于定义中文字体(setCJKmainfont),两年前我应该装过CTex并且成功运行了(因为环境变量被清空了,所以有点印象)。
我的
system("tex -v")
命令失败了,
Sorry, but C:\PROGRA~1\MiKTeX\miktex\bin\x64\tex.exe did not succeed.
大概是安装目录在Program Files下有空格? 直接到目录下双击tex.exe是没问题的。这个应该不是问题所在,因为不含中文时编译是没问题的。
Cloud2016
earclimate
您这个问题算是问对人了,我前两天手残,从texlive2016更新到texlive2017,后来又把
l3kernel
等包更新了,结果出现你的这个问题,据查
l3kernel、l3packages、l3experimetal
更新了,而配套的
xeCJK
没来得及更新,
l3kernel
等更新很快,可以参见其github <
https://github.com/latex3/latex3
>,具体内部就不说了(我也说不好),最后只好把之前的texlive卸载,重新安装texlive2017,解决问题。如果不是大神,推荐安装texlive,对于小白,不要吝啬几个G的硬盘,当然乐于折腾的除外。
MikTex是window平台下的,CTex也是,CTex很久没更新了,我估计是被放弃了,转而专注ctex包的开发,这个ctex包更新很快,中文问题也解决的很好,提供ctexart,ctexbook,ctexbeamer三个很方便使用的类。
TexLive一年一次更新,集成很多的包,这个都是测试过的,比较稳定,对tex的依赖不甚了然的童鞋,不要动不动更新它的包。另外它各系统平台都能用。
xelatex、pdflatex、lualatex等是基于tex的不同编译引擎,都被集成到TexLive了
earclimate
Cloud2016
我又清晰了很多,结合楼下谢大的意见,我觉得可能还是版本更新的问题,看来我得彻头彻尾做个更新(由于是新下载的MikTex,我以为已经是最新的,忽视了网上关于update的意见,可能还需要对tex的包做一些更新)。
至于TexLive,2个G我有点接受不了,当年转向R而没走向matlab,就是因为R只有几十M(不过现在也滚到2个G了),好在谢大说他有造轮子的计划,期待smallTex横空出世,哈哈!
Cloud2016
yihui
通过彻底的update,问题果然解决了,而且把perl卸载了也没有影响。
我想这里面确实有一个坑。一般使用者认为从官网下载了最新版本的MiKtex(包括后续自动安装的依赖包)安装后必然是最新的,所以不会考虑是版本的问题,但实际上新安装的tex竟然有很大一部分包是旧版本,如果没有老谢的特别提醒,我是不会留意到程序栏竟然存在一个MikTex update的菜单,如果没有去翻看那些博客内容,我只会把MiKtex卸载了再安装再卸载……
所以结论是windows下MiKtex是没有问题的,而且用老谢的rticles方案(Rstudio中CTex document)和
Cloud2016
提供的上述方案(在我的机子上需要把字体换成宋体和Times New Roman)都可以。
over……