Line spacing for table of contents for bookdown PDF

Viewed 1848

I am using bookdown in R to create a PDF document. I have specified the line spacing as 1.3 in the index.Rmd which has worked perfectly for the main body of text, including tables, which is fine by me. However, it has not changed the table of contents, or list of figures/tables, which instead have the default spacing. Of course, bookdown generates these additions in the background, so to me it's not straightforward to add raw LeTeX commands to make the change.

My index.Rmd looks like this:

---
title: "This is my book title"
author: "My name"
date: "March 2020"
site: bookdown::bookdown_site
output: bookdown::pdf_book
documentclass: book
description: "Example"
linestretch: 1.3
toc: true
lot: true
lof: true
---

And my _output.yml looks like this:

bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
  toc_depth: 3

Any advice or suggestions would be greatly appreciated.

2 Answers

I cobbled together an answer from @bretauv's link and another answer on SO - here's the links:

And here's the resulting advice - to reduce line spacing between lines in the table of contents, first tell your .Rmd to NOT create a table of contents in the YAML portion of your .Rmd, e.g.,

---
title: "My title"
output: 
 bookdown::pdf_document2:
    latex_engine: xelatex
    toc: FALSE            #<--- here's the line you want to ensure says FALSE
    fig_caption: yes
mainfont: Arial
fontsize: 10pt
---

Then, the first chunk in your .Rmd can specify that you do actually want to create a TOC, but it tells your .Rmd to change the line spacing for that section (say, 0.7), then change it back to the line spacing you prefer for the rest of your document (say, 1.2). The chunk could look like this:

```{=latex}
% Trigger ToC creation in LaTeX
\renewcommand{\baselinestretch}{0.7}\normalsize
\tableofcontents
\renewcommand{\baselinestretch}{1.2}\normalsize
```

Edit:

In response to @bretauv's answer and to aid in troubleshooting, I'm posting the result of their code on my machine - except I've changed linestretch to 0 and added some body text to show the linestretch is clearly different between TOC and body. Note that one might desire no line spacing between ANY lines - table of contents or body text; however, the linestretch is clearly only applied to the body text. See spacing between entries in the table of contents.

@bretauv, does this happen on your machine with linestretch = 0? Thanks for looking into this with us!

PDF document with table of contents and body text

Here's the output if I regroup index.Rmd and my_output.yml in a unique document (I put linestretch:2 just to clearly show that linespacing is applied to the TOC too):

---
title: "This is my book title"
author: "My name"
date: "March 2020"
site: bookdown::bookdown_site
output: 
  bookdown::pdf_book:
    latex_engine: xelatex
    citation_package: natbib
    keep_tex: yes
    toc_depth: 3
linestretch: 2
toc: true
lot: true
lof: true
---

# Section 1

## Subsection 1

## Subsection 2


# Section 2

## Subsection 1

## Subsection 2


# Section 3

## Subsection 1

## Subsection 2

Is this okay? If not, what do you want to change?

Related