• R语言
  • 用tinytex和rticles编译中文Rmd失败 ! LaTeX Error: Option clash for package fontspec.

问题描述

在确定tinytex可以工作后(因为可以编译英文rmd),安装了rticles包。但是编译模板CTex Documents 失败,提示
tinytex ! LaTeX Error: File 'ctexart.cls' not found.
之后尝试使用tinytex::tlmgr_install("ctex")安装,未果。
后按照网上提示,使用options(tinytex.install_packages = TRUE)允许自动安装缺失的包。开始编译后下载了许多缺失内容。之后开始报错 ! LaTeX Error: Option clash for package fontspec.
使用parse_packages函数并未在log文件中发现缺失的latex包。

System

R version 3.6.2 (2019-12-12)
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.6.2 htmltools_0.4.0 tools_3.6.2 rticles_0.19 yaml_2.2.1
[6] Rcpp_1.0.3 tinytex_0.31 rmarkdown_2.6 knitr_1.32 digest_0.6.25
[11] xfun_0.22 rlang_0.4.5 evaluate_0.14

报错内容

output file: tw.knit.md

"D:/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS tw.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output tw.tex --self-contained --table-of-contents --toc-depth 2 --number-sections --highlight-style tango --latex-engine xelatex --variable graphics
! LaTeX Error: Option clash for package fontspec.

错误: LaTeX failed to compile tw.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See tw.log for more info.
停止执行

    Why suddenly Chinese input is not working? Anyway.

    看起来是 LaTeX 的问题,fontspec 宏包是给 XeLaTeX and LuaLaTeX 用的。用 LaTeX 或者 PDFLaTeX 编译会出错。
    最好给个最小的可以重现这个错误的代码,方便大家帮忙。

      Fye 感谢您的回复。我今天在两台设备上配置中文rmd。在安装后tinytex后均安装了rticles。之后尝试编译模板文件CTex Documents,均报错,提示ctex等包找不到。于是在网络上搜索解决办法,安装了所有编译要用的latex相关包。之后一台设备可以正常编译, 但是另一台设备会提示关于这个字体包的问题。并不需要代码,直接手动knit这个ctex自带的模板就会出现这个错误。

      yihui 感谢 谢神的回复。
      我现在已经确保安装了tinytex,因为在没有其他lex组件的情况下设备可以编译英文rmd。
      那个指令是在网上看到的,因为编译中文后会发现缺失很多包,anyway现在已经把缺失的包安装上了。
      然而在中文模板文档knit后会出现fontspec包的这个冲突。请问如何解决这个冲突
      另外我有另一台设备经历一样的过程,最终可以编译中文,但是这台设备的系统里原先安装过letex和texmaker,不知道是否与这个相关。

      PS:为什么论坛里打汉字有问题,他会自动输出我敲过的字符历史,而且是每敲一次从头输出(Win10+Chrome)
      jjijiujiuxjiuxijiuxiajiuxianjiuxiangjiuxiangzjiuxiangzhjiuxiangzhejiuxiangzhejjiuxiangzhejujiuxiangzhejuhjiuxiangzhejuhujiuxiangzhejuhuajiuxiangzhejuhuayjiuxiangzhejuhuayijiuxiangzhejuhuayiy就像这句话一样

        Boss 然而在中文模板文档knit后会出现fontspec包的这个冲突。请问如何解决这个冲突

        我无法重现你的问题。还是那句话,确认一下安装了 TinyTeX(可以看 tinytex::tinytex_root() 的结果是否为空或出错)。如果安装了 TinyTeX 还是出错的话,按错误消息中的提示走。如果都走了一遍还是不行,那八成我也无能为力了。

        • Boss 回复了此帖

          yihui
          已经安装 以下为结果

          tinytex::tinytex_root()

          [1] "C:\\Users\\chen\\AppData\\Roaming\\TinyTeX"

          日志中相关报错为

          Package fontspec Info: Could not resolve font "KaiTi/B" (it probably doesn't
          (fontspec) exist).

          Package fontspec Info: Could not resolve font "SimHei/I" (it probably doesn't
          (fontspec) exist).

          Package fontspec Info: Could not resolve font "SimSun/BI" (it probably doesn't
          (fontspec) exist).

          Package fontspec Info: Font family 'SimSun(0)' created for font 'SimSun' with
          (fontspec) options
          (fontspec) [Script={CJK},BoldFont={SimHei},ItalicFont={KaiTi}].
          (fontspec)
          (fontspec) This font family consists of the following NFSS
          (fontspec) series/shapes:
          (fontspec)
          (fontspec) - 'normal' (m/n) with NFSS spec.:
          (fontspec) <->"SimSun/OT:script=hani;language=dflt;"
          (fontspec) - 'small caps' (m/sc) with NFSS spec.:
          (fontspec) - 'bold' (b/n) with NFSS spec.:
          (fontspec) <->"SimHei/OT:script=hani;language=dflt;"
          (fontspec) - 'bold small caps' (b/sc) with NFSS spec.:
          (fontspec) - 'italic' (m/it) with NFSS spec.:
          (fontspec) <->"KaiTi/OT:script=hani;language=dflt;"
          (fontspec) - 'italic small caps' (m/scit) with NFSS spec.:

          ))

          24 天 后

          谢谢回复。请问如何彻底卸载tinytex及其相关组件,我想卸载了重新安装。

            Boss

            https://yihui.org/tinytex/#for-r-users

            install.packages('tinytex')
            tinytex::uninstall_tinytex() 
            tinytex::install_tinytex()

            另外,请问你安装 TinyTeX 之前有没有安装其他 TeX 发行版,卸载干净了吗?不知道未卸载干净的 TeX 发行版会不会影响 TinyTeX。

              CyrusYip 系统中若存在其它 LaTeX 发行版,通常应该不会影响 R 用户(因为 R 包 tinytex 会优先使用 TinyTeX)。

              如果是卸载重装,那么一步 tinytex::reinstall_tinytex() 即可,这样有个额外的好处,就是之前安装的 LaTeX 包也会全部自动重装。分两步走的话,以前安装的额外的包会丢失。

              6 天 后