How can I add a table of contents in R Markdown? - r

I'm having a hard time trying to add a table of contents in R Markdown.
I want the table of contents to be on the left side of the document.
I tried this code, but it didn't work for me (when I knitr the document, it works fine, but the TOC is not available):
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
How can I solve my problem?

It is important to indent toc, e.g.
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
# h1
# h2
# h2.2

Related

RMarkdown using readthedown template. self_contained: true seems do not work

I'm referring to the template readthedown provided by the package rmdformats version 1.0.4 executed by R 4.2.1 on Windows 11
I'm running an old script with the following YAML section
---
title: "Title"
subtitle: "SubTitle"
author: "Author"
date: "(`r format(Sys.time(), '%d %b, %Y')`)"
output:
rmdformats::readthedown:
toc_depth: 5
self_contained: true
thumbnails: false
lightbox: true
gallery: true
highlight: tango
css: custom.css
---
The compilation process reach the end without errors but generating the output file (Ex. report.html) and a related folder named for Ex. report_files that contain all the images generated run-time by the rmarkdown chunks.
A few version updates ago the image folder was not generated
The statement self_contained: true it seems by masked or unused.
If you have the global option cache set as TRUE in your setup chunk, either remove that option (then it will default to FALSE) or set the option as FALSE.
Having cache set as TRUE is a possible reason to generate the folder containing necessary images.
Therefore, if you run the following reprex, you will see that no extra folder is created in your working directory.
Reprex
---
title: "Title"
subtitle: "SubTitle"
author: "Author"
date: "(`r format(Sys.time(), '%d %b, %Y')`)"
output:
rmdformats::readthedown:
toc_depth: 5
self_contained: true
thumbnails: false
lightbox: true
gallery: true
highlight: tango
---
```{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = FALSE)
```
## Some Plots
```{r}
plot(rnorm(50), runif(50))
```
```{r}
plot(rnorm(50), runif(50))
```
```{r}
plot(rnorm(50), runif(50))
```

Using custom LaTeX class changes numbering in R Markdown

I recently discovered that you can include your own LaTeX class in an R-Markdown doc to change the appearance of the PDF. Here is a minimal example:
R Markdown
---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: book
output:
pdf_document:
citation_package: natbib
toc: yes
toc_depth: 3
number_sections: true
fontsize: 12pt
---
# A
## AA
### AAA
### AAA
## AB
# B
This works as intended.
But when I define my own class, the numbering is preceded by 0s and the page numbering is off.
myclass.cls
I place a file called "myclass.cls" in the same direcoty as the RMD file above and change documentclass: myclass:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{glasgowthesis}
\LoadClass{book}
My understanding is that this should simply call the same class as above but the file now looks like this:
Maybe somebody can give me a hint what I'm doing wrong. I would like to copy the book class 1:1 before starting to change things.
I found the solution in the bookdown book and wanted to share it in case anyone wanders off to this question through google etc.
Note that when you change documentclass, you are likely to specify an
additional Pandoc argument --top-level-division=chapter so that Pandoc
knows the first-level headers should be treated as chapters instead of
sections (this is the default when documentclass is book)
So this YAML header solved the issue:
---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: myclass
output:
pdf_document:
pandoc_args: --top-level-division=chapter
citation_package: natbib
toc: yes
toc_depth: 3
number_sections: true
fontsize: 12pt
---
# A
## AA
### AAA
### AAA
## AB
# B

How can I modify yaml instructions outside of the document I am rendering

I'd like to rmarkdown::render a R document without indicating the yaml options within the document itself.
Ideally that could be an argument on rmarkdown::render or knitr::spin like what you can do to pass params (see the Rmarkdown reference book). Typically I'd like author, date and the output options too.
I think this is possible because spining the following document without specifying anything I get the following output (so there must be a template of default args that I can hopefully change)
As an example, how could I do to render a document that would give me the same output as say the below (but of course without specifying the yaml in the document ie no yaml whatsoever in the document)
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---
#' # Title
Hello world
#+ one_plus_one
1 + 1
You can pass yaml options as parameters too. For example:
---
params:
title: "add title"
author: "add author"
output: pdf_document
title: "`r params$title`"
author: "`r params$author`"
---
This is my document text.
Then, in a separate R script:
rmarkdown::render("my_doc.rmd",
params=list(title="My title",
author="eipi10"))
You could cat a sink into a tempfile.
xxx <- "
#' # Title
Hello world
#+ one_plus_one
1 + 1
"
tmp <- tempfile()
sink(tmp)
cat("
---
title: 'Sample Document'
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---", xxx)
sink()
w.d <- getwd()
rmarkdown::render(tmp, output_file=paste(w.d, "myfile", sep="/"))

Impossible to cross-referring figures and tables with `beamer_presentation` option in `knitr`

Why does \#ref() notation fail to operate with beamer-presentation?
The following question may remind you some questions on cross-reference when knitting PDF document, e.g. this, but the methods introduced in the answers have not help me when I make beamer-presentations.
Now I'm confused because \#ref(fig:label-to-refer-figure) and \#ref(tab:label-to-refer-table) notation to refer a figure/table does not work when I am knitting an .Rmd file with the option output: beamer_presentation. As shown in the following images, the raw codes for the cross-reference appear on the outputted PDF file and I cannot refer the figure/table number. Although the citations go well even in the listed environment as well as in plain text field, cross-reference for figure/table number does not properly take effect.
My environment
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
knitr_1.20
rmarkdown_1.10
RStudio v1.2.1206 (Preview Version) <- I prefer this for this reason
MWEs
The MWE I post here is created from the following sources: test-beamer.Rmd and myref.bib.
test-beamer.Rmd
---
title: "Test"
subtitle: |
| subtitle,
| with a line break
author: |
| CLR
| Rafael
institute: |
| Now I'm here,
| Now I'm there
date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
output:
beamer_presentation:
keep_tex: yes
latex_engine: lualatex
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
toc: true
#toc_depth: 3
#number_sections: TRUE
fig_caption: TRUE
dev: cairo_pdf
#extra_dependencies: subfig
citation_package: natbib
slide_level: 2
bibliography: bibs/myref.bib
biblio-style: apa
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## The only thing
With Table \#ref(tab:under-pressure-table), #test-master shows that Figure \#ref(fig:under-pressure) depicts...
## Slide with Bullets in which I want to refer a figure
- \#ref(fig:under-pressure)
- #test-master
- \#ref(tab:under-pressure-table)
## Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
## Slide with Plot
```{r under-pressure, fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth"}
plot(pressure)
```
## Slide with Table
```{r under-pressure-table, caption = "This is a table"}
knitr::kable(pressure)
```
## More extraordinary
With Table \#ref(tab:under-pressure-table), #test-master shows that Figure \#ref(fig:under-pressure) depicts...
EDIT: I added fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth" to the figure chunk, and caption = "This is a table" to the knitr::kable(). Without these codes, neither the caption nor table/figure numbers appear at all... However, the problem persists even after giving them to the entire .Rmd file, unless you carry out #Yihui's answer.
myref.bib
#master{test-master,
author = {Freddie Mercury and Brian May and John Deacon and Roger Taylor},
title = {Bohemian {R}hapsody: {W}e are the champions},
school = {{Queen}},
year = {2018},
address = {London}
}
The \#ref() notation is a feature of bookdown only. To port this feature to general R Markdown documents, you may set the base_format option of a certain bookdown output format, e.g.,
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
See Section 3.4 of the bookdown book for details.
The completed yaml section which suits for the MWE of this question may be like this:
---
title: "Test"
subtitle: |
| subtitle,
| with a line break
author: |
| CLR
| Rafael
institute: |
| Now I'm here,
| Now I'm there
date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
output:
bookdown::pdf_book:
base_format: "function(..., number_sections) rmarkdown::beamer_presentation(...)"
number_sections: true
keep_tex: yes
latex_engine: lualatex
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
toc: true
fig_caption: TRUE
dev: cairo_pdf
#extra_dependencies: subfig
citation_package: natbib
slide_level: 2
bibliography: bibs/myref.bib
biblio-style: apa
always_allow_html: yes
---

How can we pass pandoc_args to yaml header in rmarkdown?

I'm using rmarkdown to produce a word (.docx) report. I would like to change the header of the toc. This seems possible as pandoc_args can be passed as options in the yaml header in the case of a doc file (1). But I'm not doing it right. Could anyone provide a working example ?
(1) pandoc.args is included in the rmarkdown possible options and in the pandoc manual, there is a toc-title option
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: toc-title="Table des matières"
---
# One section
# Another
This produces :
The title of the table of contents is document metadata, so you can set it with YAML metadata block.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
toc-title: "Table des matières"
---
Or passed it in with the -M command-line flag.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: [
"-M", "toc-title=Table des matières"
]
---

Resources