# How to format only one section of RMarkdown pdf output using sectsy

I am creating a pdf using R-markdown. I want ALL section headings to be centered (using sectsty package),except the first one which should be left aligned. How can I do this ? How do I use sectsty for only one section ?

Here is a minimal working example :

---
date : 2018-April-25
output:
pdf_document:
toc: true
toc_depth: 1
title: "Test document"
fontsize: 12pt

- \usepackage{booktabs}
- \usepackage{sectsty} \allsectionsfont{\centering}
---

\sectionfont{\left}

## This is a heading

## This is a second heading.

• Welcome to TeX.SX! – Bobyandbob Apr 25 '18 at 11:48
• There is no \left command (well there is, but it doesn't do what you think it does). Just use \sectionfont{} – Alan Munn Apr 25 '18 at 15:10

What you tried didn't work because the \left macro is a mathmode macro for making an extensible left delimiter. Your document should have generated an error if compiled, but maybe it gets hidden from you.

Try the following:

---
title: "secsty-test"
date: "4/25/2018"
output: pdf_document
- \usepackage{booktabs}
- \usepackage{sectsty} \allsectionsfont{\centering}
---
\allsectionsfont{\raggedright}
## This is a heading

\allsectionsfont{\centering}
## This is a second heading.


• When I do \sectionfont{} in place of \sectionfont{\left} it makes no difference. When I remove the empty line between \sectionfont{} and the first heading it DOES left align the first heading, but the second heading is bold and the first one is not. Also it displays the ## before the first heading. How can I make the first and second heading exactly the same except for the left alignment? – user2338823 Apr 26 '18 at 3:27
• @user2338823 Sorry I tested with a LaTeX document not an Rmd document. I've updated with a sample Rmd. Is this what you want? – Alan Munn Apr 26 '18 at 3:46
• Yes, this is what I wanted. Query : can I not modify only ONE section using sectionfont ? Must I modify all future sections ? – user2338823 Apr 26 '18 at 4:02
• @user2338823 I don't think so. – Alan Munn Apr 26 '18 at 4:18
• Then what is the difference between sectionfont and allsectionsfont ? – user2338823 Apr 26 '18 at 5:00