pandoc "titleformat" ruler

Viewed 372

I have an issue with YAML datablock. Given the following pandoc document:

---
papersize: a4
geometry: "left=1.5cm,right=1.5cm,top=3cm,bottom=3cm"
fontsize: 12pt
# https://stackoverflow.com/a/43659664/2069099
subparagraph: yes
#documentclass: extarticle
header-includes:
    - \usepackage[explicit]{titlesec}
    - \titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}

---

Hello World
=========== 

Lorem ipsum

I correctly get my title aligned on the right. Now, suppose I need a full-width ruler below the title, as stated in https://tex.stackexchange.com/a/84062, one need to modify \titleformat statement as:

---
papersize: a4
geometry: "left=1.5cm,right=1.5cm,top=3cm,bottom=3cm"
fontsize: 12pt
# https://stackoverflow.com/a/43659664/2069099
subparagraph: yes
#documentclass: extarticle
header-includes:
    - \usepackage[explicit]{titlesec}
    - \titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}[\titlerule\vspace*{4pt}]

---

Hello World
===========    

Lorem ipsum

but this leads to the following error:

Error producing PDF.
! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.49 {[

and, indeed, the tex file shows weird code:

...
\usepackage[explicit]{titlesec}
\titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}

{[}\titlerule\vspace*{4pt}{]}

\date{}

\begin{document}
...
2 Answers

OK, passing plain Latex in YAML block solved the issue:

--
papersize: a4
geometry: "left=1.5cm,right=1.5cm,top=3cm,bottom=3cm"
fontsize: 12pt
# https://stackoverflow.com/a/43659664/2069099
subparagraph: yes
#documentclass: extarticle
header-includes:
  - \usepackage[explicit]{titlesec}
  - |
    ```{=latex}
    \titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}[\titlerule\vspace*{4pt}]
    ```  
---

Hello World
===========    

Lorem ipsum    

Pandoc parses part of that line as Markdown. So when the line is written as LaTeX, special characters are escaped to ensure that is produces the same text as the input. This is undesired here, and the best solution is to explicitly mark the line as LaTeX:

header-includes:
    - \usepackage[explicit]{titlesec}
    - `\titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}`{=latex}

One could also mark the whole header includes like so:

header-includes: |
  ```{=latex}
  \usepackage[explicit]{titlesec}
  \titleformat{\section}[hang] {\filleft\huge\sc} {label}{20pt}{#1}
  ```
Related