Automatically number sections in RMarkdown
Asked Answered
M

2

62

I am trying to create a report in R markdown that has sections ordered

  1. Section 1 Header

    1.1 sub section 1

    1.2 sub section 3

  2. Section 2 Header

    2.1

    2.2 sub section

      2.2.1 another sub section
    

Is there a way get R markdown to generate an ordered list like this?

Mana answered 19/10, 2016 at 0:14 Comment(7)
Just use # Sec 1, ## Sub 1.1 and ### Sub 1.1.1?Quarto
That will work. I was hoping for a method that generated the numbering automatically. That way if I wanted to go back and add in a section I wouldn't have to change the numbering system.Mana
I dont get you. You dont need the numbers I added. They are just for illustrative purposes.Quarto
# Sec prints Sec as a header for me.Mana
Check out the number_sections: true option....Quarto
Thanks! That is exactly what I needed. This is my first time using Rmarkdown so sorry if the question was super basic.Mana
No worries. I'll add it as an answer.Quarto
Q
135

Add the option number_sections: true to your YAML header:

---
title: "My Report"
output: 
  html_document:
    number_sections: true
---

# Main Section

## 2nd Level

### 3rd Level

And voilá, your sections are numbered:

The above text in an image with numbered sections

Quarto answered 20/10, 2016 at 14:57 Comment(1)
Is there a way to reference the automatically numbered section from within the markdown? This question asks how to link to a custom description; I want to get the generated section (or subsection) number.Legit
K
0

By setting "number_sections: true to your YAML header:" it is only possible to add numbers in the published html file, which is not friendly to the code writer.

Now here is a plugin for Rstudio that can fulfill your requirement to add or remove numbers in outlines:

OutlineNum : Automatically number sections for RMarkdown headers/outline in RStudio Have you seen https://github.com/callAgene/OutlineNum

Result of use

Knisley answered 23/10, 2023 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.