I am writing a manuscript with rmarkdown.
If I want to number all sections, I can use YAML like this
---
title: "My Report"
output:
html_document:
number_sections: true
---
See Automatically number sections in RMarkdown
But I only want to number some sections, so my document looks like
Abstract
1. Introduction
2. Methods
3. Results
References
Is there anyway to do this?
From the Pandoc User Guide you want to add the .unnumbered attribute to the header. This is done using:
# My header {.unnumbered}
or the shortcut
# My header {-}
For example, using the following document
---
title: "My Report"
output:
html_document:
number_sections: true
---
# Abstract {-}
# Introduction
# Methods
# Results
# References {-}
The HTML produced renders as:
Related
I am fairly new to RMarkdown and trying to apply some LaTeX logic here. Currently, I am creating a PDF presentation with RMarkdown (beamer_presentation). Is it possible to change the font size and color of automatic citations ([#shortname]) so that e.g., all citations appear slightly smaller than the normal text and in gray?
In LaTeX beamer presentations, I managed to re-define the cite command and apply this. I was not yet able to recreate this in RMarkdown however.
When I was trying to manually (I'd much prefer automatic a solution) change the font color of citations \textcolor{gray}{[e.g., #shortname]} (also $\textcolor{gray}{[e.g., #shortname]}$), I received this error message:
I was unable to find any missing LaTeX packages from the error log slides.log. ! Missing $ inserted. $ l.182 \end{frame} Try to find the following text in slides.Rmd: \end{frame}
You may need to add $ $ around a certain inline R expression r in slides.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
The link in the error message did not help me either.
I am grateful for your help! I'll include a MWE below.
Till
---
title: "A title"
author: "Till"
date: 25.07.2022
output:
beamer_presentation:
incremental: true # bullet points on multiple pages
toc: false # add generated page listing sections
slide_level: 2 # depth level for render content
latex_engine: lualatex
keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes: \usepackage{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) # for debugging
```
# First Section
## Example Slide
- Some text which will be followed by a citation \textcolor{gray}{[e.g., #shortname]}
- Some more text, now without a citation
The literature.bib file could look somewhat like this:
#article{shortname,
title = {citation title},
journaltitle = {Journal},
shortjournal = {Journal},
author = {Lastname, Firstname},
date = {2022},
}
The problem with your \textcolor approach is that you can't use markdown syntax in the argument of a latex macro. You can avoid this problem by using \begingroup \color{gray} [e.g., #shortname] \endgroup:
---
title: "A title"
author: "Till"
date: 25.07.2022
output:
beamer_presentation:
incremental: true # bullet points on multiple pages
toc: false # add generated page listing sections
slide_level: 2 # depth level for render content
latex_engine: lualatex
keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes: \usepackage{xcolor}
---
#```{r setup, include=FALSE}
# I did not know how to include R-Chunks in the code block withouth breaking the formatting, so just remove the Hashtags before the three ticks and this endless comment
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) # for debugging
#```
# First Section
## Example Slide
- Some text which will be followed by a citation \begingroup \tiny\color{gray} [e.g., #shortname] \endgroup
- Some more text, now without a citation
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?
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.
I defined keywords in the .Rmd file, but they are not visible in the output PDF.
Current Output
Expected results
Current .Rmd
First lines of .Rmd file looks as follows:
---
title: "No keywords within the output file"
abstract: "This is sample text for abstract. Generally speaking, I would like to show keywords list below an abstract (as in case of the linked example)"
keywords: "keywordA, keywordB"
author: "Mateusz Kędzior"
output:
bookdown::pdf_document2:
keep_tex: true
number_sections: yes
toc: false
base_format: rticles::elsevier_article
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Elsevier article
This is an R Markdown document.
I'm trying to prepare an Elsevier article.
I wonder if base_format is actually doing any work in your example (the output looks the same with and without base_format). Since base_format is an argument to pdf_book, consider changing your YAML header to
---
title: "No keywords within the output file"
author:
- name: "Mateusz Kędzior"
abstract: "This is sample text for abstract. Generally speaking, I would like to show keywords list below an abstract (as in case of the linked example)"
keywords: "keywordA, keywordB"
output:
bookdown::pdf_book:
keep_tex: true
number_sections: yes
toc: false
base_format: rticles::elsevier_article
---
which gives you the following output:
Alternatively, add keywords to the abstract:
abstract: "This is sample text for abstract. Generally speaking, I would like
to show keywords list below an abstract (as in case of the linked example) \\par
\\textbf{Keywords:} a, b"
to get
I am using RStudio for writing markdown documents and want to add Table of Contents (TOC) at top of the documents so that the user could click the relevant section for reading. There were some relevant examples on rpubs but now I can't seem to find them. Please note that I don't use pandoc and am quite new to Rmd & knitr. Is there any way to add TOCs without using pandoc? If using pandoc is must then which functions are relevant?
EDIT
Here's a small sample page:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
Header 1
---------------
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
## Header 2
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
### Header 3
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I tried running this in RStudio v 0.98.864 and it worked! but sadly it didn't work on 0.98.501 and 0.98.507. I am working on my thesis in 0.98.501 and after updating RStudio, some of my analyses didn't work. So, I reverted back to 0.98.501.
What should I do now? I really want TOCs but without harming the outputs of other analyses.
The syntax is
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.
Syntax with more options:
---
title: "Planets"
author: "Manoj Kumar"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true # table of content true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
number_sections: true ## if you want number sections at each table header
theme: united # many options for theme, this one is my favorite.
highlight: tango # specifies the syntax highlighting style
css: my.css # you can add your custom css, should be in same folder
---
If you are using pdf_document, you might want to add table of contents in a new page, which toc: true does not allow. It puts the table of contents right after the document title, author and date--because it is in yaml.
If you want to have it in a new page, you have to use some latex language. Here is what I did.
---
title: \vspace{3.5in}"Title"
author: "Name"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: true
number_sections: true
---
\newpage # adds new page after title
\tableofcontents # adds table of contents
\listoffigures
\listoftables
\newpage
So, after yaml (the chunk between ---), I added a new page using \newpage, then a table of contents using \tableofcontents, a list of figures using \listoffigures, a list of tables \listoftables, and a new page before everything else.
Note, \vspace{3in} in the title adds vertical space of 3 inch from the top before printing yaml (title, etc.).
Read more here: https://www.sharelatex.com/learn/Table_of_contents