2

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.

Bonaire
  • 23
  • 4
  • Perhaps you keep .tex files you could add in LaTeX command that handles spacing in TOC? – Roman Luštrik Mar 25 '20 at 11:07
  • In `index.Rmd`, try to include some LaTeX commands, with `header-includes` for example. See [here](https://tex.stackexchange.com/questions/56546/how-to-change-spaces-between-items-in-table-of-contents) to know how to increase TOC space in LaTeX – bretauv Mar 25 '20 at 11:19
  • Can you add the resulting .tex file to your question? – samcarter_is_at_topanswers.xyz Mar 25 '20 at 11:25
  • @bretauv Adding LaTeX commands such as `\renewcommand{\baselinestretch}{1.3}\normalsize` in the header-includes amends the line spacing in the TOC successfully, but then the page numbering in the TOC is not correct. – Bonaire Mar 25 '20 at 12:13
  • What is the expected output? @Nova's answer shows how to put different line spacing for toc than for the body of text, but I thought the issue here was that the linespacing of 1.3 was simply not applied on the toc, which is strange because when I regroup both `index.Rmd` and `my_output.yml` together, the linespacing is correctly applied everywhere. Can you clarify what you expect? – bretauv Jun 08 '20 at 19:51
  • @bretauv for reference, my linestretch line in the yaml is not applied to my TOC if I have it there. In the TOC the entries are separated by a full space and it's ugly! – Nova Jun 08 '20 at 20:10
  • @Nova I have made an answer below just to show the output when we regroup both files, do you have the same result? What do you find ugly? – bretauv Jun 08 '20 at 20:19

2 Answers2

6

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

Nova
  • 5,423
  • 2
  • 42
  • 62
  • Same thing for me. If I understand well, you want to reduce the space between the two lines in TOC so that it appears as if linestretch was 0, right? – bretauv Jun 09 '20 at 12:48
  • Exactly, and my answer above does achieve that, but I'd love to learn the "right way" to do it! – Nova Jun 09 '20 at 12:52
  • 1
    Well your answer seems fine to me, I don't think there's a "right way" of putting a different linespacing for TOC than for the body of text. I guess you could put it in a separate .tex file to improve the readibility of the .Rmd document. I thought that the OP's problem was that linespacing was not applied everywhere with `linestretch` in YAML. My answer shows that it is applied in the whole document, not just in body of text, and your answer shows how to put different linespacing, which is useful too. – bretauv Jun 09 '20 at 13:02
0

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?

bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Fun to work with you on this. I've added an edit to my answer to try and see if we're experiencing different behavior, check it out. – Nova Jun 09 '20 at 12:42