使用knitr和pandoc转换为PDF时,在markdown中显示数字位置

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

I'm trying to control the position of a plot when converting to PDF using knitr and pandoc. My .Rmd file looks this:

我试图控制一个地块的位置,当转换为PDF时,使用knitr和pandoc。我的.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

I'm converting to PDF using the functions provided here: http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html

我正在使用这里提供的函数将其转换为PDF: http://quantifyingmemory.blogspot.co.uk3/02/re想来研究-with-r-knitr.html

How can I place the plot between the second and third blocks of text? The latex code is not working as it currently stands.

我如何把情节放在第二和第三段文字之间?乳胶代码不能正常工作。

EDIT: This is what I'm trying now.

编辑:这就是我正在尝试的。

# 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 

8 个解决方案

#1


24  

I'm not aware of such an option for pandoc to set the floating option of figures when converting a Markdown document to LaTeX. If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like pandoc. Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.

我不知道pandoc在将Markdown文档转换为LaTeX时,会有这样一个选项来设置浮点数选项。如果您选择Markdown是因为它的简单性,那么您不应该期望它提供太多的功能,即使使用像pandoc这样强大的工具。底线:Markdown不是乳胶。它是为HTML而不是乳胶而设计的。

Two ways to go:

两种方式:

  1. use the Rnw syntax (R + LaTeX) instead of Rmd (R Markdown) (examples); then you will be able to use the chunk option fig.pos='H' after you \usepackage{float} in the preamble; in this case, you have full power of LaTeX, and pandoc will not be involved

    使用Rnw语法(R + LaTeX)而不是Rmd (R Markdown)(示例);然后,在前言中使用usepackage{float}之后,您就可以使用chunk选项fig.pos='H';在这种情况下,你有乳胶的全部能量,pandoc不会参与

  2. hack at the LaTeX document generated by pandoc, e.g. something like

    破解pandoc生成的乳胶文档,例如类似的东西

    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
    

If you do not like these solutions, consider requesting a new feature to pandoc on Github, then sit back and wait.

如果您不喜欢这些解决方案,可以考虑向Github上的pandoc请求一个新特性,然后坐下来等待。

#2


48  

I present an alternative solution. Instead of inserting [H] symbols into a latex document in a post-hoc manner, I suggest redefining the figure environment to ignore any position arguments and use [H].

我提出了另一种解决方案。我建议重新定义figure环境,忽略任何位置参数并使用[H]。

To do this, create a .tex file in the same directory as the .Rmd file which redefines the figure environment, and update the YAML header in the .Rmd to include the file during compilation.

为此,在与. rmd文件相同的目录中创建一个.tex文件,该文件重新定义了图形环境,并在. rmd中更新YAML头文件,以便在编译期间包含该文件。

Here is an example of a .tex file:

下面是.tex文件的一个示例:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

Here is example .Rmd which includes it (assuming you called the .tex file 'preamble-latex.tex'):

这里有一个包含它的.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)
```

#3


6  

I have a few projects where I convert from .Rmd to .pdf (mostly a beamer slide presentation) and want the graphs to not float (floating figures really don't work with slide presentations).

我有几个项目,我将. rmd转换为.pdf(主要是一个beamer幻灯片演示),并希望图形不浮动(浮动的图形确实不能用于幻灯片演示)。

The approach that I use is to add an escaped space after the line in the .md file. This means that the graph is inside of a paragraph rather than being a paragraph of its own, this means that pandoc will not wrap it in a figure environment (it also means that I cannot use a caption with it) and therefore places it at exactly that position.

我使用的方法是在.md文件的行之后添加转义空格。这意味着图形是在一个段落中,而不是它自己的一个段落中,这意味着pandoc不会将它封装在一个图形环境中(这也意味着我不能使用一个标题),因此将它放置在这个位置。

I use a Makefile to do all the conversions for me, so after running R and knitr it will automatically run a Perl script (though it could be done using R or other tools) that finds where the plots are inserted and adds the escaped space at the end of the line.

我使用一个Makefile为我做所有的转换,所以运行R和knitr之后它会自动运行一个Perl脚本(尽管它可以使用R或其他工具)发现情节的插入并添加最后逃出来的空间。

#4


6  

I am using KnitR and markdown in RSTUDIO, the solution for my case is adding in the preamble \usepackage{float}:

我正在RSTUDIO中使用KnitR和markdown,我的案例的解决方案是在preamble \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}
---

And then adding this lines of code (fig.pos='H') in the very first lines:

然后在第一行添加这几行代码(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
```

#5


3  

If what you are looking for is to just control manually where to put your figures, using this webpage: http://www.rci.rutgers.edu/~ag978/litdata/figs/, I found that if you add one backslash "\" somewhere after your plot commands, the plots will not be floating but instead will be printed in their current location.

如果你想要的是通过这个网页:http://www.rci.rutgers.edu/~ag978/litdata/figs/手动控制你的图形放置在哪里,我发现如果你在你的绘图命令之后在某个地方添加一个反斜杠“\”,那么这些图形将不会是浮动的,而是会被打印在它们当前的位置。

If you want only some plots to appear, you can modify that option for each.

如果您希望只显示一些图,您可以对每个选项进行修改。

In your example:

在你的例子:

# 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 

(etc)

(等)

#6


1  

Using a knitr hook

I somehow stumbled over this question and want to add another approach. Here I make use of the awesome flexibility supplied by knitr hooks. I simply change the plot hook to use the knitr function hook_plot_tex(). Afterwards I can just use the chunk option fig.pos as we are used to in Rnw documents in order to position figure environments (fig.cap must be set in order to invoke the figure environment).

不知何故,我在这个问题上犯了错误,想要添加另一种方法。在这里,我利用了knitr钩子提供的惊人的灵活性。我只是更改了plot hook以使用knitr函数hook_plot_tex()。然后,我可以使用chunk选项fig.pos,就像我们在Rnw文档中使用的那样,以便定位图形环境(为了调用图形环境,必须设置fig.cap)。

This works in the examples provided by the OP. I guess they also work in (somehow) more complicated examples. Why this can be done that easily and is not the default for Rmd documents, I am not sure. Maybe Yihui can clarify that.

这适用于opi提供的示例。我想它们也适用于(某种程度上)更复杂的示例。为什么这样做很容易,而且不是Rmd文档的默认值,我不确定。也许伊辉可以澄清这一点。

Here is the code:

这是代码:

---
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()
```

Without fig.pos='h', the plot will usually jump to the second page.

没有图形。pos='h',通常会跳转到第二页。

#7


0  

Is this what you're after:

这就是你想要的:

```{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

#8


0  

The solution is not too straightforward, maybe someone else will be able to streamline it.

解决方案并不是很简单,也许其他人可以简化它。

The basic steps. (Windows 7)

的基本步骤。(Windows 7)

  1. You can add the argument fig.pos="H" to the knitr options, either globally or for each individual chunk. NOTE the capital H. This instructs latex to place figure floats exactly where they are called in the Rmd file.

    您可以将参数fig.pos="H"添加到knitr选项中,可以是全局的,也可以是单个块的。注意大写h,它指示latex要将图形浮点放在Rmd文件中调用的位置。

  2. BUT, this requires the package to be used by latex, this you can specify in the template that pandoc uses to construct pdf files. You do this by adding the line \usepackage{float}

    但是,这需要由latex来使用包,您可以在pandoc用来构建pdf文件的模板中指定这个包。您可以通过添加行\usepackage{float}来实现这一点

  3. BUT, you first need to find the current template file to modify. I could not find this anywhere but you can get pandoc to print the contents of the template to the console with this command: pandoc -D latex

    但是,首先需要找到要修改的当前模板文件。我在任何地方都找不到它,但是您可以让pandoc用以下命令将模板的内容打印到控制台:pandoc -D latex

  4. Cut and paste this template code into an empty text file.

    剪切并粘贴此模板代码到一个空的文本文件中。

  5. Add the line: \usepackage{float}

    添加行:\ usepackage {浮动}

  6. Save under the filename "default.latex" in a directory such as C:\Users\YOURNAME\pandoc\templates

    在文件名“默认”下保存。如C:\Users\YOURNAME\pandoc\templates乳胶”目录

  7. Add the option --data-dir=C:/Users/YOURNAME/pandoc/templates" to your call to pandoc OR Pandoc.convert("my file.md", format="pdf", options=c("--data-dir=C:/Users/YOURNAME/pandoc/templates")) if using pander in R.

    将选项——data-dir=C:/Users/ your name /pandoc/template添加到调用pandoc或pandoc中。(“我的文件进行转换。如果在R中使用pander, md",格式="pdf",选项=c("- data-dir= c:/Users/YOURNAME/pandoc/templates")。

I hope this works for you.

我希望这对你有用。

#1


24  

I'm not aware of such an option for pandoc to set the floating option of figures when converting a Markdown document to LaTeX. If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like pandoc. Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.

我不知道pandoc在将Markdown文档转换为LaTeX时,会有这样一个选项来设置浮点数选项。如果您选择Markdown是因为它的简单性,那么您不应该期望它提供太多的功能,即使使用像pandoc这样强大的工具。底线:Markdown不是乳胶。它是为HTML而不是乳胶而设计的。

Two ways to go:

两种方式:

  1. use the Rnw syntax (R + LaTeX) instead of Rmd (R Markdown) (examples); then you will be able to use the chunk option fig.pos='H' after you \usepackage{float} in the preamble; in this case, you have full power of LaTeX, and pandoc will not be involved

    使用Rnw语法(R + LaTeX)而不是Rmd (R Markdown)(示例);然后,在前言中使用usepackage{float}之后,您就可以使用chunk选项fig.pos='H';在这种情况下,你有乳胶的全部能量,pandoc不会参与

  2. hack at the LaTeX document generated by pandoc, e.g. something like

    破解pandoc生成的乳胶文档,例如类似的东西

    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
    

If you do not like these solutions, consider requesting a new feature to pandoc on Github, then sit back and wait.

如果您不喜欢这些解决方案,可以考虑向Github上的pandoc请求一个新特性,然后坐下来等待。

#2


48  

I present an alternative solution. Instead of inserting [H] symbols into a latex document in a post-hoc manner, I suggest redefining the figure environment to ignore any position arguments and use [H].

我提出了另一种解决方案。我建议重新定义figure环境,忽略任何位置参数并使用[H]。

To do this, create a .tex file in the same directory as the .Rmd file which redefines the figure environment, and update the YAML header in the .Rmd to include the file during compilation.

为此,在与. rmd文件相同的目录中创建一个.tex文件,该文件重新定义了图形环境,并在. rmd中更新YAML头文件,以便在编译期间包含该文件。

Here is an example of a .tex file:

下面是.tex文件的一个示例:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

Here is example .Rmd which includes it (assuming you called the .tex file 'preamble-latex.tex'):

这里有一个包含它的.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)
```

#3


6  

I have a few projects where I convert from .Rmd to .pdf (mostly a beamer slide presentation) and want the graphs to not float (floating figures really don't work with slide presentations).

我有几个项目,我将. rmd转换为.pdf(主要是一个beamer幻灯片演示),并希望图形不浮动(浮动的图形确实不能用于幻灯片演示)。

The approach that I use is to add an escaped space after the line in the .md file. This means that the graph is inside of a paragraph rather than being a paragraph of its own, this means that pandoc will not wrap it in a figure environment (it also means that I cannot use a caption with it) and therefore places it at exactly that position.

我使用的方法是在.md文件的行之后添加转义空格。这意味着图形是在一个段落中,而不是它自己的一个段落中,这意味着pandoc不会将它封装在一个图形环境中(这也意味着我不能使用一个标题),因此将它放置在这个位置。

I use a Makefile to do all the conversions for me, so after running R and knitr it will automatically run a Perl script (though it could be done using R or other tools) that finds where the plots are inserted and adds the escaped space at the end of the line.

我使用一个Makefile为我做所有的转换,所以运行R和knitr之后它会自动运行一个Perl脚本(尽管它可以使用R或其他工具)发现情节的插入并添加最后逃出来的空间。

#4


6  

I am using KnitR and markdown in RSTUDIO, the solution for my case is adding in the preamble \usepackage{float}:

我正在RSTUDIO中使用KnitR和markdown,我的案例的解决方案是在preamble \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}
---

And then adding this lines of code (fig.pos='H') in the very first lines:

然后在第一行添加这几行代码(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
```

#5


3  

If what you are looking for is to just control manually where to put your figures, using this webpage: http://www.rci.rutgers.edu/~ag978/litdata/figs/, I found that if you add one backslash "\" somewhere after your plot commands, the plots will not be floating but instead will be printed in their current location.

如果你想要的是通过这个网页:http://www.rci.rutgers.edu/~ag978/litdata/figs/手动控制你的图形放置在哪里,我发现如果你在你的绘图命令之后在某个地方添加一个反斜杠“\”,那么这些图形将不会是浮动的,而是会被打印在它们当前的位置。

If you want only some plots to appear, you can modify that option for each.

如果您希望只显示一些图,您可以对每个选项进行修改。

In your example:

在你的例子:

# 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 

(etc)

(等)

#6


1  

Using a knitr hook

I somehow stumbled over this question and want to add another approach. Here I make use of the awesome flexibility supplied by knitr hooks. I simply change the plot hook to use the knitr function hook_plot_tex(). Afterwards I can just use the chunk option fig.pos as we are used to in Rnw documents in order to position figure environments (fig.cap must be set in order to invoke the figure environment).

不知何故,我在这个问题上犯了错误,想要添加另一种方法。在这里,我利用了knitr钩子提供的惊人的灵活性。我只是更改了plot hook以使用knitr函数hook_plot_tex()。然后,我可以使用chunk选项fig.pos,就像我们在Rnw文档中使用的那样,以便定位图形环境(为了调用图形环境,必须设置fig.cap)。

This works in the examples provided by the OP. I guess they also work in (somehow) more complicated examples. Why this can be done that easily and is not the default for Rmd documents, I am not sure. Maybe Yihui can clarify that.

这适用于opi提供的示例。我想它们也适用于(某种程度上)更复杂的示例。为什么这样做很容易,而且不是Rmd文档的默认值,我不确定。也许伊辉可以澄清这一点。

Here is the code:

这是代码:

---
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()
```

Without fig.pos='h', the plot will usually jump to the second page.

没有图形。pos='h',通常会跳转到第二页。

#7


0  

Is this what you're after:

这就是你想要的:

```{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

#8


0  

The solution is not too straightforward, maybe someone else will be able to streamline it.

解决方案并不是很简单,也许其他人可以简化它。

The basic steps. (Windows 7)

的基本步骤。(Windows 7)

  1. You can add the argument fig.pos="H" to the knitr options, either globally or for each individual chunk. NOTE the capital H. This instructs latex to place figure floats exactly where they are called in the Rmd file.

    您可以将参数fig.pos="H"添加到knitr选项中,可以是全局的,也可以是单个块的。注意大写h,它指示latex要将图形浮点放在Rmd文件中调用的位置。

  2. BUT, this requires the package to be used by latex, this you can specify in the template that pandoc uses to construct pdf files. You do this by adding the line \usepackage{float}

    但是,这需要由latex来使用包,您可以在pandoc用来构建pdf文件的模板中指定这个包。您可以通过添加行\usepackage{float}来实现这一点

  3. BUT, you first need to find the current template file to modify. I could not find this anywhere but you can get pandoc to print the contents of the template to the console with this command: pandoc -D latex

    但是,首先需要找到要修改的当前模板文件。我在任何地方都找不到它,但是您可以让pandoc用以下命令将模板的内容打印到控制台:pandoc -D latex

  4. Cut and paste this template code into an empty text file.

    剪切并粘贴此模板代码到一个空的文本文件中。

  5. Add the line: \usepackage{float}

    添加行:\ usepackage {浮动}

  6. Save under the filename "default.latex" in a directory such as C:\Users\YOURNAME\pandoc\templates

    在文件名“默认”下保存。如C:\Users\YOURNAME\pandoc\templates乳胶”目录

  7. Add the option --data-dir=C:/Users/YOURNAME/pandoc/templates" to your call to pandoc OR Pandoc.convert("my file.md", format="pdf", options=c("--data-dir=C:/Users/YOURNAME/pandoc/templates")) if using pander in R.

    将选项——data-dir=C:/Users/ your name /pandoc/template添加到调用pandoc或pandoc中。(“我的文件进行转换。如果在R中使用pander, md",格式="pdf",选项=c("- data-dir= c:/Users/YOURNAME/pandoc/templates")。

I hope this works for you.

我希望这对你有用。