# How do I constrain the location of tables and figures in my .RMD file?

I wrote a .RMD file that has many figures and tables. Here is the initial info:

``````---
title: "My Title"
author: ""
date: "5/14/2021"
output: bookdown::pdf_book
bibliography: bibliography.bib
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk\$set(echo = FALSE,
fig_align = "left",
fig_width = 7,
fig_height = 7,
dev = "png",
cache = FALSE)
```

The file is divided into Chapters (#), Sections (##) and Subsections (###). In each Subsection I have inserted a table and three figures, like this

### President

Some text ...

Figure \@ref(fig:presidentialline) shows ...

f(fig:presidentialmean) shows that ...

Figure \@ref(fig:presidentialvol) has Y ...

```{r presidentialsumm}
kable(Presidential_Summary_BarCharts\$Summary,
format='latex',
caption='President Summary Statistics')
```

```{r presidentialline, fig.cap=' Performance by President'}
Presidential_LineChart
```

```{r presidentialmean, fig.cap='President Mean'}
Presidential_Summary_BarCharts\$MeanBarChart
```

```{r presidentialvol, fig.cap='President Vol'}
Presidential_Summary_BarCharts\$VolBarChart
```
### Governor

Governor Subsection text ...

Government subsection figures and tables, similar to the above
``````

The problem is that the three figures and the table do not stay inside the subsection marked with ###. For example, the PDF file would show the text of the President Subsection and one or more figures then show the text of the Governor Subsection and only then show the remaining figures of the President subsection. The tables tend to be inserted before the appropriate Subsection. Is there a way to force the figures and table in the PDF file to stay within a Subsection demarcated in the .RMD file by ###?

`pdf` files are rendered by the behind-the-scenes `pandoc` program into a `tex` file before being rendered by LaTeX and then converted into `pdf` format. Figures and table float without fine grained control, unfortunately. Although it is possible to flush those objects, a deep dive into `tex` syntax is likely required. If all that is desired is to have the figures and tables to appear in the same level, it is probably the path of least resistance to save the subsections separately and then stitch them together.

Thanks. Quite useful. When you say "save the subsections separately" does it mean having a different .RMD for each subsection or is there a way to produce different .PDF files from different subsections in the context of a single .RMD file?

Separate .RMD is what I had in mind, though that pretty much commits to having a pagination numbering mess. AFAIK, sub-rendering isn't a feature.

If your output is a pdf, you could use LaTeX syntax to control the placement of tables and figures. You could for example use `\FloatBarrier` from the `placeins` LaTeX package. You just need to add the package to your YAML header, e.g.
`header-includes: \usepackage{placeins}`

And then add `\FloatBarrier` in your .rmd file in a text chunk at the point where you want the table or the figure to come before.

Best, Richard

2 Likes

Maybe this chapter in the R Markdown cookbook could be of help

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.