knitr pandoc:“无法使用pdf编写器生成pdf输出" [英] knitr pandoc: "cannot produce pdf output with pdf writer"
问题描述
最前:在knitr中使用 pandoc()
,在尝试将.md或.Rmd编译为PDF时会抱怨.
Up front: using pandoc()
in knitr, it complains when trying to compile .md or .Rmd into a PDF.
我正在简化可重复性研究的过程,正如许多地方所记录的那样.我正在使用 pandoc
和 knitr
并生成出色的文档.我还试图简化一些不擅长编程的同事,但我们正在尝试使用类似的文件.对于以用户友好"为基础的以降价为中心的编辑器,有几个选项,由于种种原因,我选择RStudio(对我来说,这是emacs/ess,但这是不同的).
I'm streamlining the process for reproducible research, as has been documented in many places. I'm using pandoc
and knitr
and producing great documents. I'm also trying to streamline for some co-workers who are not as adept with programming, yet we're trying to use similar files. There are several options for "user friendly" markdown-centric editors, and for several reasons I'm leaning on RStudio (for them, emacs/ess for me, but that's different).
我的工作流程:给他们一个markdown(.md或.Rmd)文件,让他们能够进行更改并将其重新呈现为PDF.不幸的是,RStudio不允许(AFAICT)设置模板或其他任意的Pandoc配置参数(例如,章节,数字部分),因此在R/knitr中使用 pandoc()
非常有意义.
My workflow: give them a markdown (.md or .Rmd) file and have them be able to make changes and optionally re-render it into a PDF. Unfortunately, RStudio does not (AFAICT) allow setting templates or other arbitrary pandoc configuration parameters (e.g., chapters, number-sections), so using pandoc()
in R/knitr makes a lot of sense here.
使用 whitepaper.Rmd
作为输入文件,我在R中运行 pandoc('whitepaper.Rmd','pdf')
并立即获得:
Using whitepaper.Rmd
as the input file, I run pandoc('whitepaper.Rmd', 'pdf')
in R and immediately get:
> pandoc('whitepaper.Rmd', 'pdf')
executing pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -t pdf -o whitepaper.pdf "whitepaper.Rmd"
pandoc.exe: cannot produce pdf output with pdf writer
Error in (function (input, format, ext, cfg) : conversion failed
我在特定于knitr的标头中明确包含"t:latex",尽管没有它, pandoc()
仍在系统调用中添加"-t pdf",这在pandoc.exe
不接受.
I explicitly have "t:latex" in my knitr-specific header, though without it, pandoc()
is still adding "-t pdf" to the system call, something that pandoc.exe
does not accept.
通过故障排除,如果我删除'-t pdf'
,该命令也可以正常工作,因此输入文件本身似乎没有问题:
With troubleshooting, the command works just fine if I remove '-t pdf'
, so it seems that there is nothing wrong with the input file itself:
> system('pandoc -t latex --standalone --smart --number-sections --template=report.tex -f markdown -o whitepaper.pdf "whitepaper.Rmd"')
关于此主题还有许多其他对话: 14586177 , 14508429 ,15258233 ,和受到广泛讨论的 11025123 .他们全都解决了需要命令行工作,额外的中间步骤,外部Makefile或 knit2pdf()
(不使用texi2pdf的代码)的解决方案.
There have been numerous other conversations regarding this topic: 14586177, 14508429, 15258233, and the heavily-discussed 11025123. They all resolve to solutions that require command-line work, extra middle-steps, external Makefiles, or knit2pdf()
(which uses texi2pdf, not desired).
我所看到的约束:
- 在R环境中轻松操作;
- 利用Yihui的"
<!-pandoc ...->
"文件内配置(我继续在模板之间任意切换(例如以下示例之一); - 最好用一个标准化"命令(即"
pandoc('whitepaper.Rmd','pdf')
")执行此操作.
- operate easily within the R environment;
- take advantage of Yihui's "
<!--pandoc ... -->
" in-file configuration (which allows me to continue to switch arbitrarily between my templates, for one of several examples); - preferably, execute this with a single "standardized" command (i.e., "
pandoc('whitepaper.Rmd', 'pdf')
").
...因此,一旦在文件中设置了参数,编辑和重新渲染就比较麻烦.
... so that, once the parameters are set in-file, editing and re-rendering is relatively brain-dead.
我可以修补和覆盖Yihui的 knitr ::: pandoc_one()
来删除有害的'-t'和格式,但是我想知道其他地方可能会有哪些副作用.这种解决方案既不可持续,也不是正确的方法(tm)".
I can patch and overwrite Yihui's knitr:::pandoc_one()
to remove the offending addition of '-t' and format, but I wonder what side-effects that might have elsewhere. This solution isn't sustainable nor "The Right Way (tm)".
有关解决此问题的正确方法(tm)"的建议?我想念一个简单/显而易见的解决方案吗?
Suggestions for "Right Ways (tm)" to solve this problem? Am I missing an easy/obvious solution?
顺便说一句:谢谢谢一辉(Yihui Xie)的针织衫,以及约翰麦克法兰(John MacFarlane)的pandoc.太棒了!(也许我可以向其中一个或两个提交补丁建议,以解决我的用例,尽管如果只是我一个人,那可能就不值得了.)
BTW: thanks, Yihui Xie, for knitr, and John MacFarlane for pandoc. Awesomeness! (Perhaps I could submit patch suggestions to either or both to work around for my use-case, though if it's just me then it might not be worthwhile.)
推荐答案
我认为您需要的所有信息都在?pandoc
中,其中包括运行 system("pandoc-h)
查看可能的输出格式.从中您了解到
I think all there information you need is there in ?pandoc
, which includes the example of running system("pandoc -h")
to see possible output formats. From that you learn that
输出格式:asciidoc,beamer,上下文,docbook,docx,dzslides,epub,epub3,fb2,html,html5,json,latex,man,markdown,markdown_github,markdown_mmd,markdown_phpextra,markdown_strict,mediawiki,本机,odt,opendocument,opml,org,pdf *,纯文本,reallyjs,rst,rtf,s5,slideous,slidy,texinfo,纺织品[*对于pdf输出,请使用乳胶或投影仪,以及-o FILENAME.pdf]
Output formats: asciidoc, beamer, context, docbook, docx, dzslides, epub, epub3, fb2, html, html5, json, latex, man, markdown, markdown_github, markdown_mmd, markdown_phpextra, markdown_strict, mediawiki, native, odt, opendocument, opml, org, pdf*, plain, revealjs, rst, rtf, s5, slideous, slidy, texinfo, textile [*for pdf output, use latex or beamer and -o FILENAME.pdf]
因此,基本上 format ="pdf"
是无效的,您应该使用 pandoc("tmp.Rmd",format ="latex",ext ="pdf")
(根据?pandoc
的描述,默认为 ext ="pdf"
部分,因此,您真正需要的只是 pandoc("tmp.Rmd","latex")
).至于为什么 pandoc('whitepaper.Rmd','pdf')
导致使用 -t pdf
进行调用,那么,您告诉它在第二个参数中执行此操作到您的 pandoc()
调用.
So basically format = "pdf"
is invalid, you should use pandoc("tmp.Rmd", format = "latex", ext = "pdf")
(and acutally the ext="pdf"
part is the default, according to ?pandoc
, so all you really need is pandoc("tmp.Rmd", "latex")
). As for why pandoc('whitepaper.Rmd', 'pdf')
resulted in a call with -t pdf
, well, you told it to do that in the second argument to your pandoc()
call.
这篇关于knitr pandoc:“无法使用pdf编写器生成pdf输出"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!