yihui 1. 多谢提点,改了 .Rprofile 解决了。如果使用 CentOS 仓库自带的R是肯定捆绑的,我始终没有找到替代办法。
2. 我用非常干净的 CentOS minimal 版本,先按照

wget -qO- \
  "https://github.com/yihui/tinytex/raw/master/tools/install-unx.sh" | sh

TinyTeX 装好,自己把依赖找齐,然后编译 R, 再设置 .Rprofiletinytex 包也可以继续用了

    今天根据我的日常使用情况测试了一下,中文出不来啊!

    占位,回家之后编辑?

    yihui 1. 折腾一次就够了 ? 我把它打包成虚拟机镜象了,一劳永逸

    1. 虚拟机放在百度云,分享链接: <https://pan.baidu.com/s/1nv67anJ> 密码: v3mr,用账户 cloud2016 密码 cloud登陆虚拟机即可使用,配置过程比较血腥,就不放了。

    2. 测试的 Rmd 文件,看这里1 ,编译出来的效果,看这里2

    PS: 虚拟机镜像 TinyTeX.ova 有1.55个G,是因为装了常用的一些R包,列表如下:

    > list.files(.libPaths())
      [1] "assertthat"        "backports"         "base"
      [4] "base64enc"         "BH"                "bibtex"
      [7] "bitops"            "bookdown"          "boot"
     [10] "caTools"           "citr"              "class"
     [13] "cluster"           "codetools"         "colormap"
     [16] "colorspace"        "compiler"          "curl"
     [19] "datasets"          "devtools"          "dichromat"
     [22] "digest"            "evaluate"          "foreign"
     [25] "geoR"              "ggplot2"           "git2r"
     [28] "graphics"          "grDevices"         "grid"
     [31] "gridExtra"         "gtable"            "highr"
     [34] "htmltools"         "htmlwidgets"       "httpuv"
     [37] "httr"              "jsonlite"          "KernSmooth"
     [40] "knitr"             "labeling"          "lattice"
     [43] "lazyeval"          "lubridate"         "magrittr"
     [46] "mapdata"           "mapproj"           "maps"
     [49] "maptools"          "markdown"          "MASS"
     [52] "Matrix"            "maxLik"            "memoise"
     [55] "methods"           "mgcv"              "mime"
     [58] "miniUI"            "miscTools"         "munsell"
     [61] "nlme"              "nnet"              "numDeriv"
     [64] "openssl"           "parallel"          "pdist"
     [67] "plyr"              "PrevMap"           "R6"
     [70] "RandomFields"      "RandomFieldsUtils" "raster"
     [73] "RColorBrewer"      "Rcpp"              "RefManageR"
     [76] "reshape2"          "rgl"               "rlang"
     [79] "rmarkdown"         "rpart"             "rprojroot"
     [82] "rstudioapi"        "rticles"           "sandwich"
     [85] "scales"            "shiny"             "showtext"
     [88] "showtextdb"        "sourcetools"       "sp"
     [91] "spatial"           "splancs"           "splines"
     [94] "stats"             "stats4"            "stringi"
     [97] "stringr"           "survival"          "sysfonts"
    [100] "tcltk"             "tibble"            "tinytex"
    [103] "tools"             "translations"      "truncnorm"
    [106] "utils"             "V8"                "viridisLite"
    [109] "whisker"           "withr"             "xml2"
    [112] "xtable"            "yaml"              "zoo"

    本来想把 rocker/geospatial 这个 docker 镜像有的都装上,但是 sf 包装不上,那一堆依赖全部需要手动从官网下载编译,因为仓库自带的版本太低,不能用,我滴肾!

    出于好奇尝试了一下,这里面有深坑。。。

    Debian sid,R 3.4.3。在 RStudio 里装的,安装过程没啥,提示:

    TinyTeX installed to /home/xxx/.TinyTeX
    Warning message:
    In tinytex::install_tinytex() :
      TinyTeX was not successfully installed or configured.Your PATH variable is /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games. See https://yihui.name/tinytex/faq/ for more information.

    发现这个路径不对啊,进一步,Sys.getenv('PATH')确认了问题,路径是系统默认的,并不是~/.zshrc或者~/.bashrc之类的文件自定义的。改了~/.profile问题依旧,开始怀疑了。

    果断终端开个 R 看看,结果终端 R console 打开没有问题,Sys.getenv('PATH')显示正确,但是:

    > tinytex::is_tinytext()
    FALSE

    再开 RStudio,PATH还是系统默认,自定义的又没了,到这里基本确实肯定哪里有问题。强行在 RStudio 里测试:

    R>library(tinytex)
    R>Sys.getenv('PATH')
    [1] "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
    R>tinytex:::is_tinytex()
    [1] FALSE

    好吧,只能 Google 了,结果:system() call from RStudio does not find path; same command from commandline R works fine 。目测发现了 RStudio 的万年 bug。

    好了,知道了,终端打开 RStudio 呢,果然,终端开 RStudio 之后Sys.getenv('PATH')终于 RStudio 读到了正确的路径,但是,但是,

    R>tinytex:::is_tinytex()
    [1] FALSE

    依旧。

      JackieMe
      Never mind.
      测试了一个简单文件,可以编译出 PDF。中文貌似有问题,趟下一个坑去了。

      yihui 好的!新版 rmarkdown 已安装。我用 RStudio 新建 bookdown 项目得到了 'A Minimal Book Example',build 出了一个 test.tex 文件,里面的参考文献声明如下:

      \bibliography{book.bib,packages.bib}

      下面是用 tinytex + CTeX 对 test.tex 文件的编译信息。貌似找到 .bib 文件了:Found bibliography file(s) [book.bib packages.bib],但 .bbl 文件里的条目是空的。

      > tinytex::pdflatex('test.tex')
      Error: Failed to compile test.tex.
      In addition: Warning message:
      running command '"pdflatex" -halt-on-error -interaction=batchmode "test.tex"' had status 1 
      
      > tinytex::latexmk('test.tex', 'pdflatex')
      Latexmk: This is Latexmk, John Collins, 20 Nov. 2017, version: 4.54.
      Rule 'pdflatex': File changes, etc:
         Changed files, or newly in use since previous run(s):
            'test.aux'
      ------------
      Run number 1 of rule 'pdflatex'
      ------------
      ------------
      Running 'pdflatex  -halt-on-error -interaction=batchmode -recorder  "test.tex"'
      ------------
      Latexmk: applying rule 'pdflatex'...
      This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500)
      entering extended mode
      Latexmk: Found input bbl file 'test.bbl'
      Latexmk: Log file says output to 'test.pdf'
      Latexmk: List of undefined refs and citations:
        Citation `R-bookdown' on page 7 undefined on input line 249
        Citation `xie2015' on page 7 undefined on input line 250
      === TeX engine is 'pdfTeX'
      Latexmk: Found bibliography file(s) [book.bib packages.bib]
      Latexmk: Summary of warnings:
        Latex failed to resolve 2 citation(s)
      Rule 'pdflatex': File changes, etc:
         Changed files, or newly in use since previous run(s):
            'test.toc'
      ------------
      Run number 2 of rule 'pdflatex'
      ------------
      ------------
      Running 'pdflatex  -halt-on-error -interaction=batchmode -recorder  "test.tex"'
      ------------
      Latexmk: applying rule 'pdflatex'...
      This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500)
      entering extended mode
      Latexmk: Found input bbl file 'test.bbl'
      Latexmk: Log file says output to 'test.pdf'
      Latexmk: List of undefined refs and citations:
        Citation `R-bookdown' on page 7 undefined on input line 249
        Citation `xie2015' on page 7 undefined on input line 250
      === TeX engine is 'pdfTeX'
      Latexmk: Found bibliography file(s) [book.bib packages.bib]
      Latexmk: Summary of warnings:
        Latex failed to resolve 2 citation(s)
      Latexmk: All targets (test.pdf) are up-to-date
      Warning message:
      running command '"latexmk" -pdf -latexoption=-halt-on-error -interaction=batchmode -pdflatex=pdflatex "test.tex"' had status 12 
      > devtools::session_info()
      Session info -------------------------------------------------------------------------------
       setting  value                                              
       version  R version 3.4.3 (2017-11-30)                       
       system   x86_64, mingw32                                    
       ui       RStudio (1.1.383)                                  
       language (EN)                                               
       collate  Chinese (Simplified)_People's Republic of China.936
       tz       Europe/Berlin                                      
       date     2017-12-20                                         
      
      Packages -----------------------------------------------------------------------------------
       package   * version date       source                        
       base      * 3.4.3   2017-11-30 local                         
       compiler    3.4.3   2017-11-30 local                         
       curl        3.0     2017-10-06 CRAN (R 3.4.2)                
       datasets  * 3.4.3   2017-11-30 local                         
       devtools    1.13.4  2017-11-09 CRAN (R 3.4.3)                
       digest      0.6.12  2017-01-27 CRAN (R 3.3.2)                
       git2r       0.19.0  2017-07-19 CRAN (R 3.4.1)                
       graphics  * 3.4.3   2017-11-30 local                         
       grDevices * 3.4.3   2017-11-30 local                         
       httr        1.3.1   2017-08-20 CRAN (R 3.4.1)                
       memoise     1.1.0   2017-04-21 CRAN (R 3.3.3)                
       methods   * 3.4.3   2017-11-30 local                         
       R6          2.2.2   2017-06-17 CRAN (R 3.3.3)                
       stats     * 3.4.3   2017-11-30 local                         
       tinytex     0.1.6   2017-12-19 Github (yihui/tinytex@acbc9db)
       tools       3.4.3   2017-11-30 local                         
       utils     * 3.4.3   2017-11-30 local                         
       withr       2.1.0   2017-11-01 CRAN (R 3.4.3)                
       yaml        2.1.16  2017-12-12 CRAN (R 3.4.3)  

        Cloud2016 谢谢。真是跟不上节奏。已升级,但仍然不行。这个不是 tinytex 的问题,是 MiKTeX 或者 BiBTeX 的问题。我放弃了,就带病生存吧,也挺好。

        tinytex     0.2     2017-12-20 Github (yihui/tinytex@dbb4900) 

        JackieMe 这个 PATH 问题不能算 RStudio 的问题,所有图形界面工具都会存在这个问题。核心问题就是只有你的命令行终端会读那些 ~/.bash_profile 之类的文件,图形界面工具不会读那些文件,所以你在那里面修改 PATH 是无效的。

        dapengde 你直接在命令行窗口中跑 "pdflatex" -halt-on-error -interaction=batchmode "test.tex" 是什么结果?有没有日志文件 test.log?

          yihui 你咋就不让我放弃呢? 在cmd 窗口运行 pdflatex,有 log 文件,里面关于参考文献的信息是:

          Package natbib Warning: Citation `R-bookdown' on page 7 undefined on input line
           249.
          Package natbib Warning: Citation `xie2015' on page 7 undefined on input line 25
          0.

          这是运行第一遍 pdflatex 的记录。关键是下一步运行 bibtex,就成了 book.bib.bib 了,所以后面第二遍第三遍运行的 pdflatex 就没啥意义了:

          Microsoft Windows [Version 6.1.7601]
          Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
          
          D:\temp\newpkg\test>"pdflatex" -halt-on-error -interaction=batchmode "test.tex"
          
          This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6500)
          entering extended mode
          
          D:\temp\newpkg\test>bibtex test
          This is BibTeX, Version 0.99d (MiKTeX 2.9.6500)
          The top-level auxiliary file: test.aux
          The style file: apalike.bst
          I couldn't open database file book.bib.bib
          ---line 50 of file test.aux
           : \bibdata{book.bib
           :                  ,packages.bib}
          I'm skipping whatever remains of this command
          I found no database files---while reading file test.aux
          Warning--I didn't find a database entry for "R-bookdown"
          Warning--I didn't find a database entry for "xie2015"
          (There were 2 error messages)

            dapengde 嗯,我只想知道第一遍 pdflatex 为什么会失败,后面不需要跑 bibtex(它肯定会失败,这个我明白)。你得先把那些 .aux / .bbl / .blg 之类的辅助文件删掉再跑 pdflatex,我想看的是 .log 文件里面说什么。

            因为你这个问题实在是太匪夷所思了,我想不明白为什么鱼儿会漏网。

              yihui .log 太长,来这里看看。你现在搞得我非常讳疾忌医。

              我在 .tex 里把 .bib 文件的扩展名去掉编译,得到的 .log 文件跟没去掉扩展名得到的 .log 文件比较了一下,两个 .log 文件一模一样。但是第二步 运行 bibtex test 就不同了,一个找到了 .bib 文件,一个没找到 .bib.bib 文件。

                yihui 反正试过了在 R console 和 终端打开的 RStudio 都可以成功编译 PDF,我的折腾也算告一段落了。其实我开始就说纯属好奇,因为我平时基本不写 R Markdown 文档?

                  dapengde 在两个网友的首次见面会上,我们解决了这个谜题。参考文献编不出来是因为 CTeX 里面的 latexmk.exe 居然可以不依赖 LaTeX 包 latexmk 就可以运行,而且疑案现场的电脑上还因为不明原因碰巧装了 Perl,在这两重因素下,tinytex::latexmk() 被说服了使用命令行工具 latexmk 来编 PDF,由于该工具没有跟我一样对 .aux 文件中的 .bib 文件名打补丁,于是 bibtex 抱怨说找不到 foo.bib.bib 文件。我刚在 tinytex 中强制启用了模拟模式,也就是说除非用户特地指定用 latexmk,否则我就用我自己的模拟模式去编 PDF,这个模式对带 .bib 扩展名的文件是安全的(跨平台通用)。

                  Cloud2016 我希望你可以试试最新的开发版,看看你不自己调整 PATH 变量能否正常编译 PDF。

                    yihui 网友见面我总是紧张,尤其是男网友,尤其是男偶像。手都不知道往哪儿放了。PS:Perl 是哪里来的,真不知道。

                    感谢偶像帮我修电脑!

                    PPS: 又查了一下,我的 Windows 下有一个叫 Strawberry Perl 的安装,和一个 ActivePerl 的安装。你远程看到的 c:/perl 来自后者。根据安装时间猜测,我可能是在折腾某些在 linux 容易安装但是在 windows 不容易安装的东西时,按网上的方法装上去的,可能是 python 的某个库或者别的什么东西——完全不记得了。

                    我卸载了这两个 Perl 之后,即使不使用你今天施的法术,而保留 latexmk.exe 原来的样子,也可以正常编译了!那么说到底这个锅是 perl 来背咯?perl 在 latex 里多管闲事?

                      dapengde Perl 是躺着中枪,只是因为 latexmk 包本来是用 Perl 写的而已。真正的锅还是该 MiKTeX 里的 bibtex 来背,它是麻烦的根源。它看见 foo.bib 时非要认为是 foo.bib.bib,为了向它投降,我们必须把扩展名去掉,而去掉扩展名会导致文档不具有可移植性(某个工具下能编,换个工具就不能编)。可移植性比隐藏扩展名要重要多了,这一点 MiKTeX 中的 bibtex 的作者可能不太认同(估计伊不用非 Windows 的系统),而 latexmk.exe 又没像我一样对这个问题打补丁,放任 bibtex 出错。既然它不打补丁,我只好把它默认辞退了。

                        yihui 明白了。目前是在 bookdown 里编译就没问题,但是在 cmd 里或者 texstudio 里编译 tex 文件仍然是在傻傻地找 foo.bib.bib. 也就是说, bookdown / rmarkdown 为 bibtex 填坑的行为受到了我电脑里 perl 的干扰,而其他软件根本连 bibtex 的坑都没填。

                        你上面多次提到的为 bibtex 填坑的行为,我一直很困惑,就是因为我这里 perl 的存在导致我这里这个坑压根儿就没填上。

                        这也解释了为何我最初用 bookdown 没这问题——当时是今年 3 月份,而我的 Perl 是 5 月份安装了。

                        这正是小白长得像哥哥:真相大白。