how to get figure floated / surrounded by text etc

2020-03-07 04:54发布

问题:

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.

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?

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.

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 principal components"}
ggplot() + ...
```

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

So the first principal 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.

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.

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.

回答1:

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.

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.

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.

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.

\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}