Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indent numbered sections and subsections in R Markdown (bookdown)?

Tags:

r

r-markdown

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

enter image description here

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)

like image 876
steveb Avatar asked Sep 01 '25 10:09

steveb


2 Answers

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

enter image description here

like image 156
Peter Avatar answered Sep 03 '25 23:09

Peter


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}

enter image description here

P.S. Mr. Yihui Xie, can you shed light on this case? Thanks ;)

like image 41
manro Avatar answered Sep 03 '25 23:09

manro