R knitr PDF problems with \includegraphics

放肆的年华 提交于 2019-12-12 07:25:24

问题


Using a new empty .rmd document, this code works:

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

But without the first line, it doesn't:

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

I get an error:

! 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

Weird. Any help appreciated!


回答1:


It's because the latex template doesn't load the graphicx package by default. You can do it manually by adding this to the yaml header:

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



回答2:


You may let Pandoc know that you have graphics in this document by specifying the YAML metadata:

---
graphics: yes
---


来源:https://stackoverflow.com/questions/28266473/r-knitr-pdf-problems-with-includegraphics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!