如何在knitr的pdf输出中用数字标题保持数字位置?

时间:2021-10-16 06:41:22

I am using knitr (1.9.5 and 1.9.17) and rmarkdown (0.5.3.1), and would like to hold figure position in the pdf output. The generated pdf file is working fine when chunk option fig.pos="H" is used.

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

However, the figure position is not hold when fig_caption: yes is set in the yaml header.

但是,当在yaml标头中设置fig_caption:yes时,数字位置不成立。

How should I fix this problem? Thanks for any suggestions.

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

EDIT:

编辑:

After learning the float environment of Latex. I add float package into header.

在学习了乳胶的漂浮环境之后。我将float包添加到头文件中。

\usepackage{float}

But the generated tex file always use htbp in the figure environment regard to any fig.pos options are used. After manually changing htbp to H, positions of all figures are hold.

但是生成的tex文件总是在图形环境中使用htbp,而不是使用任何fig.pos选项。手动将htbp更改为H后,所有数字的位置都保持不变。

This is my example of rmd file:

这是我的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

5 个解决方案

#1


13  

As Andrew pointed out, this fig.pos doesn't work in chunks, but it does work if it is put in global options:

正如安德鲁所指出的那样,这个fig.pos在块中不起作用,但如果将它放在全局选项中它确实有效:

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H')
```

#2


10  

Update look at this better solution here. (the summary of the problem below is still good, but follow the link to a better solution).

这里更新一下这个更好的解决方案。 (以下问题的摘要仍然很好,但请按照更好的解决方案链接)。

To summarise some testing in RStudio

总结一下RStudio中的一些测试

The knitr chunk argument fig.pos = "H" works as long as fig_caption: yes is not in the yaml header.

knitr chunk参数fig.pos =“H”与fig_caption一样长:yes不在yaml标题中。

Each figure in the generated .tex looks like this

生成的.tex中的每个数字都是这样的

\subsection{my_section}\label{my_section}

\includegraphics{path_to_fig.pdf}

But if fig_caption: yes is in the yaml header then the .tex looks like this

但是如果fig_caption:是在yaml标题中,则.tex看起来像这样

\subsection{my_section}\label{my_section}

\begin{figure}[htbp]
\centering
\includegraphics{path_to_fig.pdf}
\caption{}
\end{figure}

fig.pos = "H" has not been used, "htbp" is there instead.

fig.pos =“H”尚未使用,“htbp”代替。

A workaround for this using RStudio:

使用RStudio的解决方法:

put

fig_caption: yes
keep_tex: yes

in the yaml as well as

在yaml以及

header-includes: \usepackage{float}

then search and replace [htbp] with [H] in the generated .tex file

然后在生成的.tex文件中用[H]搜索并替换[htbp]

then open the .tex file in RStudio and use the "Compile PDF" button.

然后在RStudio中打开.tex文件并使用“编译PDF”按钮。


Example .Rmd

示例.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)
```

#3


3  

As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp to H.

正如Yihui在他的回答中所提到的(使用knitr和pandoc转换为PDF时的降价中的数字位置),我们不能期望太多关于mardown的格式化。要解决此问题,只需编写一些R脚本将htbp替换为H.

Compared with knit from knitr package, I found render from rmarkdown is better to export a tex file. Just remember to add keep_tex: yes in the yaml header of your rmarkdown file.

与knitr包的knit相比,我发现从rmarkdown渲染更好地导出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')

#4


2  

Although the answer provided by @Bangyou works, it complicates knitting.

虽然@Bangyou提供的答案有效,但它使针织变得复杂。

You can also set a global default option for figure placement in latex, including this in your YAML header includes:

您还可以为latex中的图形放置设置全局默认选项,包括在YAML标题中的此选项包括:

\makeatletter\renewcommand*{\fps@figure}{H}\makeatother

As explained here (and here for the \makeat... part).

正如这里所解释的(这里是\ makeat ...部分)。

This way you can just use the knit button in RStudio or rmarkdown::render and be done with it.

这样你就可以使用RStudio中的编织按钮或rmarkdown :: render来完成它。

Issue is, all figures fill be forced with H and you won't be able to set one to float around.

问题是,所有数字都用H强制填充,你将无法设置一个浮动。

#5


2  

The option that worked for me:

对我有用的选项:

In the .tex put at the beginning: \usepackage{float}.

在.tex放在开头:\ usepackage {float}。

At the beginning of the Rmd: knitr::opts_chunk$set(fig.pos = 'H'). The H in upper case).

在Rmd的开头:knitr :: opts_chunk $ set(fig.pos ='H')。大写的H)。

And in every chunk with an image: fig.cap="lorem blabla" and out.extra='' (parameter value=empty string).

并且在每个带有图像的块中:fig.cap =“lorem blabla”和out.extra =''(参数值=空字符串)。

No need to define: fig_caption: yes and keep_tex: yes in the yaml.

无需定义:fig_caption:yes和keep_tex:是的yaml。

These options make the image to keep their position, either for include_graphics and the plots generated by R code.

这些选项使图像保持其位置,包括include_graphics和R代码生成的图。

I used them in the bookdown enviroment, generating the pdf and the html as expected :)

我在bookdown环境中使用它们,按预期生成pdf和html :)

#1


13  

As Andrew pointed out, this fig.pos doesn't work in chunks, but it does work if it is put in global options:

正如安德鲁所指出的那样,这个fig.pos在块中不起作用,但如果将它放在全局选项中它确实有效:

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H')
```

#2


10  

Update look at this better solution here. (the summary of the problem below is still good, but follow the link to a better solution).

这里更新一下这个更好的解决方案。 (以下问题的摘要仍然很好,但请按照更好的解决方案链接)。

To summarise some testing in RStudio

总结一下RStudio中的一些测试

The knitr chunk argument fig.pos = "H" works as long as fig_caption: yes is not in the yaml header.

knitr chunk参数fig.pos =“H”与fig_caption一样长:yes不在yaml标题中。

Each figure in the generated .tex looks like this

生成的.tex中的每个数字都是这样的

\subsection{my_section}\label{my_section}

\includegraphics{path_to_fig.pdf}

But if fig_caption: yes is in the yaml header then the .tex looks like this

但是如果fig_caption:是在yaml标题中,则.tex看起来像这样

\subsection{my_section}\label{my_section}

\begin{figure}[htbp]
\centering
\includegraphics{path_to_fig.pdf}
\caption{}
\end{figure}

fig.pos = "H" has not been used, "htbp" is there instead.

fig.pos =“H”尚未使用,“htbp”代替。

A workaround for this using RStudio:

使用RStudio的解决方法:

put

fig_caption: yes
keep_tex: yes

in the yaml as well as

在yaml以及

header-includes: \usepackage{float}

then search and replace [htbp] with [H] in the generated .tex file

然后在生成的.tex文件中用[H]搜索并替换[htbp]

then open the .tex file in RStudio and use the "Compile PDF" button.

然后在RStudio中打开.tex文件并使用“编译PDF”按钮。


Example .Rmd

示例.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)
```

#3


3  

As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp to H.

正如Yihui在他的回答中所提到的(使用knitr和pandoc转换为PDF时的降价中的数字位置),我们不能期望太多关于mardown的格式化。要解决此问题,只需编写一些R脚本将htbp替换为H.

Compared with knit from knitr package, I found render from rmarkdown is better to export a tex file. Just remember to add keep_tex: yes in the yaml header of your rmarkdown file.

与knitr包的knit相比,我发现从rmarkdown渲染更好地导出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')

#4


2  

Although the answer provided by @Bangyou works, it complicates knitting.

虽然@Bangyou提供的答案有效,但它使针织变得复杂。

You can also set a global default option for figure placement in latex, including this in your YAML header includes:

您还可以为latex中的图形放置设置全局默认选项,包括在YAML标题中的此选项包括:

\makeatletter\renewcommand*{\fps@figure}{H}\makeatother

As explained here (and here for the \makeat... part).

正如这里所解释的(这里是\ makeat ...部分)。

This way you can just use the knit button in RStudio or rmarkdown::render and be done with it.

这样你就可以使用RStudio中的编织按钮或rmarkdown :: render来完成它。

Issue is, all figures fill be forced with H and you won't be able to set one to float around.

问题是,所有数字都用H强制填充,你将无法设置一个浮动。

#5


2  

The option that worked for me:

对我有用的选项:

In the .tex put at the beginning: \usepackage{float}.

在.tex放在开头:\ usepackage {float}。

At the beginning of the Rmd: knitr::opts_chunk$set(fig.pos = 'H'). The H in upper case).

在Rmd的开头:knitr :: opts_chunk $ set(fig.pos ='H')。大写的H)。

And in every chunk with an image: fig.cap="lorem blabla" and out.extra='' (parameter value=empty string).

并且在每个带有图像的块中:fig.cap =“lorem blabla”和out.extra =''(参数值=空字符串)。

No need to define: fig_caption: yes and keep_tex: yes in the yaml.

无需定义:fig_caption:yes和keep_tex:是的yaml。

These options make the image to keep their position, either for include_graphics and the plots generated by R code.

这些选项使图像保持其位置,包括include_graphics和R代码生成的图。

I used them in the bookdown enviroment, generating the pdf and the html as expected :)

我在bookdown环境中使用它们,按预期生成pdf和html :)