Ban*_*you 31 r knitr r-markdown
我正在使用knitr(1.9.5和1.9.17)和rmarkdown(0.5.3.1),并希望在pdf输出中保持数字位置.fig.pos="H"
使用chunk选项时,生成的pdf文件正常工作.
但是,fig_caption: yes
在yaml标题中设置时,图形位置不会保持.
我该如何解决这个问题?谢谢你的任何建议.
编辑:
在了解乳胶的漂浮环境之后.我将float
包添加到标题中.
\usepackage{float}
Run Code Online (Sandbox Code Playgroud)
但生成的tex文件总是htbp
在figure
环境fig.pos
中使用任何选项.手动切换htbp
到之后H
,所有数字的位置都保持不变.
这是我的rmd文件示例:
---
title: "Untitled"
output:
pdf_document:
fig_caption: yes
includes:
in_header: mystyles.sty
---
# Section 1
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r fig1, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
# Section 2
More test
```{r fig2, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
# Section 3
```{r fig3, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
More test
Run Code Online (Sandbox Code Playgroud)
Dav*_*vic 19
正如安德鲁指出的那样,这fig.pos
在块中不起作用,但如果将它放在全局选项中它确实有效:
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H')
```
Run Code Online (Sandbox Code Playgroud)
use*_*767 16
对我来说,添加float
包然后\floatplacement{figure}{H}
在YAML中解决了以下问题:
---
title: "test"
date: "`r Sys.Date()`"
output:
pdf_document :
keep_tex: true
number_sections: true
header-includes:
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{wrapfig}
\usepackage{float}
\floatplacement{figure}{H}
---
Run Code Online (Sandbox Code Playgroud)
小智 13
更新看这个更好的解决方案在这里.(以下问题的摘要仍然很好,但请按照更好的解决方案链接).
总结一下RStudio中的一些测试
只要fig_caption: yes
不在yaml标题中,knitr chunk参数fig.pos ="H"就可以工作.
生成的.tex中的每个数字都是这样的
\subsection{my_section}\label{my_section}
\includegraphics{path_to_fig.pdf}
Run Code Online (Sandbox Code Playgroud)
但如果fig_caption: yes
在yaml标题中,则.tex看起来像这样
\subsection{my_section}\label{my_section}
\begin{figure}[htbp]
\centering
\includegraphics{path_to_fig.pdf}
\caption{}
\end{figure}
Run Code Online (Sandbox Code Playgroud)
fig.pos = "H"
没有被使用,"htbp"
是否有.
使用RStudio的解决方法:
放
fig_caption: yes
keep_tex: yes
Run Code Online (Sandbox Code Playgroud)
在yaml以及
header-includes: \usepackage{float}
Run Code Online (Sandbox Code Playgroud)
然后搜索和替换[htbp]
用[H]
在生成的.tex文件
然后在RStudio中打开.tex文件并使用"编译PDF"按钮.
示例.Rmd
---
title: "Testing fig placement with captions"
author: "Andrew Dolman"
date: "1 September 2015"
output:
pdf_document:
fig_caption: yes
keep_tex: yes
header-includes: \usepackage{float}
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE, fig.pos="H"}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r, echo=FALSE, fig.pos="H"}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)
小智 8
虽然@Bangyou提供的答案有效,但它使针织变得复杂.
您还可以在latex中为数字放置设置全局默认选项,包括在YAML标题中包括:
\makeatletter\renewcommand*{\fps@figure}{H}\makeatother
Run Code Online (Sandbox Code Playgroud)
这样你就可以使用RStudio中的编织按钮或rmarkdown :: render来完成它.
问题是,所有数字都用H强制填充,你将无法设置一个浮动.
使用knitr和pandoc转换为PDF时Markdown中图位置的代码帮助我,帮助其他人发现它有用。
---
title: "Example"
author: "Martin"
output: pdf_document
---
```{r}
knitr::knit_hooks$set(plot = function(x, options) {
knitr::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()
```
Run Code Online (Sandbox Code Playgroud)
正如 Yihui 在他的回答中提到的(使用 knitr 和 pandoc 转换为 PDF 时 Markdown 中的图形位置),我们不能对 mardown 的格式期望太多。要解决这一问题,只是写一些脚本[R替换htbp
到H
。
与knit
from knitr 包相比,我发现render
从 rmarkdown 导出tex
文件更好。请记住keep_tex: yes
在 rmarkdown 文件的 yaml 标头中添加。
library(rmarkdown)
render('filepath.Rmd')
x <- readLines('filepath.tex')
pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
x[pos] <- gsub('htbp', 'H', x[pos])
writeLines(x, 'filepath.tex')
tools::texi2pdf('filepath.tex', clean = TRUE) # gives foo.pdf
file.remove('filepath.tex')
Run Code Online (Sandbox Code Playgroud)
对我有用的选项:
在.tex把开头:\usepackage{float}
。
Rmd开头:knitr::opts_chunk$set(fig.pos = 'H')
。在H
为大写)。
并且在每个带有图像的块中:fig.cap="lorem blabla"
和out.extra=''
(参数值=空字符串)。
无需定义:
fig_caption: yes
并且keep_tex: yes
在yaml中。
这些选项使图像保持其位置,无论是对于include_graphics
还是由R代码生成的图。
我在bookdown
环境中使用它们,生成了预期的pdf和html :)
归档时间: |
|
查看次数: |
38735 次 |
最近记录: |