Increment section numbers in PDF output

百般思念 提交于 2019-12-22 09:59:36

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/33395773/increment-section-numbers-in-pdf-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!