Prefix section headings with a letter

I want to add a letter prefix to my section (and subsection) numbers, so that 101, 101.1, 101.1.1 become S101, S101.1, S101.1.1 but I'm not sure how to implement. Document headers reproduced below. I just use the #section# , ##subsection## and ###subsubsection### tags.


documentclass: article
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: '1'
word_document:
toc: no
toc_depth: '1'
html_document:
df_print: paged
number_sections: yes
toc: no
toc_depth: '1'
pandoc_args:
- --number-sections
- --number-offset=300

I think Pandoc does not support this by default for now. You'll need to apply customized solution according to your desired output.
Which format do you want ?

  • For HTML, it would be some JS and CSS
  • For PDF, it would be some latex in header to modify the number
  • For Word, it would a reference document with the Title1 style numbered with letter.(the easiest )

You'll find a some details is this answer by @rlesur that is still up to date on SO

Otherwise, you'll need to hack I guess by pre processing the markdown to add the letters and not letting pandoc do it itself, or using lua filters for pandoc but that is not the easiest.

hope it helps.

1 Like

Thanks! Looking to export to PDF format.

You'll need to do some stuff with latex then. Did you try to play with answer in SO ?

Got it working. Thanks for the tips. Also thanks to https://www.stat.berkeley.edu/~paciorek/computingTips/Customizing_numbering_pages.html
Solution was as follows:


title: "Document Title"
documentclass: article
date: "July 1, 2020"
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: '1
pandoc_args:
- --number-sections
- --number-offset=300
fontsize: 11pt
header-includes:

  • \usepackage{fancyhdr}
  • \usepackage{lipsum}
  • \usepackage{lastpage}
  • \usepackage{booktabs}
  • \pagestyle{fancy}
  • \fancyhf{}

Add the S before section heading

  • \renewcommand{\thesection}{S\arabic{section}}

version: 1

header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \usepackage{lastpage}
- \usepackage{booktabs}
- \pagestyle{fancy}
- \fancyhf{}
# Add the S before section heading
- \renewcommand{\thesection}{S\arabic{section}}

Glad it worked!

Does this works? I surprised because Pandoc manual says it is only for HTML outputs. https://pandoc.org/MANUAL.html#option--number-offset
Also, number_sections: yes is the same as adding --number-sections, isn't it ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.