Line spacing for table of contents for bookdown PDF - r

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.

I cobbled together an answer from #bretauv's link and another answer on SO - here's the links:
How to change spaces between items in Table of Contents
Chapter(s) before table of contents in Bookdown PDF output
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!

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

bookdown - how to get figure title above figure and figure notes below figure when using word_document2

I am trying to switch from LaTeX to RMarkdown+bookdown when writing a paper that includes tables and figures. One main motivation is to be able to create both Word documents and PDF documents.
For word_document2, how can I add a figure title/caption above the figure as well as figure notes below the same figure?
Here is how I would do it in LaTeX (and it would be convenient to continue using LaTeX as I am transferring everything over from LaTeX to RMarkdown).
\begin{figure}
\caption{Figure Title...}
\label{figure_label}
\vspace*{.2cm}
\includegraphics[width=7in]{figure_name.png}\\
\caption*{Figure Notes...}
\end{figure}
More broadly, is it possible to use LaTeX code with word_document2?
Note that my preamble currently looks like:
---
title: "..."
author: "..."
date: "10/14/2021"
output:
bookdown::word_document2:
number_sections: false
bookdown::pdf_document2:
number_sections: false
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
citation_package: default
bibliography: ../references.bib
always_allow_html: true
---
And I am currently able to get the caption above the figure, but just for the pdf_document2 (see Caption above figure using knitr (LaTeX/PDF)):
```{r figure-label, fig.cap="Figure Title", fig.width=6, fig.height=3.7, fig.align="center"}
...
This doesn't seem to work when creating the Word document.
yihui says in his documentation¹ we can use the officedown² package to influence word layouts. It has the Plot- option topcaption: you can set it to true as shown below.
[¹] https://bookdown.org/yihui/rmarkdown-cookbook/word-officedown.html
[²] https://cran.r-project.org/web/packages/officedown/index.html
---
title: "..."
author: "..."
date: "10/14/2021"
output:
officedown::rdocx_document:
plots:
style: Normal
align: center
topcaption: true
bookdown::pdf_document2:
number_sections: false
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
citation_package: default
---
```{r figure-label, fig.cap="Top Caption", fig.width=6, fig.height=3.7}
plot(rnorm(1000,300,32))
library(officedown)
``
this should give you a docx with figure captions on top.

How to set position of list of figure (lof) in R bookdown YAML

I have a R bookdown whose YAML looks like this:
---
title: "My title"
toc: False
lof: True
author: "the author"
output:
bookdown::pdf_document2:
keep_tex: yes
---
By default it generates a pdf file with the list of figures at the beginning just after the title.
Is there a way to have this list at the end, after the references?
Remove lof: True and add \listoffigures after your references.

How to reference sections in rmarkdown with pdf output

How do I use \label and \ref to link sections in rmarkdown when outputting to pdf please. I have tried various permutations from https://bookdown.org/yihui/bookdown/cross-references.html and https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html with no success.
One attempt
---
title: "Untitled"
output: pdf_document
---
See Section \#ref(sec:label).
# Section One (\#sec:label)
which gives
You can modify your document as follows:
---
title: "Untitled 1"
date: "28 de junio de 2020"
link-citations: yes
output:
pdf_document:
includes:
keep_tex: yes
number_sections: yes
toc_depth: 2
---
\section{This is the first section} \label{section1}
You will be some in \ref{section1}
Then you can see how numbers appear, just add \label{} to your sections and use \ref{} to call. Also, I suggest using \section{}, and modify YAML as I included. Hoping this can help.

R Markdown: change the title of the "References" section?

I am writing a report with R Markdown in which I include references. The problem is that R markdown automatically includes references at the end of the report (without calling them with \printbibliography for example) and the section title is "References". I am writing in French so I would like the title to be "Références", but more generally is there any way to modify the title of that section?
Below is reproducible example:
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Partie 1
\cite{greenwood_financial_1989}
# Partie 2
bla bla
and here is the content of the references.bib file:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Does anybody have a solution?
Here's the solution that combines this answer on doing something similar with bibtex and this one on the TeX required to do it in biblatex, plus #Ralf's comment about adding lang: fr
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
lang: fr
---
In my document, I used :
header-includes:
- \usepackage[french]{babel}
And your Contents section also changes.
I had a similar problem I wanted to place the bibliography above a supplemental section and placing <div id = "refs"></div> where you want the references successfully repositioned the section. I also had no problem renaming the section by simply changing the # title above that section. This did work for PDF output as well as HTML despite appearing to be an HTML only solution, though I was also using the markdown flavored ([#Smith2019]) references in the body of my document I don't know if using the LaTeX style will affect things.

R: Removing the "Chapter" part from the title in bookdown::pdf_book with documentclass: report

consider my header and first section
---
title: "asdf"
author: "asdf"
date: "13 Januar 2018"
documentclass: report
output:
bookdown::pdf_book:
citation_package: biblatex
latex_engine: pdflatex
number_sections: yes
fig_caption: yes
---
# Introduction
Here begins my introduction
I want to remove the automatic "Chapter 1" part, thats sits in front of the actual chapter title (in this case Introduction)
This does not work
header-includes:
\renewcommand{\chaptername}{}
Also using another documentclass does not work, because I need the number of my sections/chapters be represented in the figure captions (e.g Figure 3.1)
What I would like is to end up with a Title 1 Introduction , than 2 Methods and so on.
Should not be to hard, but I can't figure it out.
Thanks
Greg
You can use
subparagraph: true
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
together with
\usepackage{titlesec}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
in preamble.tex (c.f. https://tex.stackexchange.com/questions/10326/how-to-set-the-chapter-style-in-report-class). You have to make sure that the LaTeX package titlesec is installed in your TeX system.
At the time of writing subparagraph: true was needed since the used pandoc LaTeX template redefined \paragraph and \subparagraph in a way interfering with titlesec. This has changed since then. at least with rmarkdown version 2.1 one no longer needs subparagraph: true. thanks #MarkNeal for noticing this!

Resources