我正在使用knitr(1.9.5和1.9.17)和rmarkdown(0.5.3.1),并希望在pdf输出中保持数字位置.使用区块选项fig.pos="H"时,生成的pdf文件工作正常.

但是,当在中设置fig_caption: yes时,图形位置不会保持不变

我应该如何解决这个问题?谢谢你的建议.

编辑:

在学习了latex 的漂浮环境之后.我将float个包裹添加到标题中.

\usepackage{float}

但是,生成的tex文件在figure环境中始终使用htbp,因为使用了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

推荐答案

正如Yihui在他的回答(Figure position in markdown when converting to PDF with knitr and pandoc)中提到的,我们不能对mardown的格式有太多期望.为了解决这个问题,只需编写一些R脚本来替换htbpH.

与knitr包中的knit相比,我发现rmarkdown中的render更适合导出tex文件.只需记住在rmarkdown文件的yaml头中添加keep_tex: yes.

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

R相关问答推荐

R -列表元素中所有命名项的总和

将R data.frame转换为json数组(源代码)

R -模运算后的加法

如何判断某列中由某些行组成的百分比

以R为基数排列奇数个图

根据shiny 应用程序中的数字输入更改图标 colored颜色

geom_Ribbon条件填充创建与数据不匹配的形状(ggplot 2 r)

根据收件箱中的特定值提取列名

根据R中两个变量的两个条件删除带有dspirr的行

大规模重新标记haven标签数据

如何从R中的字符串元素中减go 一个数字?

将嵌套列表子集化为嵌套列表

在R中为马赛克图中的每个字段着色

如何改变x轴比例的列在面

SHINY:使用JS函数应用的CSS样式显示HTML表格

用两种 colored颜色 填充方框图

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

将列的值乘以在不同数据集中找到的值

生存时间序列的逻辑检验

抽样变换-REXP与RWEIBUR