# How to fill section on titlesec

How can i make a titleformat that fills in the whole line? Something like that:

«counter» «name» « ------ rule fill --------»

Normal text....

I managed to do it using the runin type but it has several problems:

\newcommand{\crulefill}{\leavevmode\leaders\hrule height 0.7ex depth\dimexpr 0.5pt-0.7ex\hfill\kern 0pt}

\titleformat{\section}[runin] {\bfseries\Large} {\fboxrule=0.5pt\fcolorbox{black}{white!0}{{\color{black}\thesection.}}\rule[.7ex]{10pt}{.5pt}\ } {0pt} {\upshape\bfseries\Large} [\ \crulefill\vspace*{1.5ex}~\\hspace*{6pt}]

1. \vspace defines the separation between the normal text and the section.
2. \hspace defines the separation in the first line of the normal text (the indentation)
3. When there is no text between a section and a subsection, the space is too large, but when there is text, the space is fine.

## 1 Answer

A solution with the default hang style,and the explicit option:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}

\usepackage{lipsum}

\usepackage[explicit]{titlesec}

\newcommand{\crulefill}{\leavevmode\leaders\hrule height 0.7ex depth\dimexpr 0.5pt-0.7ex\hfill\kern 0pt}

\titleformat{\section}{\bfseries\Large} {\fboxrule=0.5pt\fcolorbox{black}{white!0}{{\color{black}\thesection.}}\rule[.7ex]{10pt}{.5pt} } {0pt} {\upshape\bfseries\Large#1 \ \crulefill}%

\titleformat{\subsection}{\bfseries\large} {\thesubsection.\enspace\color{LightSteelBlue}\vrule}{0.6em} {#1}

\begin{document}
\setcounter{chapter}{1}
\section{Sección}

\subsection{Subsection}
\lipsum[11]
\section{Sección}
\lipsum[12]
\subsection{Subsection}
\lipsum[13]

\end{document}


• \color{LightSteelBlue}\vrule\color{black} should be {\color{LightSteelBlue}\vrule} Jul 17, 2018 at 21:33
• @egreg: That's right. I thought I had to revert to the default colour, but you're right. I've changed the code. Thanks for the tip! Jul 17, 2018 at 21:37