I know how to indent text such as bullets and numeric lists, but I am also interested in indenting numbered sections and subsections as well. Given the following code, it renders to the screen shot following the code. The table of contents has subsections indented and I would like to indent the content body as well.
Is there a way to indent the subsections in the body of the document as well? See the Desired Output Example item afterwards.
---
title: "R Markdown Example With Numbered Sections"
output:
bookdown::pdf_document2:
toc: true
toc_depth: 6
number_sections: true
---
# Section A
## Level 2 A
### Level 3 A
#### Level 4 A
## Level 2 A
# Section B
## Level 2 B
Rendered without indented subsections
Desired Output Example
... table of contents as above ...
1 Section A
1.1 Level 2 A
1.1.1 Level 3 A
1.1.1.1 Level 4 A
1.2 Level 2 A
2 Section B
2.1 Level 2 B
EDIT: 2021-12-15
The solution for this by #Peter worked for me on the Mac but required a workaround for the Linux system I am using. I found the issue with the solution not working on my Linux machine was the result of a bug in version 2.10 of titlesec (which is what I have). The fix can be either to update titlesec or use a workaround; both are described in this link: titlesec: loss of section numbering with the new update (2016/03/15)
Not entirely sure if it is the heading in the table of contents (toc) or or the headings in the body of the document that want to be indented.
You can indent the headings in the text using the latex titlesec package and the titlespacing command (as originally posted, then deleted following comments from #manro).
However, this only works for the first three levels out of the box, which in latex-land are generally considered enough. Updated comment #manro has found a solution to this.
Indentation in the toc is more straight forward: its a case of getting the correct indentation for the yaml commands...
---
title: "R Markdown Example With Numbered Sections"
output:
bookdown::pdf_document2:
toc: true
toc_depth: 6
number_sections: true
header-includes:
- \usepackage{titlesec}
---
\titlespacing{\section}{0pt}{*4}{*1.5}
\titlespacing{\subsection}{20pt}{*4}{*1.5}
\titlespacing{\subsubsection}{40pt}{*4}{*1.5}
\titlespacing{\subsubsubsection}{60pt}{*4}{*1.5} # does not work
# Section A
## Level 2 A
### Level 3 A
#### Level 4 A
## Level 2 A
# Section B
## Level 2 B
I don't know, but in
bookdown::pdf_document2:
... toc_depth can be only max = 2.
Maybe, it is a bug? Or I did something wrong?
But, for
output: pdf_document
... we can make this solution (Peter, I remember your contribution!)
---
title: "Toc is Toc"
header-includes:
- \usepackage{titlesec}
output:
pdf_document:
latex_engine: lualatex
toc: yes
number_sections: yes
toc_depth: 4
documentclass: article
---
\titlespacing{\section}{0pt}{*4}{*1.5}
\titlespacing{\subsection}{20pt}{*4}{*1.5}
\titlespacing{\subsubsection}{40pt}{*4}{*1.5}
\titlespacing{\paragraph}{60pt}{*4}{*1.5}
\section{I don't know}
\subsection{Why}
\subsubsection{In bookdown}
\paragraph{TocDepth is only two}
P.S. Mr. Yihui Xie, can you shed light on this case? Thanks ;)
Related
I was looking at the documentation for the kableExtra package (https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html#column__row_specification) and love the table of contents that's being used here.
The default way, to add in the table of contents in the header options creates a more rigid TOC at the top of the page. This is shown with the following parameters in the document header -
title: "Planets"
author: "Manoj Kumar"
date: "March 3, 2016"
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
Does anyone know how to replicate the example on the kableExtra documentation?
Thanks,
Jim
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'm using RMarkdown to take course notes and each major section corresponds to a given lecture. I'd like to have the section headers automatically formatted as
"Lecture 1", "Lecture 2", etc. Here's basically what I'm looking for.
Lecture 1
Going over syllabus.
Lecture 2
Actually learning some stuff
However, when I use RMarkdown's default settings I get the following format (with section numbers preceding names):
1 Lecture
Going over syllabus.
2 Lecture
Actually learning some stuff.
How do I get the automatic numbering to either:
(1) follow the name (e.g. "October 1st - Lecture 1")
or
(2) be referenced in the name (e.g. with some sort of pseudocode "October 1st - Lecture {%section_number%}")?
Below is a minimal reproducible example of RMarkdown code which can be knit to PDF.
---
title: "Course_Notes"
output:
pdf_document:
number_sections: true
---
# Lecture
Going over the syllabus.
# Lecture
Actually learning some stuff
According to a TeX answer on altering the section title format, you can use the titlesec TeX package to change the section formatting as follows:
\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}
However, titlesec doesn't work out of the box with Pandoc: another Q&A shows that you need to add subparagraph: yes to the YAML header to get it working.
Putting it together, the following modifications should get you the result you're after:
---
title: "Course_Notes"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage[explicit]{titlesec}
- \titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}
subparagraph: yes
---
# Lecture
Going over the syllabus.
# Lecture
Actually learning some stuff
This is a follow up question to this. I'd like to put a header within a {.tabset} that does not get referenced by the TOC or break the tabbing. For example:
---
output:
html_document:
toc: true
toc_float: true
---
# Tabset 1 {.tabset}
## A
Text under tab A
<h1>Don't want this in TOC</h1>
## B
Text under tab B
Produces this:
Is there anyway to format text like a header but not have it referenced in the TOC?
Going by the comments there already seems to be an issue dealing with this particular problem.
However, a simple workaround would be to use a normal <p> tag but style it like a header. So for your particular example you could do the following:
---
output:
html_document:
toc: true
toc_float: true
---
# Tabset 1 {.tabset}
## A
Text under tab A
<p style="font-weight:600; font-size:36px">Paragraph that looks like a header.</p>
## B
Text under tab B
According to this link a value of 600 for font-weight qualifies as semi-bold which is what Bootstrap seems to be using for their <h1> headers.
Here is simple RMarkdown document with two sections and two images.
---
output:
bookdown::html_document2: default
bookdown::word_document2: default
bookdown::pdf_document2: default
---
\newpage
# Part 1
Part 1 starts here. See Figure \#ref(fig:fig1-1)
![(\#fig:fig1-1) expected to be Figure 1.1.](/usr/lib/rstudio/www/images/rstudio.png)
# Part 2
Part 2 starts here. See Figure \#ref(fig:fig2-1)
![(\#fig:fig2-1) expected to be Figure 2.1.](/usr/lib/rstudio/www/images/rstudio.png)
I expect that two images will render with Knit to the following numbering - first is Figure 1.1, second is Figure 2.1. But I get this rendering only in html_document2 (see image below):
I use latest RStudio 1.1.414 with latest bookdown from Git (38efc82).
I have two questions here:
Why I don't have Figure 1.1 and Figure 2.1 in Word or PDF?
How can I get Figure 1.1 and Figure 2.1 in Word or PDF?
I can only address PDF output via LaTeX. By default the LaTeX documentclass article is used which uses continuous figure numbering. If your document is so long that it makes sense to number the figures within each top-level section, then you might want to use report or book instead, e.g.:
---
documentclass: book
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
Alternatively you can use some LaTeX package to change the formatting of figure numbers, e.g.:
---
header-includes:
\usepackage{chngcntr}
\counterwithin{figure}{section}
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
An alternative would be the \numberwithin command from amsmath in case you are using that package anyway.