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

Viewed 34

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?

1 Answers

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