[Tex/LaTex] How to format only one section of RMarkdown pdf output using sectsy

markdownsectsty

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

header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \allsectionsfont{\centering}
---


\sectionfont{\left}

## This is a heading

## This is a second heading.

Best Answer

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
header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \allsectionsfont{\centering}
---
\allsectionsfont{\raggedright}
## This is a heading

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

output of code