1

I am working on a somewhat large report, where I produce data tables and figures in r and then use rmarkdown to compile those results into a pdf report. Normally I do not have issues making tables/figures appear where I want them (HOLD_position does the trick), but I discovered that the normal trick does not work for some reason where a 4th level header is concerned.

Attaching a reproducible example, first making a figure and tables in R:

data("iris")
library(ggplot2)

# plots with numbered titles to help track in document
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
  geom_smooth() + ggtitle("Plot 1")
png('mock1.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()

p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
  geom_smooth() + ggtitle("Plot 2")
png('mock2.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()

p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
  geom_smooth() + ggtitle("Plot 3")
png('mock3.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()

write.csv(iris[1:30,], file = "table.csv")

Now an rmd which produces figures/tables that are out of order with respect to the 4th level headers (#### 4th level header)


title: "I broke HOLD_position"
author: "cdtip"
date: "3/1/2022"
output: pdf_document
---

# 1st level heading

## 2nd level heading

### 3rd level heading

#### 4th: set 1

```{=latex}
\begin{figure}[H]
  \centering
  \includegraphics[width=14 cm]{mock1}
  \caption{Caption for mock fig.} 
\end{figure}
```

```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)

kbl(tab, caption = "Table 1", booktabs = T) %>%
  kable_styling(latex_options = c("HOLD_position"))

```

#### 4th: set 2

```{=latex}
\begin{figure}[H]
  \centering
  \includegraphics[width=14 cm]{mock2}
  \caption{Caption for mock fig.} 
\end{figure}
```

```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)

kbl(tab, caption = "Table 2", booktabs = T) %>%
  kable_styling(latex_options = c("HOLD_position"))

```

#### 4th: set 3

```{=latex}
\begin{figure}[H]
  \centering
  \includegraphics[width=14 cm]{mock3}
  \caption{Caption for mock fig.} 
\end{figure}
```

```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 3", booktabs = T) %>%
  kable_styling(latex_options = c("HOLD_position"))

```

If you follow the code provided and assuming my issue is reproducible, you should produce a pdf where the tables and figures are placed whereever the mystical pdf float gods decided to place them, and NOT in the order I specified. Now, if you repeat that same .rmd portion, but change the 4th level to 3rd level headers (e.g., #### to ###), the items should be placed correctly.

I've tried a few solutions proposed for similar issues:

For the immediate project it is fine to just use 3rd level headings, but I don't like how this in turn affects the ToC as I now have several appended names to mark the differences in what previously would have been under different 3rd level categories. Can this be fixed in order to use 4th level headers?

cdtip
  • 153
  • 7
  • ran in the exact same issue under xelatex and with bookdown::pdf_document2 - I asked on GH and it seeems its a latex float-package behaviour. I had success with adding a line of text after the level-4-heading followed by the table - it worked – tobin_lab Apr 22 '22 at 06:45

0 Answers0