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

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}}}

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

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

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

Repeat Title and Date R Markdown PDF

I am struggling what what is likely a trivial markdown matter. I am hoping to avoid YAML and additional header templates and such if I can. So my question is straightforward and I welcome your feedback. For an R Markdown knitted as a PDF, how can I repeat the title and date on each printed page?
Alternatively I am happy to use headers using the '#' syntax. But if I can use the title parameters that would be preferred.
The Header thus far:
---
title: "Dashboard"
date: '`r paste(Sys.Date(), " to ", Sys.Date()+14)`'
output:
pdf_document:
latex_engine: xelatex
mainfont: Calibri
geometry: margin=1cm
classoption: landscape
params:
set_title: !r test_title
header-includes:
- \usepackage{titling}
- \pretitle{\begin{flushleft}\huge}
- \posttitle{\end{flushleft}}
- \preauthor{\begin{flushleft}}
- \postauthor{\end{flushleft}}
- \predate{\begin{flushleft}\large}
- \postdate{\end{flushleft}}
---
From the rmarkdown cookbook:
When a Rmd document is compiled, all of its metadata in the YAML section
will be stored in the list object rmarkdown::metadata. You can use this object
in your R code. For example, rmarkdown::metadata$title gives you the title
of the document.

Duplicated title page in r-markdown beamer presentation

Im trying to include a title page customized in latex in my r markdown beamer presentation, but im getting two title pages in the output: one generated by R markdown and another one generated by the latex code that I included in the before-body option of the YAML header.
Is there any way to prevent r markdown from generating a title page so that I only get my personalized title page in the output?
Here is a MWE:
---
author: my-name
title: my-title
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
beamer_presentation:
includes:
before_body: title-page.tex
---
## First frame
- some content
## Second frame
- some content
Contents of title-page.tex:
\begin{frame}[plain]
\inserttitle
\insertauthor
\insertdate
\end{frame}
I already tried adding \AtBeginDocument{\let\maketitle\relax} to a file included in_header but I still got two title pages.
Instead of manually creating your title page, you can simply modify the frame title template:
---
author: my-name
title: my-title
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
beamer_presentation:
keep_tex: true
header-includes: |
\setbeamertemplate{title page}{
\inserttitle
\insertauthor
\insertdate
}
---
## First frame
- some content
## Second frame
- some content

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