# titlesec section formating with rules

This is a rather specific question I have had for years now but I always just implemented a crummy manual solution.

I want my section headings to look like this. That being the section is in small caps (which has a rule on both sides)

                      -------------A CENTERED SECTION------------


The following causes this, with the after-rule not in the same line.

                      -------------A CENTERED SECTION
------------

\titleformat{\section}[hang]{\color{theblue}\centering\bfseries\scshape\Huge}{}{0in}{\raisebox{0.5ex}{\rule{2in}{0.5pt}}}[\raisebox{0.5ex}{\rule{2in}{0.5pt}}]


And this change produces the proper structure, but it is not centered.

 -------------A NOT CENTERED SECTION------------

\titleformat{\section}[runin]{\color{theblue}\centering\bfseries\scshape\Huge}{}{0in}{\raisebox{0.5ex}{\rule{2in}{0.5pt}}}[\raisebox{0.5ex}{\rule{2in}{0.5pt}}\newline]


Another curriosity is that the other shape options seem to break all the formatting content, even the color.

For reference, from titlesec.pdf

\titleformat{ 〈 command 〉 }[ 〈 shape 〉 ]{ 〈 format 〉 }{ 〈 label 〉 }{ 〈 sep 〉 }{ 〈 before-code 〉 }[ 〈 after-code 〉 ]

With two rules 2 inches long, your title must be really short to make everything fit on a line (or your paper sheet very wide)!

Anyway, here is a possibility with rules 1.5 in long, only \huge size, and the block style:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[showframe]{geometry}

\usepackage[svgnames]{xcolor}
\usepackage[explicit]{titlesec}
\titleformat{\section}[block]{\filcenter\color{SteelBlue}\centering\bfseries\scshape\huge}{\rule[0.5ex]{1.5in}{0.5pt}}{0.5em}{#1\enspace \rule[0.5ex]{1.5in}{0.5pt}}
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\section{A Test Section}

\lipsum[3]

\end{document}


• I am confused, why did you use {} instead of [] for the after-code? – Bob Aug 4 '18 at 17:55
• I think I see what you did. Instead you used the label section in place of before-code and the before-code section in place of after-code. – Bob Aug 4 '18 at 18:54
• @Bob: I didn't really use any after-code: I only used the 4th argument of \titleformat, anf the explicit optionsq lets me write the code for a small rule after the title. – Bernard Aug 4 '18 at 19:00