I want to export a .Rmd file primarily as a latex pdf.
我想导出一个。rmd文件,主要是作为一个乳胶pdf。
This is the code that I'm currently using
这是我目前使用的代码
```{r ,fig.cap="caption",fig.env='figure', fig.width=10, fig.height=10,echo=FALSE, results='asis', warning=FALSE, strip.white=TRUE}
library(png)
library(grid)
img <- readPNG("filepath/overview.png")
grid.raster(img)
```
As you can see, I'm already using strip.white=TRUE
& fig.env='figure'
but they don't seem to work. The .PNG file hasn't got any (white) spacing above or below the image.
正如你看到的,我已经在用长条了。白色=TRUE & fig.env= " figure ",但它们似乎并不奏效。png文件在图像上方或下方没有任何(白色)间距。
I know I can use latex directly and achieve what I want, but I want to able to reproduce this in Word if needed. Also in Word, there's half a page empty space above and below the image.
我知道我可以直接使用latex来实现我想要的东西,但是如果需要的话,我希望能够在Word中重现这些东西。同样,在图片上下有半页的空白。
Any help would be greatly appreciated. Thanks
如有任何帮助,我们将不胜感激。谢谢
1 个解决方案
#1
1
Your problem is not linked with knitr but with the raster image, which produces a white edge to keep it from being distorted. For instance, if you type ? graphics::plot.raster
you will see an asp
argument set to 1 retain the ratio from the raster. Plot your image in a standard R output you will see the blank parts, if you adapt the window, those white parts are removed. So what you need is to check your image dimensions and then use a fig.asp
ratio in knitr to produce a frame that will allow your image to fit.
你的问题不是与knitr有关,而是与光栅图像有关,光栅图像产生白色边缘,以防止它被扭曲。例如,如果你打字?图形:情节。你会看到一个设置为1的asp参数保留来自光栅的比率。在标准的R输出中绘制你的图像,你会看到空白部分,如果你调整窗口,那些白色的部分被删除。所以你需要的是检查你的图像尺寸,然后在knitr中使用一个fig.asp比率来产生一个允许你的图像适合的框架。
Check your image ratio
Example using magick
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
image<- image_read(url)
print(image)
this returns
这将返回
format width height colorspace filesize
1 PNG 960 600 sRGB 762423
You can also use readPNG()
还可以使用readPNG()
curl::curl_download(url, "image.png")
image <- png::readPNG("image.png",info=TRUE)
attr(image,"info")
From knitr options we have the parameter fig.asp
从knitr选项中,我们得到了参数fig.asp
fig.asp: (NULL; numeric) the aspect ratio of the plot, i.e. the ratio of height/width; when fig.asp is specified, the height of a plot (the chunk option fig.height) is calculated from fig.width * fig.asp
fig.asp:(零;数字)图的纵横比,即高度/宽度的比值;当指定fig.asp时,从fig.width * fig.asp中计算出一个plot(块选项fig.height)的高度
So here we calculate height/width
= 0.62.
这里我们计算高度/宽度= 0。62。
Using knitr and docx output
Here I'm using a square output passed as an opts.label
argument set in the first chunk, this will amplify the problem when the image is wide.
这里我使用的是作为opts的平方输出。在第一个块中设置label参数,当图像很宽时,这会放大问题。
---
title: "Crop image ?"
output: word_document
---
```{r echo=FALSE}
require(knitr)
library(magick)
opts_template$set(squarefigure = list(fig.height = 6, fig.width = 6))
```
=lorem()
```{r opts.label ="squarefigure", echo=FALSE, fig.cap = "plot without setting the right raster output size"}
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
img <- image_read(url)
img%>%grid.raster()
```
=lorem()
```{r opts.label ="squarefigure", fig.cap = "plot with correct margin, square size", fig.asp=0.62}
img%>%grid.raster()
```
=lorem()
As you can see the first image has a blank edge while the second is diplayed correctly.
正如您所看到的,第一个图像有空白边,而第二个图像是正确的。
Using LATEX with trim
I know the answer for LATEX was not asked in the question, still if some readers are using knitr or sweave to produce a LATEX output then, the following shows how to trim an image whithin knitr. The arguments used are trim={<left> <lower> <right> <upper>
and the unit can be cm mm in ... (one of LATEX unit for length). To pass those arguments, you can use the out.extra
argument in the chunk options. Note that using the fig.asp
argument for your image like above will work too.
我知道这个问题没有问乳胶的答案,但是如果一些读者正在使用knitr或sweave来产生乳胶输出,下面将展示如何修剪一个图像。所使用的参数是trim={ <左> <下> <右> <上> ,单位在…(一个长度的乳胶单位)。要传递这些参数,可以使用out。块选项中的额外参数。注意,像上面这样使用fig.asp参数也可以。
\documentclass{article}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\blindtext
<<r1, echo=FALSE >>=
library(knitr)
library(ggplot2)
library(magick)
# download image to disk
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
curl::curl_download(url, "image.png")
img <- image_read(png::readPNG("image.png"))
plot(img)
@
\blindtext
\newpage
\blindtext
<<r2, echo=FALSE,out.extra='trim={0 5cm 0 5cm},clip' >>=
plot(img)
@
\blindtext
\end{document}
Using HTML
Here is an excellent blog also for understanding arguments like fig.retina
and out.width
这是一个很好的博客,也可以理解像fig.retina和out.width这样的论点
Finally the strip.white
argument is to remove blank lines of code. It will not resize your image.
最后地带。白色参数是删除空行代码。它不会调整你的图像大小。
strip.white: (TRUE; logical) whether to remove the white lines in the beginning or end of a source chunk in the output
地带。怀特:(真实;逻辑)是否在输出中的源块的开头或结尾删除白线
#1
1
Your problem is not linked with knitr but with the raster image, which produces a white edge to keep it from being distorted. For instance, if you type ? graphics::plot.raster
you will see an asp
argument set to 1 retain the ratio from the raster. Plot your image in a standard R output you will see the blank parts, if you adapt the window, those white parts are removed. So what you need is to check your image dimensions and then use a fig.asp
ratio in knitr to produce a frame that will allow your image to fit.
你的问题不是与knitr有关,而是与光栅图像有关,光栅图像产生白色边缘,以防止它被扭曲。例如,如果你打字?图形:情节。你会看到一个设置为1的asp参数保留来自光栅的比率。在标准的R输出中绘制你的图像,你会看到空白部分,如果你调整窗口,那些白色的部分被删除。所以你需要的是检查你的图像尺寸,然后在knitr中使用一个fig.asp比率来产生一个允许你的图像适合的框架。
Check your image ratio
Example using magick
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
image<- image_read(url)
print(image)
this returns
这将返回
format width height colorspace filesize
1 PNG 960 600 sRGB 762423
You can also use readPNG()
还可以使用readPNG()
curl::curl_download(url, "image.png")
image <- png::readPNG("image.png",info=TRUE)
attr(image,"info")
From knitr options we have the parameter fig.asp
从knitr选项中,我们得到了参数fig.asp
fig.asp: (NULL; numeric) the aspect ratio of the plot, i.e. the ratio of height/width; when fig.asp is specified, the height of a plot (the chunk option fig.height) is calculated from fig.width * fig.asp
fig.asp:(零;数字)图的纵横比,即高度/宽度的比值;当指定fig.asp时,从fig.width * fig.asp中计算出一个plot(块选项fig.height)的高度
So here we calculate height/width
= 0.62.
这里我们计算高度/宽度= 0。62。
Using knitr and docx output
Here I'm using a square output passed as an opts.label
argument set in the first chunk, this will amplify the problem when the image is wide.
这里我使用的是作为opts的平方输出。在第一个块中设置label参数,当图像很宽时,这会放大问题。
---
title: "Crop image ?"
output: word_document
---
```{r echo=FALSE}
require(knitr)
library(magick)
opts_template$set(squarefigure = list(fig.height = 6, fig.width = 6))
```
=lorem()
```{r opts.label ="squarefigure", echo=FALSE, fig.cap = "plot without setting the right raster output size"}
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
img <- image_read(url)
img%>%grid.raster()
```
=lorem()
```{r opts.label ="squarefigure", fig.cap = "plot with correct margin, square size", fig.asp=0.62}
img%>%grid.raster()
```
=lorem()
As you can see the first image has a blank edge while the second is diplayed correctly.
正如您所看到的,第一个图像有空白边,而第二个图像是正确的。
Using LATEX with trim
I know the answer for LATEX was not asked in the question, still if some readers are using knitr or sweave to produce a LATEX output then, the following shows how to trim an image whithin knitr. The arguments used are trim={<left> <lower> <right> <upper>
and the unit can be cm mm in ... (one of LATEX unit for length). To pass those arguments, you can use the out.extra
argument in the chunk options. Note that using the fig.asp
argument for your image like above will work too.
我知道这个问题没有问乳胶的答案,但是如果一些读者正在使用knitr或sweave来产生乳胶输出,下面将展示如何修剪一个图像。所使用的参数是trim={ <左> <下> <右> <上> ,单位在…(一个长度的乳胶单位)。要传递这些参数,可以使用out。块选项中的额外参数。注意,像上面这样使用fig.asp参数也可以。
\documentclass{article}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\blindtext
<<r1, echo=FALSE >>=
library(knitr)
library(ggplot2)
library(magick)
# download image to disk
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
curl::curl_download(url, "image.png")
img <- image_read(png::readPNG("image.png"))
plot(img)
@
\blindtext
\newpage
\blindtext
<<r2, echo=FALSE,out.extra='trim={0 5cm 0 5cm},clip' >>=
plot(img)
@
\blindtext
\end{document}
Using HTML
Here is an excellent blog also for understanding arguments like fig.retina
and out.width
这是一个很好的博客,也可以理解像fig.retina和out.width这样的论点
Finally the strip.white
argument is to remove blank lines of code. It will not resize your image.
最后地带。白色参数是删除空行代码。它不会调整你的图像大小。
strip.white: (TRUE; logical) whether to remove the white lines in the beginning or end of a source chunk in the output
地带。怀特:(真实;逻辑)是否在输出中的源块的开头或结尾删除白线