Fancyhead for unnumbered appendix in R Markdown knit to pdf - latex

A question about fancyhdr in R Markdown knitting to pdf.
Can anyone help me to fix the header for the appendix? I have numbered sections in the document, and the fancyhead for pages with the numbered sections is oke. But for the unnumbered appendix, it keeps showing the last numbered section, where I just want the title of the current unnumbered appendix to be shown. Code example below. Thanks!
---
title: "Example"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
---
\newpage
# Chapter
\newpage
# Appendix {-}

You can adjust the header like this:
---
title: "Example"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
---
\newpage
# Chapter
\newpage
# Appendix {-}
\markboth{something here}{something there}

Related

Latex in document referencing via Varioref is not working in Rmarkdown

I've been using vanilla latex documents for a long time and have recently switched over to Rmarkdown. I'm struggling to get varioref to work, or any in-document referencing for that matter. See below example Rmarkdown file contents. I've tried to make it a minimally reproducible example.
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes: \usepackage{varioref}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}
It seems like the reference is being created with a clickable link, but there is no reference to the section being created..
Rmarkdown producing unnumbered sections has nothing to do with varioref. You can make them numbered like this:
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes:
- \usepackage{varioref}
- \setcounter{secnumdepth}{4}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}

How do I format a TOC in bookdown using latex?

I am using bookdown with latex styling to make a report that contains headers, footers and section formatting. When i knit the report all pages have the correct formatting except the TOC.
MWE is below. There are no headers and the default section title formatting is not applied to the contents page.
index.Rmd
---
title: "A Quick Test"
author: "Jack Jill"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: article
description: "This is a minimal example of a report."
papersize: a4
output:
bookdown::pdf_document2:
includes:
in_header: in_header.tex
before_body: before_body.tex
toc: true
geometry: top=20mm,bottom=60mm,footnotesep=10mm,left=24mm,right=30mm
---
\newpage
# **HEADING 1**
## **Subheading**
Here is some text for the document.
# **HEADING 2**
Here is some text for the document.
# **HEADING 3**
Here is some text for the document.
in_header.tex
%Essential packages
\usepackage{graphicx, color}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{etoolbox}
\usepackage{tocloft}
\definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66}
\definecolor{aliceblue}{rgb}{0.94, 0.97, 1.0}
\definecolor{amber}{rgb}{1.0, 0.75, 0.0}
\definecolor{amaranth}{rgb}{0.9, 0.17, 0.31}
\definecolor{amber}{rgb}{1.0, 0.75, 0.0}
\definecolor{black}{rgb}{0, 0, 0}
% This changes all section headings
\titleformat*{\section}{\bfseries\Large\itshape}
\titleformat*{\section}{\color{airforceblue}}
% Turn off default title page so I can make my own
\let\oldmaketitle\maketitle
\AtBeginDocument{\let\maketitle\relax}
before_body.tex
\pagestyle{fancy}
\fancyhead{}
\fancyhf{}
% Page layout
\parindent 0pt
\parskip 0pt
% Make custom page numbers
\cfoot{Page \thepage \hspace{1pt} of \pageref{LastPage}}
% Make a pretty header for every page
\fancyhead[L]{\includegraphics[width=3.5cm]{logo.png}}
\fancyhead[R]{\raisebox{1.6cm}{\color{amaranth}www.website.com.au}}
% Make the title page here
\setlength\headheight{68pt}
\vspace*{2.5cm}
\fontsize{36pt}{42pt}\selectfont\bfseries\color{airforceblue}
MY REPORT TITLE
\vspace*{0.5cm}
\color{airforceblue}
{\bfseries\huge{ SUBTITLE } }
\vspace*{1.0cm}
\colorbox{amber}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\bfseries\huge\strut\textcolor{aliceblue}{ AUTHOR NAME } }}
% replace default title page with my new one
\let\maketitle\oldmaketitle
% end page
\clearpage
% Set default fonts for doc
\fontsize{10}{12}\selectfont
\fontfamily{qcr}\selectfont
\setlength{\headheight}{98pt}
\color{black}
% Manually adding TOC does not add header either
% \tableofcontents
% \addcontentsline{toc}{section}{\contentsname}
The toc page uses the plain page style. If your version of the fancyhdr package is reasonable new, you can change the plain page style to be the same as your fancy page style with the simple command
\fancypagestyle{plain}[fancy]{}
---
title: "A Quick Test"
author: "Jack Jill"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: article
description: "This is a minimal example of a report."
papersize: a4
output:
bookdown::pdf_document2:
toc: true
toc_depth: 2
keep_tex: true
includes:
in_header: in_header.tex
before_body: before_body.tex
header-includes: \fancypagestyle{plain}[fancy]{}
geometry: top=20mm,bottom=60mm,footnotesep=10mm,left=24mm,right=30mm
---
\newpage
# **HEADING 1**
## **Subheading**
Here is some text for the document.
# **HEADING 2**
Here is some text for the document.
# **HEADING 3**
Here is some text for the document.

Rmd: Hide FOOTER on title&content page

I am creating a RMarkdown template of Beamer slides and use the metropolis theme as a basis.
Now I want to add a footer with some text on the left side and the slide number as fraction on the right side. That works. Furthermore, the whole footer should not be shown on the title & content page. How can I control where the foot line is shown?
This is my (minimal working example):
slides.rmd
---
title: "Title"
subtitle: "Subtitle"
author: "Simon"
institute: "RUB"
date: "September 22, 2021"
output:
beamer_presentation:
keep_md: true
keep_tex: no
latex_engine: xelatex
#theme: metropolis
includes:
in_header:
#- toc.tex
slide_level: 2 # which header level should be printed as slides
incremental: no
header-includes:
- \usetheme[numbering=fraction]{metropolis}
- \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
- \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021}}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Content
\tableofcontents[]
# Level I
Test
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
I know that it would be possible in Latex via the begingroup & endgroup command combined with the plain-option ({}) used on \setbeamertemplate{footline}:
\documentclass{beamer}
\setbeamertemplate{footline}[frame number]
\begin{document}
\begin{frame}
normal frame
\end{frame}
\begingroup
\setbeamertemplate{footline}{}
\begin{frame}
without footline
\end{frame}
\endgroup
\begin{frame}
normal frame
\end{frame}
\end{document}
But I don't know how to implement it in RMarkdown.
To remove the footline from your title and toc page, you can use the same trick as in https://topanswers.xyz/tex?q=1004#a1198
Add the following to your header includes:
\makeatletter
\def\ps#navigation#titlepage{%
\setbeamertemplate{footline}{}
\#nameuse{ps#navigation}
}
\addtobeamertemplate{title page}{\thispagestyle{navigation#titlepage}}{}
\pretocmd{\tableofcontents}{\thispagestyle{navigation#titlepage}}{}{}
\makeatother
(please also note that the syntax \tiny{...} is wrong. This macro is a switch and does not take an argument. You can instead use {\tiny ...}
---
title: "Title"
subtitle: "Subtitle"
author: "Simon"
institute: "RUB"
date: "September 22, 2021"
output:
beamer_presentation:
keep_md: true
keep_tex: no
latex_engine: xelatex
#theme: metropolis
includes:
in_header:
#- toc.tex
slide_level: 2 # which header level should be printed as slides
incremental: no
header-includes:
- \usetheme[numbering=fraction]{metropolis}
- \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
- \setbeamertemplate{frame footer}{{\tiny\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021}}}
- \makeatletter
- \def\ps#navigation#titlepage{\setbeamertemplate{footline}{}\#nameuse{ps#navigation}}
- \addtobeamertemplate{title page}{\thispagestyle{navigation#titlepage}}{}
- \pretocmd{\tableofcontents}{\thispagestyle{navigation#titlepage}}{}{}
- \setbeamertemplate{section in toc}{\leavevmode\inserttocsectionnumber. \inserttocsection\par}
- \makeatother
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Content
\tableofcontents[]
# Level I
Test
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```

Include after_body in toc with bookdown

I am creating a pdf book, and want to include the after_body header in the toc. I use the following yaml:
---
author: "name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
bookdown::pdf_book:
toc: false
citation_package: natbib
includes:
before_body: frontpage.tex
after_body: after_body.tex
in_header: preamble.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib, library.bib]
biblio-style: apalike
link-citations: yes
---
In the after_body.tex i have the following:
\backmatter
\begin{titlepage}
\LARGE
\textbf{Enclosed articles I - III}
\end{titlepage}
How do I include the "Enclosed articles I - III" in the toc? I create the toc with \tableofcontents before \mainmatter in the index.Rmd file.
Edit: I also noticed that bookdown creates an empty page with a page number after the \titlepage latex environment. This also happens in the frontpage.tex. Is there any way to remove the numbers on these pages?
I managed to do this by changing the \textbf to \chapter, and include this in the preamble.tex:
\usepackage{fancyhdr}
\pagestyle{fancy}
\setlength{\headheight}{13.6pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{%
\if#mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
As taken from this post

Incorrect conversion from R Markdown to LaTeX

Why does the following R Markdown minimal (non)-working example not compile to PDF?
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
In particular, the problematic conversion happens to -\lfoot{From: K. Grant} and -\cfoot{To: Dean A. Smith}, as seen in the output .tex file:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true
For some reason, both of these lines are converted to true causing
LaTeX error: Missing \begin{document}
thereby preventing the document from compiling to PDF.
Changing \lfoot and \cfoot to just about anything else seems to lead to them being converted correctly. So what's going on here? I take it that there must be a problem with either knitr or pandoc in the conversion process.
NB: I'm not too familiar with R Markdown, and this is a follow-up question to Headers and footers created in Fancyhead not shown in PDF on TeX.SX on Tom's behalf.
The : character is the problem. pandoc seems to be trying to parse the header-includes content as if it were variables, and : is used to separate variables and values. It compiles if you quote the lines in question (don't forget, then, to escape the leading backslash)
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\\lfoot{From: K. Grant}"
- "\\cfoot{To: Dean A. Smith}"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
Stitching the answer here with trial and error, I found that only by deleting the title key and using YAML pipe-denoted multiline string syntax would it compile:
---
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
---
I recommend using pandoc's raw_attribute feature (enabled by default) to mark the raw LaTeX section as such. This is the most robust solution.
header-includes: |
```{=latex}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
```

Resources