R markdown toc_depth only certain level - r

in R markdown you can set table of content by this code.
---
title: "Habits"
output:
html_document:
toc: true
toc_depth: 2
---
This way also 1st level of depth is included. I'm curious if there is a way how to set toc_debth ONLY to 2.

Related

How to have `pkgdown` use figure numbers and cross-references in vignettes?

I've created several vignettes for a package, with figures I want to reference in the text.
Using the template for a .Rmd vignette, I can do this by using bookdown::html_document2 as follows in my yaml header:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
Yet, when I build the associated pkgdown site, I don't get figure numbers or cross-references,
done with \#ref(fig:chunk_name).
Is there some magic I can add to my _pkgdown.yml file to have it use the bookdown output format?
Edit: Not sure if this has anything to do with this issue, but my figure chunk labels are of the form topic-figure rather than topic_figure.
E.g.,
```{r, plastic1-HE3D}
#| echo=FALSE,
#| fig.cap="3D HE plot for the plastic MLM"
knitr::include_graphics("fig/plastic-HE3D.png")
```
A solution that seems to work is suggested in https://github.com/r-lib/pkgdown/issues/2201
Essentially, add pkgdown: as_is: true to the yaml headers of vitnettes:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
toc_depth: 2
pkgdown:
as_is: true

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.

Line spacing for table of contents for bookdown PDF

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?

rmarkdown - export table of contents

I have an RMarkdown file used for creating a QC report on other sets of data, and the report is relatively long. My table of contents essentially contains the name of all the checks I run on the data, and I'd like to have the table of contents itself exported into a new document, for outside users to quickly see what checks are run.
Is there any way to export just the TOC to word/pdf/html, or otherwise 'roll up' and export the different sections of an RMarkdown file?
I'm not entirely clear on what you're trying to achieve.
Perhaps you're after a "floating" TOC, like this?
---
title: "Title"
output:
html_document:
toc: true
toc_float: true
---
# Section 1
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
# Section 2
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
For a html_document output format, you can set the body Pandoc variable to a void string:
---
title: "Title"
output:
html_document:
toc: true
pandoc_args: ['-V', 'body=""']
---
This solution will not work for a pdf_document or a word_document.

Resources