为什么 Rmd 不呈现为 pdf 而 html 很好 [英] Why does Rmd not render to pdf while html is fine

查看:66
本文介绍了为什么 Rmd 不呈现为 pdf 而 html 很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将以下文件呈现为 html,则一切正常.如果我渲染为 pdf,它会抛出错误

If I render the following file to html, everything works. If I render to pdf, it throws the error

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

output file: test.knit.md ! LaTeX Error: Unknown graphics extension: .png?raw=true. Fehler: Failed to compile test.tex. See test.log for more info.

原因是,Rmd被翻译成

The reason is, that the Rmd is translated to

\begin{figure}
\centering
\includegraphics[width=4.16667in]{pics/myimage.png?raw=true}
\caption{Some text here.}
\end{figure}

在 test.tex 和上面的代码当然没有意义.
示例:

in test.tex and above code does not make sense of course.
Example:

---
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

我需要 github 的 ?raw=TRUE,请参阅 此处.您还可以从此处复制示例!

I need the ?raw=TRUE for github, see here. You can also clone the example from here!

编辑:我还在这里询问了开发人员,因为我有感觉,keep_md:yes...

Edit: I also asked the developers here, because I have the feeling, something goes wrong with keep_md: yes...

推荐答案

您可以通过 knitr::is_html_output() 对输出格式的图像路径进行条件处理,例如,

You can condition the image path on the output format via knitr::is_html_output(), e.g.,

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

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

text

```{r, echo=FALSE, results='asis'}
cat(
  '![Some text here.](pics/myimage.png',
  if (knitr::is_html_output()) '?raw=true',
  '){width=400px}',
  sep = ''
)
```

text

这篇关于为什么 Rmd 不呈现为 pdf 而 html 很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆