Change the style of chapters (sections) of the .pdf document from rstudio with knitr package LATEX “titlesec”

knitrrmarkdownrstudiotitlesec

I try to style the chapters (sections) of an article document (documentclass: article) using the latex titlesec package, but I don't get any results, I mean that when I configure the document there is no change.

In the YAML header of rmarkdown

header-includes:
## Style Chapter - package "TITLESEC"
  - \usepackage[T1]{fontenc}
  - \usepackage{titlesec, blindtext, color}

Outside the YAML header


<!-- TITLESEC - Estilo CAPÍTULOS -->
\definecolor{gray75}{gray}{0.75}
\newcommand{\hsp}{\hspace{20pt}}
\titleformat{\chapter}[hang]{\Huge\bfseries}{\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}
\titleformat{\section}[hang]{\Huge\bfseries}{\thesection\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}

In the .tex file the code of the chapter (section):


\hypertarget{name-section}{%
\section{Name Section}\label{name-section}}


Best Answer

As you noted, there are no chapters but sections in an article document class. Therefore, it's funny that you try to redefine a command that does not exist. On the other hand, it is not clear to me what are you trying to do with \titleformat, but I guess that you are searching for some like this:

mwe

---
docummentclass: article
output:
  pdf_document: 
    number_sections: yes
header-includes:
- \usepackage{titlesec, xcolor, lipsum}
# \titleformat{command}[shape]{format}{label}{sep}{before-code}[after-code]
- \titleformat{\section}{\Huge\bfseries}{\thesection\hspace{20pt}\textcolor{gray!75}{|}}{20pt}{}
---

# Introduction
\lipsum[1][1-5]

# Objectives
\lipsum[1][6-9]