Can't use both fig.pos and out.width/height in knitr?

Viewed 44

I am using the bookdown package to build my pdf. Here is my _output.yml:

## Here to create the pdf
bookdown::pdf_book:
  extra_dependencies: ["float"]
  toc: true
  template: _assets/template.tex
  mainmatter: true
  includes:
    in_header: _assets/preamble.tex
  latex_engine: lualatex
  keep_tex: yes
  citation_package: natbib

When I use only fig.pos = "!H" or out.height="75%", the knitting works.

However, I'd like to use both fig.pos = "!H" and out.height="75%" (for instance {r fig, fig.cap="Caption", out.height="75%", fig.pos = "!H"}) but when I do so, I have:

! LaTeX Error: Unknown float option `H'.

I checked and the package float is correctly installed.

1 Answers

There is nothing that prevents fig.pos and out.height from being used a the same time. If I evaluate the code below:

---
title: "R Notebook"
output: latex_fragment
---

```{r fig, fig.cap="Caption", out.height="75%", fig.pos = "!H"}
library(ggplot2)
ggplot()+ theme_minimal()
```

I get the following latex output:

\begin{figure}[!H]
\includegraphics[height=0.75\textheight]{TestX_files/figure-latex/fig-1} \caption{Caption}\label{fig:fig}
\end{figure}

Which is what you'd expect with the options given (note the 0.75\textheight and the [!H])

The error comes from the package float not being available. To properly produce a pdf with fig.pos options, add the following:

---
output: 
  pdf_document :
    extra_dependencies: ["float"]
---

as explained here

Related