R knitr中使用\includegraphics出现PDF问题

18

使用一个新的空白.rmd文档,这段代码可以工作:

![](RainbowDolphin.png)
\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}

但是没有第一行,它就不行:

\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}

我遇到一个错误:

! Undefined control sequence.
l.71 \includegraphics

pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" Sampling_03.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Sampling_03.pdf --template "C:\PROGRA~1\R\R-31~1.2\library\RMARKD~1\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43 
Execution halted

很奇怪,需要任何帮助!

2个回答

14

这是因为latex模板默认没有加载graphicx包。您可以通过将以下内容添加到yaml头文件来手动加载它:

---
title: "Untitled"
header-includes: \usepackage{graphicx}
output: 
    pdf_document:
        keep_tex: true
---

13
你可以通过指定YAML元数据来告诉Pandoc这个文档中包含图形信息:
---
graphics: yes
---

2
值得一提的是,这已经成为rmarkdown的默认行为,因此即使在YAML中没有graphics: yes,它也可以正常工作:https://github.com/rstudio/rmarkdown/pull/538 这个变化将出现在下一个版本的rmarkdown中。 - Yihui Xie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接