为什么在HTML没问题的情况下Rmd无法呈现为pdf

时间:2019-04-09 14:47:16

标签: r-markdown

如果我将以下文件渲染为html,则一切正常。 如果我渲染为pdf,则会引发错误

  

输出文件:test.knit.md   ! LaTeX错误:未知图形扩展名:.png?raw = true。   Fehler:无法编译test.tex。有关更多信息,请参见test.log。

原因是Rmd被翻译为

\begin{figure}
\centering
\includegraphics[width=4.16667in]{pics/myimage.png?raw=true}
\caption{Some text here.}
\end{figure}
在test.tex及以上代码中的

毫无意义。
示例:

---
title: "Untitled"
author: "Myname"
date: "5 April 2019"
output:
  pdf_document:
    number_sections: yes
    toc: yes
    toc_depth: '2'
  html_document:
    keep_md: yes
    number_sections: yes
    toc: yes
    toc_depth: 2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

text

![Some text here.](pics/myimage.png?raw=true){width=400px}

text

我需要?raw=TRUE用于github,请参阅here。 您还可以从here克隆示例!

编辑:我还问开发者here,因为我有感觉keep_md: yes出了点问题...

2 个答案:

答案 0 :(得分:2)

您可以通过for (auto it = v1.cbegin(); it != v1.end(); ++it) { std::cout << v1[i]; } for (auto i = 0u; i < v2.size(); ++i) { std::cout << v1[i]; } for (auto const i: v3) { std::cout << i; } (例如,

)在输出格式上调节图像路径
knitr::is_html_output()

答案 1 :(得分:1)

我将使用以下解决方法:

  1. 在您的.rmd中删除部分?raw=true
  2. 编织到 html 后:将html文件读入R,将.png替换为.png?raw=true并再次存储:

您可以使用以下代码:

html <- readLines("your-file.html")
html <- sapply(html, function(x) gsub(".png", ".png?raw=true", x))
writeLines(html, "your-file.html")

就像您在html文件(它们所属的位置)中有?raw=true注释,而不是在.tex文件(以及.pdf)中没有含义的{{1}}注释一样