如何在R markdown中获得文字浮动/文字等包围?

时间:2022-03-16 18:23:20

I am currently writing some reports with LaTeX. Due to most of my analysis is made with R, I switched to Rmarkdown, which I am very happy with so far, except one thing, namely when it comes to include figures in the pdf output.

我目前正在用LaTeX写一些报告。由于我的大部分分析是用R做的,所以我切换到Rmarkdown,到目前为止我对此非常满意,除了一件事,即在pdf输出中包含数字时。

Some of my plots are floating, so they are surrounded by text and the other kinds of output, put some other of my figures take a whole page in the pdf document, although they are not that big. And I have absolutely no clue why it is like that. Does anyone know that problem or a solution?

我的一些情节是浮动的,因此它们被文本和其他类型的输出所包围,把我的其他一些数字放在pdf文档中整整一页,尽管它们并不那么大。我完全不知道为什么会这样。有谁知道这个问题或解决方案?

I would like to switch to Rmarkdown for good, but if that image issue stays like that I can´t use it for more serious reports, where also the form matters.

我想切换到Rmarkdown,但是如果那个图像问题仍然存在,那么我就无法将它用于更严肃的报告,而且表格也很重要。

Example:

---
title: "Multivariate Analysis"
subtitle: "written report"
author: "by XXX"
date: "24.10.2017"
output: 
   pdf_document:
      fig_caption: yes
---

```{r include = FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```

...
some more R and Latex stuff which works well, approx. 6 pages
...

These eigenvalues determine the proportion of the total variance explained by the i-th component.

```{r, fig.cap="individual and total proportion of total variance explained by the regarding principle components"}
ggplot() + ...
```

```{r}
options(digits=2)
```

So the first principle component contains already `r eig.val[1]/sum(eig.val)*100`% of the information. 
more text text text text text text text text text text text text text text text text text text text text 
text text text text text text text text text text text text text text 

$$
\begin{aligned}
&Y_1=`r eig.vec[1,1]` ...  some latex equations
\end{aligned}
$$

$Y_1$ seems to be text text text text text text text text text text text text text text text text text text text text text text text
text text text text **PAGEBREAK PAGEBREAK** text text text text text text text text text text text text text 

In this case I got a pagebreak in the second part of the text where it says PAGEBREAK. On the next page I get my figure from above, which only takes 50% of that pages space but still there´s nothing else on that page. On the next page after the figure the text continous.

在这种情况下,我在文本的第二部分中有一个分页符,其中显示了PAGEBREAK。在下一页我从上面得到了我的数字,这只占该页面空间的50%,但在该页面上仍然没有其他内容。在图之后的下一页文本连续。

That´s like a harsh break in the middle of a sentence separated by a whole page which is half empty.

这就像是一个句子中间的一个严厉的断裂,被一整页半空隔开。

Why is it like that? To me it seems to be random which images are floating in the text and which not in total, 30-50% have that issue as the one above.

为什么会那样?对我来说,似乎是随机的图像漂浮在文本中,并且总体而言,30-50%具有上述问题。

Does anyone have any kind of idea how to solve that? Every hint is welcome I am really desparated...

有没有人有任何想法如何解决?每一个提示都是受欢迎的我真的很绝望......

I also tried to set fig.pos='h' but didn´t change a thing.

我也尝试设置fig.pos ='h',但没有改变一件事。

1 个解决方案

#1


0  

I tend to organize my folder as following

我倾向于组织我的文件夹如下

  • images
  • tables
  • data

These folder can be placed on top of the folder where the markdown (or sweave) script is. They are used to place image, tables generated by xtable or other, and saved data and models.

这些文件夹可以放在markdown(或sweave)脚本所在的文件夹的顶部。它们用于放置由xtable或其他生成的图像,表格,以及保存的数据和模型。

In my scripts, I always have an initial chunk where I put the path to those directories, this way I can easily point to the folders as in the following example.

在我的脚本中,我总是有一个初始块,我将路径放到这些目录中,这样我就可以轻松指向文件夹,如下例所示。

In the latex preamble, note the \graphicspath{{./images/}} which points to your image folder.

在乳胶前言中,请注意指向图像文件夹的\ graphicspath {{./ images /}}。

In knitr, using the argument fig.show='hide' will allow you to avoid directly printing the output. The figure is printed in the image folder using the image_dir argument.

在knitr中,使用参数fig.show ='hide'将允许您避免直接打印输出。使用image_dir参数将图形打印在图像文件夹中。

Since you are using latex, you can use pdf() instead of png() to save the pictures. Finally you use the figure environment in latex to handle your figures the way you want.

由于您使用的是乳胶,因此可以使用pdf()而不是png()来保存图片。最后,您可以使用乳胶中的数字环境以您想要的方式处理您的数字。

\documentclass{article}
\graphicspath{{./images/}}
\usepackage{graphicx}
\title{Test example to show how to handle figure placements}
\date{} % no date
\begin{document}
\maketitle{}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@

<<init, echo = FALSE, results = 'hide'>>=
require(stringr)
# here I set the path to my directories
image_dir <- str_c(getwd(),"/images/",sep="")
@

<<print_figure, echo = FALSE, results = 'hide',fig.show='hide'>>=    
png(file=str_c(image_dir,"random.png"))
plot(rnorm(20))
dev.off()
@

Here is the Figure \ref{test_plot}.
\begin{figure}[htbp]
  \begin{center}
  \includegraphics[width=0.8\textwidth]{random.png}
  \caption{Test plot output.}
  \label{test_plot}
  \end{center}
\end{figure}
\end{document}

如何在R markdown中获得文字浮动/文字等包围?

#1


0  

I tend to organize my folder as following

我倾向于组织我的文件夹如下

  • images
  • tables
  • data

These folder can be placed on top of the folder where the markdown (or sweave) script is. They are used to place image, tables generated by xtable or other, and saved data and models.

这些文件夹可以放在markdown(或sweave)脚本所在的文件夹的顶部。它们用于放置由xtable或其他生成的图像,表格,以及保存的数据和模型。

In my scripts, I always have an initial chunk where I put the path to those directories, this way I can easily point to the folders as in the following example.

在我的脚本中,我总是有一个初始块,我将路径放到这些目录中,这样我就可以轻松指向文件夹,如下例所示。

In the latex preamble, note the \graphicspath{{./images/}} which points to your image folder.

在乳胶前言中,请注意指向图像文件夹的\ graphicspath {{./ images /}}。

In knitr, using the argument fig.show='hide' will allow you to avoid directly printing the output. The figure is printed in the image folder using the image_dir argument.

在knitr中,使用参数fig.show ='hide'将允许您避免直接打印输出。使用image_dir参数将图形打印在图像文件夹中。

Since you are using latex, you can use pdf() instead of png() to save the pictures. Finally you use the figure environment in latex to handle your figures the way you want.

由于您使用的是乳胶,因此可以使用pdf()而不是png()来保存图片。最后,您可以使用乳胶中的数字环境以您想要的方式处理您的数字。

\documentclass{article}
\graphicspath{{./images/}}
\usepackage{graphicx}
\title{Test example to show how to handle figure placements}
\date{} % no date
\begin{document}
\maketitle{}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@

<<init, echo = FALSE, results = 'hide'>>=
require(stringr)
# here I set the path to my directories
image_dir <- str_c(getwd(),"/images/",sep="")
@

<<print_figure, echo = FALSE, results = 'hide',fig.show='hide'>>=    
png(file=str_c(image_dir,"random.png"))
plot(rnorm(20))
dev.off()
@

Here is the Figure \ref{test_plot}.
\begin{figure}[htbp]
  \begin{center}
  \includegraphics[width=0.8\textwidth]{random.png}
  \caption{Test plot output.}
  \label{test_plot}
  \end{center}
\end{figure}
\end{document}

如何在R markdown中获得文字浮动/文字等包围?