Search code examples
latexr-markdownbookdowntableofcontents

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}

This is what the document looks like


Solution

  • 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. 
    

    enter image description here