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

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.

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

Footnotes and logo in Rmarkdown generated PDF

I am trying to integrate footnotes AND a logo on the frontpage of a Rmarkdown generated PDF.
I try this:
---
title : "My report"
date : November 4th, 2022
author: "Author 1[^note1]"
documentclass : report
keep_tex : true
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}
\includegraphics[width=2in,height=2in]{logo.png}\LARGE\\}
- \posttitle{\end{center}}
output : pdf_document
---
[^note1]: Writing, Research, Editing
Inspired by this procedure to integrate the logo:
https://bookdown.org/yihui/rmarkdown-cookbook/latex-logo.html
And this for the footnotes:
https://stackoverflow.com/a/62514739/12398676
However, I am getting this error:
! Argument of \reserved#a has an extra }.
<inserted text>
\par
l.69 ...or 1\footnote{Writing, Research, Editing}}
Error: LaTeX failed to compile logo_footnote.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See logo_footnote.log for more info.
Execution halted
Anyone has an idea of what is going wrong?
EDIT: the solution provided by samcarter_is_at_topanswers.xyz
... is one step closer to the solution but not quite there.
I now have a logo and author name, but the author name is overlayed with the date
EDIT2: In the solution provided by samcarter_is_at_topanswers.xyz I get a similar display problem even without a logo
---
title : "My report"
date : November 4th, 2022
documentclass : report
keep_tex : true
header-includes:
- \AtBeginDocument{\author{Author 1\protect\footnote{Writing, Research, Editing}}}
output :
pdf_document:
keep_tex: true
---
test
You can work around the problem like this:
---
title : "My report"
date : November 4th, 2022
documentclass : report
keep_tex : true
header-includes:
- \AtBeginDocument{\author{Author 1\protect\footnote{Writing, Research, Editing}}}
- \usepackage{titling}
- \pretitle{\begin{center}
\includegraphics[width=2in,height=2in]{example-image-duck}\LARGE\par}
- \posttitle{\end{center}}
output :
pdf_document:
keep_tex: true
---
test

Include by \input an header-includes: preamble in a markdown file

I have plenty of markdown files starting with a rather long header-includes, followed by some included directives:
---
header-includes:
- \usepackage{fvextra}
- \usepackage{tcolorbox}
- \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,breakanywhere,breaksymbol=,breakanywheresymbolpre=,commandchars=\\\{\}}
- \usepackage[margins=raggedright]{floatrow}
title: "Mathématiques \n55. Les dérivées"
subtitle: ""
author: Marc Le Bihan
geometry: margin=2cm
fontsize: 12pt
output: pdf
classoption: fleqn
urlcolor: blue
---
\input{apprentissage-include}
where apprentissage-include.tex has such definitions:
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,breakanywhere,breaksymbol=,breakanywheresymbolpre=,commandchars=\\\{\}}
\newcommand{\donnee}[1]{\textbf{\textcolor{Purple}{#1}}}
\newcommand{\definition}[1]{\textbf{\textcolor{blue}{#1}}}
[...]
I would like to include the header-includes preamble too, to change its content more easily when updates are needed.
I've created an header-include.tex containing:
---
header-includes:
- \usepackage{fvextra}
[...]
---
But whatever I've tried :
\input{header-include}
\input{apprentissage-include}
or
---
\input{header-include}
---
\input{apprentissage-include}
I'm receiving the message :
! LaTeX Error: Can be used only in preamble.
Do I have a way to succeed?
As you are loading packages in the header-include.tex file it needs to go into the header. To get access to the latex syntax of \input{...}, you have to put it into the header includes:
markdown file:
---
output:
pdf_document:
keep_tex: true
header-includes:
- \input{header-include}
- \input{apprentissage-include}
title: "Mathématiques \n55. Les dérivées"
subtitle: ""
author: Marc Le Bihan
geometry: margin=2cm
fontsize: 12pt
classoption: fleqn
urlcolor: blue
---
test
The header-include.tex file needs to use latex syntax, so no - or any other markdown syntax
header-include.tex:
\usepackage{fvextra}
The apprentissage-include.tex file is the same as in your question
apprentissage-include.tex
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,breakanywhere,breaksymbol=,breakanywheresymbolpre=,commandchars=\\\{\}}
\newcommand{\donnee}[1]{\textbf{\textcolor{Purple}{#1}}}
\newcommand{\definition}[1]{\textbf{\textcolor{blue}{#1}}}

Remove table of contents from a R bookdown to pdf_book or pdf_document2

I compiling a short document using bookdown in Rstudio. I would simply like to drop the table of contents at the beginning of the document. I could "knit to pdf" instead of "knit to pdf_book" or "knit to pdf_document2", but then would loose a lot of the bookdown functions (cross references etc.).
Should be fairly easy, isn't it?
---
title: "MWE"
output:
bookdown::pdf_book:
toc: false
---
It also works with pdf_document2:
---
title: "MWE"
output:
bookdown::pdf_document2:
toc: false
---

Apply knitr option function in "before_body" when using Rmarkdown to compile PDF

As question, I am using knitr option (i.e. include=temp.switch) to compile multiple reports based on the value given to a "switch" (i.e. temp.switch), this are working well for the content, but I would like to use similar function to modify the cover page of the report, which is stored in the tex file read in yaml, like the one below:
---
output:
pdf_document:
# template: template.tex
number_sections: true
keep_tex: true
includes:
in_header: preamble.tex
before_body: doc-prefix.tex
papersize: a4
editor_options:
chunk_output_type: console
---
Can anyone tell me how to make the doc-prefix.tex "conditional" based on the value of the "switch"? Thanks!

How can I use latex packages in R markdown beamer?

I want to include latex packages in R markdown pdf and beamer.
Could you help me in including usepackage command in rmarkdown beamer file?
Thanks a lot.
A simple solution would be to have your header setup like this:
---
title: "Document title"
author: "Author's name"
date: "Document date"
output: beamer_presentation
header-includes:
- \usepackage[brazil]{babel}
- \usepackage{graphicx}
- \usepackage[a4paper]{geometry}
---
The YAML header of the Rmd would look like:
---
title: "Habits"
output:
pdf_document:
includes:
in_header: header.tex
---
Then, in the file header.tex, include any usepackage statements you need, eg. \usepackage{pdflscape}
See http://rmarkdown.rstudio.com/pdf_document_format.html for more information.

Resources