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 月份安装了。

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

            dapengde 完全正确。你不能通过纯 LaTeX 命令行或者 TeXStudio 编译。当然,其实 Rscript -e "rmarkdown::render('foo.Rmd')" 也是命令行。

              yihui 经测试没问题,可以正常编译出pdf。是在 R 内设置 Sys.setenv('PATH') 追加 TinyTeX 路径?
              #L50

                Cloud2016 是的,只是在有必要的情况下(条件:系统是 Linux、~/bin/tlmgr 存在但通过 Sys.which() 找不到或者找到的不是它),临时调整 PATH,编完了再恢复。

                yihui
                我本身除了 TinyTeX 之外没有装任何 TeX,使用 zsh,之前把 TinyTeX 路径自己加在~/.zshrc
                刚刚从~/.zshrcPATH设置中去掉 TinyTeX 。然后:

                devtools::install_github('yihui/tinytex')
                tinytex::install_tinytex(force=TRUE)

                重新安装。安装一切顺利。
                之后直接打开 RStudio:

                R >>> library(tinytex)
                R >>> Sys.getenv('PATH')
                [1] "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
                R >>> tinytex::is_tinytext()
                Error: 'is_tinytext' is not an exported object from 'namespace:tinytex'

                不管,直接打开测试的在R Markdown文档中使用中文.Rmd编译。报错:

                pandoc: xelatex not found. xelatex is needed for pdf output.
                Error: pandoc document conversion failed with error 41
                Execution halted

                貌似还是路径问题?找不到 xelatex。还是我配置问题?

                但是测试编译xelatex('xxx.tex')文件是可以的,但是 RStudio 打开 tex 文件再点击 Compile PDF 还是提示找不到 XeLaTeX:

                Unabled to find specified LaTeX program 'XeLaTeX' on the system path

                所以如果要用 knitr还需要另外配置?

                另外:

                R >>> tlmgr_update()
                tlmgr update --all --self
                tlmgr: package repository http://mirrors.ustc.edu.cn/CTAN/systems/texlive/tlnet (verified)
                sh: 1: fmtutil-sys: not found
                R >>> tlmgr_search('fmtutil-sys')
                tlmgr search --file --global 'fmtutil-sys'
                tlmgr: package repository http://mirrors.ustc.edu.cn/CTAN/systems/texlive/tlnet (verified)
                tetex:
                	texmf-dist/doc/man/man1/fmtutil-sys.1
                	texmf-dist/doc/man/man1/fmtutil-sys.man1.pdf
                	texmf-dist/scripts/texlive/fmtutil-sys.sh
                tetex.amd64-freebsd:
                	bin/amd64-freebsd/fmtutil-sys
                tetex.amd64-netbsd:
                	bin/amd64-netbsd/fmtutil-sys
                tetex.armel-linux:
                	bin/armel-linux/fmtutil-sys
                tetex.armhf-linux:
                	bin/armhf-linux/fmtutil-sys
                tetex.i386-cygwin:
                	bin/i386-cygwin/fmtutil-sys
                tetex.i386-darwin:
                	bin/i386-darwin/fmtutil-sys
                tetex.i386-freebsd:
                	bin/i386-freebsd/fmtutil-sys
                tetex.i386-linux:
                	bin/i386-linux/fmtutil-sys
                tetex.i386-netbsd:
                	bin/i386-netbsd/fmtutil-sys
                tetex.i386-solaris:
                	bin/i386-solaris/fmtutil-sys
                tetex.powerpc-darwin:
                	bin/powerpc-darwin/fmtutil-sys
                tetex.powerpc-linux:
                	bin/powerpc-linux/fmtutil-sys
                tetex.sparc-solaris:
                	bin/sparc-solaris/fmtutil-sys
                tetex.win32:
                	bin/win32/fmtutil-sys.exe
                tetex.x86_64-cygwin:
                	bin/x86_64-cygwin/fmtutil-sys
                tetex.x86_64-darwin:
                	bin/x86_64-darwin/fmtutil-sys
                tetex.x86_64-darwinlegacy:
                	bin/x86_64-darwinlegacy/fmtutil-sys
                tetex.x86_64-linux:
                	bin/x86_64-linux/fmtutil-sys
                tetex.x86_64-solaris:
                	bin/x86_64-solaris/fmtutil-sys

                这个?

                  JackieMe 你需要

                  devtools::install_github(c('yihui/tinytex', 'rstudio/rmarkdown'))

                  另外,是 tinytex:::is_tinytex() 而不是 tinytex::is_tinytext()

                    yihui OK。install_github之后可以编译 Rmd 文件了。
                    tinytex::is_tinytex()命令去掉了是吧?

                      19 天 后

                      yihui

                      code.tex 文件内容如下:

                      \documentclass{ctexart}
                      \usepackage{minted}
                      
                      \title{R语言}
                      \author{某某}
                      
                      \begin{document}
                      \maketitle
                      \section{数据描述}
                      
                      \begin{minted}{c}
                      int main() {
                      	printf("hello, world");
                      	return 0;
                      }
                      \end{minted}
                      
                      \begin{minted}{python}
                      def boring(args = None):
                      	pass
                      \end{minted}
                      
                      \begin{minted}{r}
                      require(stats) # for lowess, rpois, rnorm
                      plot(cars)
                      lines(lowess(cars))
                      plot(sin, -pi, 2*pi) # see ?plot.function
                      ## Discrete Distribution Plot:
                      plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
                           main = "rpois(100, lambda = 5)")
                      ## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
                      plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
                      points(x, cex = .5, col = "dark red")
                      \end{minted}
                      
                      \mint{python}|import this|
                      
                      \end{document}

                      使用 minted 包的tex文件,需要在编译的时候使用-shell-escape参数,如果使用 tinytex::xelatex,不知道如何传这个参数?如果不能传递,谢大大是否可以再添加 args 选项让用户自己传递类似上述 -shell-escape 参数?

                      system2('xelatex', args = '-shell-escape code') # 没问题,但是要运行几遍才有目录索引
                      
                      tinytex::xelatex(file = 'code.tex') 
                      
                      # I was unable to find any missing LaTeX packages from the error log code.log.
                      # ! Package minted Error: You must invoke LaTeX with the -shell-escape flag.
                      # 
                      # Error: Failed to compile code.tex. See code.log for more info.
                      # In addition: Warning message:
                      #   running command '"xelatex" -halt-on-error -interaction=batchmode "code.tex"' had status 1

                        Cloud2016 安装 Github 上最新版的 tinytex 包 devtools::install_github('yihui/tinytex'),然后

                        tinytex::xelatex('code.tex', engine_args = '-shell-escape')

                        应该就可以了。如果懒得每次都敲这个,可以设置全局选项:

                        options(tinytex.engine_args = '-shell-escape')

                        然后 tinytex::xelatex('code.tex')

                        另外我建议你少花点时间折腾 minted 包。据我观察,你正在一步一步重复蹚我年少时蹚过的浑水,LaTeX 真的很浪费时间,投入产出严重不成比例。这种语法高亮的问题还是用 R Markdown 算了,Pandoc 已经把事情都给办好了。如果你真的闲得慌,我可以给你找一堆更有意义的事情来做。

                          yihui 你还说!要不是你鼓捣出来个 bookdown,我早就跟 LaTeX 决裂了!