我正在尝试使用knitr和pandoc转换为PDF时控制绘图的位置。我的.Rmd文件看起来像这样:
# My report
Some text some text some text some text some text some text some text some text some text
```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'}
library(ggplot2)
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
Some text some text some text some text some text some text some text some text some text
\usepackage{graphicx}
\begin{figure}[placeHere]
\centering
\includegraphics[width=0.5\textwidth]{placeHere}
\end{figure}
Some text some text some text some text some text some text some text some text some text
我正在使用此处提供的功能转换为PDF:http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html
如何在第二和第三个文本块之间放置图?乳胶代码目前无效。
编辑:这就是我现在正在尝试的。
# My report
```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=FALSE)
library(ggplot2)
```
```{r, echo=FALSE, fig.height=3}
ggplot(mtcars, aes(disp, hp)) + geom_point()
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
```{r, echo=FALSE, fig.height=3}
ggplot(mtcars, aes(vs, am)) + geom_point()
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
```{r, echo=FALSE, fig.height=6}
ggplot(mtcars, aes(disp, cyl)) + geom_point()
```
```{r, echo=FALSE, fig.height=6}
ggplot(mtcars, aes(hp, qsec)) + geom_point()
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
```{r, echo=FALSE, fig.height=3}
ggplot(mtcars, aes(hp, wt)) + geom_point()
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
```{r, echo=FALSE, fig.height=5}
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
答案 0 :(得分:68)
我提出了另一种解决方案。我建议不要以事后方式将[H]符号插入到乳胶文档中,而是重新定义图形环境以忽略任何位置参数并使用[H]。
为此,请在重定义图形环境的.Rmd文件所在的目录中创建.tex文件,并在.Rmd中更新YAML标头以在编译期间包含该文件。
以下是.tex文件的示例:
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
以下是包含它的示例.Rmd(假设您调用.tex文件' preamble-latex.tex'):
---
title: "example"
author: "you"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
rmarkdown::pdf_document:
fig_caption: yes
includes:
in_header: preamble-latex.tex
---
```{r, fig.cap='Markdownvellous!'}
plot(1:10, 1:10)
```
答案 1 :(得分:25)
在将Markdown文档转换为LaTeX时,我不知道pandoc
设置浮动数字选项的选项。如果您选择Markdown的简单性,那么即使使用强大的工具pandoc
,也不应期望它具有太大的威力。底线: Markdown不是LaTeX 。它是为HTML而不是LaTeX而设计的。
两种方法:
使用Rnw语法(R + LaTeX)代替Rmd(R Markdown)(examples);然后你可以在序言中fig.pos='H'
之后使用块选项\usepackage{float}
;在这种情况下,您拥有LaTeX的全部功能,并且不会涉及pandoc
破解pandoc生成的LaTeX文档,例如:
之类的东西library(knitr)
knit('foo.Rmd') # gives foo.md
pandoc('foo.md', format='latex') # gives foo.tex
x = readLines('foo.tex')
# insert the float package
x = sub('(\\\\begin\\{document\\})', '\\\\usepackage{float}\n\\1', x)
# add the H option for all figures
x = gsub('(\\\\begin\\{figure\\})', '\\1[H]', x)
# write the processed tex file back
writeLines(x, 'foo.tex')
# compile to pdf
tools::texi2pdf('foo.tex') # gives foo.pdf
如果您不喜欢这些解决方案,请在Github上考虑requesting a new feature pandoc,然后坐下来等待。
答案 2 :(得分:10)
我在RSTUDIO中使用KnitR和markdown,我案例的解决方案是在序言\usepackage{float}
中添加:
---
title: "Proyect 2"
author: "FV"
date: "2016-12-3"
output:
pdf_document:
fig_caption: yes
fig_crop: no
fig_height: 2
fig_width: 3
highlight: haddock
keep_tex: yes
number_sections: yes
toc: yes
toc_depth: 2
html_document:
fig_caption: yes
theme: journal
toc: yes
toc_depth: 2
header-includes:
- \usepackage{graphicx}
- \usepackage{float}
---
然后在第一行添加这行代码(fig.pos ='H'):
```{r echo=FALSE,warning=FALSE}
library(knitr)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
external=TRUE,
echo=TRUE,
warning=FALSE,
fig.pos='H'
)
a4width<- 8.3
a4height<- 11.7
```
答案 3 :(得分:6)
我有一些项目,我将.Rmd转换为.pdf(主要是beamer幻灯片演示文稿)并希望图形不浮动(浮动数字实际上不适用于幻灯片演示)。
我使用的方法是在.md文件中的行之后添加一个转义空格。这意味着图形在一个段落内而不是它自己的一个段落,这意味着pandoc不会将它包装在一个图形环境中(这也意味着我不能使用它的标题)并因此将其准确放置那个位置。
我使用Makefile为我做所有的转换,所以在运行R和knitr后,它会自动运行一个Perl脚本(尽管可以使用R或其他工具完成),找到插图的位置并添加在线的末端逃脱了空间。
答案 4 :(得分:5)
如果您正在寻找的只是手动控制您的数字放置位置,请使用此网页:http://www.rci.rutgers.edu/~ag978/litdata/figs/,我发现如果您添加一个反斜杠&#34; \&#34;在绘图命令后的某个地方,绘图不会浮动,而是打印在当前位置。
如果您只想显示一些图表,则可以为每个图表修改该选项。
在你的例子中:
# My report
```{r setup, include=FALSE}
# set global chunk options
knitr::opts_chunk$set(cache=FALSE)
library(ggplot2)
```
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
```{r, echo=FALSE, fig.height=3}
ggplot(mtcars, aes(disp, hp)) + geom_point()
```
\
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
(等)
答案 5 :(得分:2)
我在某种程度上偶然发现了这个问题并希望添加另一种方法。在这里,我利用了针织钩提供的令人敬畏的灵活性。我只需更改绘图钩子即可使用knitr函数hook_plot_tex()
。之后我可以像在Rnw文档中一样使用块选项fig.pos
来定位figure
环境(必须设置fig.cap
才能调用figure
环境)。
这适用于OP提供的示例。我猜他们也在(不知何故)更复杂的例子中工作。为什么这可以轻松完成,而不是Rmd文档的默认值,我不确定。也许易辉可以澄清一下。
以下是代码:
---
title: "Example"
author: "Martin"
output: pdf_document
---
```{r}
knitr::knit_hooks$set(plot = function(x, options) {
hook_plot_tex(x, options)
})
```
```{r myplot, echo=FALSE, results='hide', fig.cap='Test', fig.pos='h'}
library(ggplot2)
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
如果没有fig.pos='h'
,情节通常会跳到第二页。
答案 6 :(得分:0)
这就是你所追求的:
```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=FALSE)
library(ggplot2)
```
# My report
Some text some text some text some text some text some text some text some text some text
Some text some text some text some text some text some text some text some text some text
```{r myplot, echo=FALSE}
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
Some text some text some text some text some text some text some text some text some text
答案 7 :(得分:0)
解决方案不是太简单,也许其他人可以简化它。
基本步骤。 (Windows 7)
您可以将参数fig.pos="H"
添加到knitr选项中,无论是全局还是每个单独的块。注意大写字母H.这指示乳胶将图形浮动完全放置在Rmd
文件中的位置。
但是,这需要latex使用包,您可以在pandoc用于构造pdf文件的模板中指定。您可以通过添加第\usepackage{float}
行
但是,您首先需要找到要修改的当前模板文件。我无法在任何地方找到它,但您可以使用此命令获取pandoc将模板的内容打印到控制台:pandoc -D latex
将此模板代码剪切并粘贴到空文本文件中。
添加以下行:\usepackage{float}
在"default.latex"
C:\Users\YOURNAME\pandoc\templates
下保存
如果在R中使用平移器,则将选项--data-dir=C:/Users/YOURNAME/pandoc/templates"
添加到您对pandoc OR Pandoc.convert("my file.md", format="pdf", options=c("--data-dir=C:/Users/YOURNAME/pandoc/templates"))
的调用中。
我希望这适合你。