2

I'm creating an R Markdown document which outputs a PDF document. My YAML header is the following:

---
title: "Introduction"
author: "John Doe
date: "August 26, 2018"
mainfont: Pancetta Pro
documentclass: book
output:
  pdf_document:
    number_sections: true
    df_print: kable
    fig_caption: yes
    fig_width: 6
    highlight: tango
    includes:
      in_header: preamble.tex
    latex_engine: xelatex
geometry: headheight=25pt, tmargin=25mm, bmargin=20mm, innermargin=20mm, outermargin=20mm
---

In the preamble.tex file I want to have the following LaTeX commands (which just modify the way the headers are displayed):

\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}    
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]

However, when these last lines are included in the preamble.tex I get an error when knitting the R Markdown file:

! Argument of \paragraph has an extra }.
<inserted text> 
\par 
l.1290 \ttl@extract\paragraph
Error: Failed to compile Template.tex

I can't figure out why it won't run. The contents of thepreamble.texfile are the following:

% !TeX program = lualatex
\usepackage{relsize} % To make math slightly larger.

\newcommand{\thehook}{%
 \hspace{.5em}%
 \setlength{\unitlength}{1em}%
 \raisebox{-.5em}{\begin{picture}(.4,1.7)
  \put(0,0){\line(1,0){.2}}
  \put(.2,0){\line(0,1){1.7}}
  \put(.2,1.7){\line(1,0){.2}}
 \end{picture}}%
 \hspace{0.5em}%
} %This creates the "hook" symbol at the beginning of each chapter.

\usepackage{anyfontsize}
\usepackage{fontspec}
\setmainfont{Pancetta Pro}

%   We set the font for the chapters:
\newfontfamily\chapterfont{Pancetta Pro}

%   And now for the sections:
\newfontfamily\sectionfont{Pancetta Pro}


\usepackage{fancyhdr}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[RO]{\large\sffamily\rightmark\thehook\textbf{\thepage}}
\fancyhead[LE]{\large\sffamily\textbf{\thepage}\thehook\rightmark}

\fancypagestyle{plain}{%
 \fancyhf{}
}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}

\fontsize{12}{20}\selectfont

\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]

When excluding the last 6 lines in the previous code there's no error and the pdf is created.

1 Answers1

2

If you want to use titlesec together with rmarkdown you have to add

subparagraph: yes

to your YAML headers, c.f. several other answers.


The default LaTeX class used by rmarkdown is article, which has no chapters. You should add

documentclass: report

or

documentclass: book

to your YAML header.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Thank you Ralf, but that didn't fix it. I'll re-edit the question and write my whole preamble and my R Markdown YAML header. Nonetheless, many thanks for the quick response and the good catch! (I really appreciate it!) – Alexis Solis Aug 28 '18 at 04:54
  • @AlexisSolis Please provide a [mcve] instead of including all the code. – Ralf Stubner Aug 28 '18 at 04:59
  • That's what I tried, Ralf; thanks for the quick response. As a side note, all I want to do is create a good template for my lecture notes which include code, text and code-output. Maybe I should start checking out the "bookdown" package? – Alexis Solis Aug 28 '18 at 05:05
  • @AlexisSolis See the updated answer, even though this change would have warranted a new question. Looking at `bookdown` is definitely a a good idea. Most documents that need chapters will profit from using that. This won't help with these issues, though. – Ralf Stubner Aug 28 '18 at 07:31