1

I am writing a text in bookdown with Rstudio and want to include a picture within the text like this:

Some text goes here

```{r, fig.cap="\\label{fig:figs}figlabel"}
knitr::include_graphics("images/image.png")
```
Some other text goes here.

However, when I render the book with bookdown::render_book("index.Rmd"), The inserted picture is placed on the next page rather than where it is placed in the text. I want it to be placed between the two sentences, but it is placed below the last one.

Is there a way to control where in the text the image is rendered? I have tried to look at chunk options for images, and also in the bookdown documentation, but neither seem to document ways to control placement of figures.

Haakonkas
  • 961
  • 9
  • 26
  • 3
    https://stackoverflow.com/a/44888475/9941039 May this will help you – PierreD Jan 28 '19 at 10:31
  • The link in this comment only works for table at the moment, not with figures. See this thread instead: https://stackoverflow.com/questions/16626462/figure-position-in-markdown-when-converting-to-pdf-with-knitr-and-pandoc/33801326#33801326 (no satisfactory solution so far.) – Julien Colomb Jul 13 '20 at 09:23

1 Answers1

1

LaTeX will try to find an optimal location for the figure. You can force the placement of floating figures and tables with \FloatBarrier. Note that doing so, you might end up with a lot of white space on the bottom of the page.

Some text goes here. See fig. \@ref(fig:my-fig)

```{r my-fig, fig.cap="fig caption"}
knitr::include_graphics("images/image.png")
```

\FloatBarrier

Some other text goes here.
Thierry
  • 18,049
  • 5
  • 48
  • 66