Search code examples
latexyamlknitrr-markdownpandoc

Increment section numbers in PDF output


I'm trying to increment the section headers in my rMarkdown (PDF output) file by 1. Instead of # First resulting in 1 First, I'd like 2 First.

I found a way to define the offset in html_output using the following syntax, but it doesn't work for pdf_output.

---
title: "Untitled"
author: "author"
date: "date"
output:
  html_document:
    toc: true
    pandoc_args: [
      "--number-sections",
      "--number-offset=1"
    ]
---
# First Header
# First SubHeader

This results in

2   First Header
2.1 First Subheader

However, this syntax does not work for PDF documents:

---
title: "Untitled"
author: "author"
date: "date"
output:
  pdf_document:
    toc: true
    pandoc_args: [
      "--number-sections",
      "--number-offset=1"
    ]
---

#   First Header
## First Subheader

This results in

1   First Header
1.1 First Subheader

Section headers are not incremented by 1.

According to PANDOC documentation, number-offset only exists for HTML documents.

--number-offset=NUMBER[,NUMBER,…] Offset for section headings in HTML output (ignored in other output formats). The first number is added to the section number for top-level headers, the second for second-level headers, and so on. So, for example, if you want the first top-level header in your document to be numbered “6”, specify --number-offset=5. If your document starts with a level-2 header which you want to be numbered “1.5”, specify --number-offset=1,4. Offsets are 0 by default. Implies --number-sections.

How can I increment section headers in PDF documents?

Thank you!


Solution

  • By default, pandoc does PDF generation by generating a LaTeX file first...

    $ echo '# first' | pandoc -t latex
    \section{first}\label{first}
    

    To tell LaTeX to start with a different number, you can include raw TeX in your markdown document, like:

    \setcounter{section}{2}
    
    # my title