书本中的knitr :: include_graphics未呈现图像 [英] knitr::include_graphics in bookdown, is not rendering the image
问题描述
我正在尝试使用Bookdown渲染的文件中包含.png文件. knitr::include_graphics()
应该是要走的路.
I am trying to include a .png file in a file that I am rendering using bookdown. knitr::include_graphics()
should be the way to go.
代码:
```{r fig1, fig.cap='My Caption', echo=FALSE, message=FALSE, warning=FALSE}
knitr::include_graphics("./Figures/My Figure.png")
```
在.Rmd文件中,我可以运行我的r块,它呈现下面的图像.因此,路径应该是正确的.但是,当我编织章节或渲染整本书时,该图不会被渲染.
In the .Rmd file, I can run my r block, and it renders the image below. So, the path should be correct. However, when I knit the chapter, or render the entire book, the figure is not rendered.
难道是我的其他一些选择超越了这个数字吗?以下是我的index.Rmd文件的YAML标头,以及_output.yml文件中的代码.
Could it be that some of my other options are overriding the figure? Below are my YAML header of the index.Rmd file, and the code in my _output.yml file.
---
title: "My Title"
author: "My Name"
date: "`r Sys.Date()`"
output:
bookdown::gitbook:
split_by: section
bookdown::pdf_book:
keep_tex: no
documentclass: book
classoption: openany
bibliography: [Mybib.bib]
csl: Mycsl.csl
biblio-style: apalike
link-citations: yes
description: "My Description"
---
bookdown::gitbook:
config:
toc:
before: |
<li><a href="./">Short Title</a></li>
bookdown::pdf_book:
latex_engine: xelatex
citation_package: natbib
bookdown::epub_book: default
推荐答案
图像文件路径中有空格.
文件名中的空格在R中不是问题,这就是为什么您能够在RStudio中看到图像.但是,knitr稍微复杂一点,因为它实际上执行许多不同的程序(R,pandoc和LaTeX).
Spaces in file names are not a problem within R, and that is why you were able to see the image within RStudio. However, knitr is a bit more complex as it actually executes a number of different programs (R, pandoc & LaTeX).
如 knitr 的指导中所述,\includegraphics{}
插入图片.如此处所述:
As explained within the guidance to knitr, the knitr:: includegraphics
function actually executes different results depending on whether the output is HTML, PDF or md. When building a PDF, it is passing the images through LaTeX and uses the function \includegraphics{}
to insert the picture. As explained here:
图像的文件名不得包含空格或多个点"
"The file name of the image should not contain white spaces nor multiple dots"
删除空格将解决此问题,即使在技术上允许,通常也应避免在R中使用空格.
Removing spaces will fix the problem, and it would generally be good practise to avoid them in R even if they technically are allowed.
向 @pzhao 致意,以首先突出问题所在.
Credit to @pzhao for initially highlighting the problem.
这篇关于书本中的knitr :: include_graphics未呈现图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!