# Create a section rule that is the same width as the section title

I would like to create something that looks like this:

Now, I have seen a solution that works with the asker's example environment (using \predisplaysize). However, my desired solution is a little different. In particular, I am trying to use \titleformat:

\documentclass[12pt]{article}

\usepackage{xcolor, lipsum}
\usepackage[calcwidth]{titlesec}

\definecolor{carmine}{rgb}{0.68, 0.0, 0.09}

\titleformat{\section}[hang]{\Large\sffamily\bfseries}
{\color{carmine}\S$\,$\thesection}{0.5em}{\color{black}}
%This creates a rule that matches the full page's width, rather than the title's
[\vspace{-1.5em}\rule{\textwidth}{0.2pt}]

\begin{document}
\section{Introduction}
\lipsum[1-1]
\end{document}


This looks like:

So far, my attempts to use the titlesec have been unsuccessful. When using \titlewidth as an argument to \rule, I find that the underline completely disappears. As well, the example given in the documentation draws a rule (of full page width) above the section name.

Is there a way to create a rule under a section title that has the same width as the title?

This is not very good style, but here we go…

The documentation mentions there can be wrong measurements of titles with the hang option (which b.t.w. is the default). You can use the block style, which is the same as hang as long as you don't have multiline titles.

Also, I suggest using the [T1] font encoding, as this will load cmsuper which has a bold version of §, and I don't think yopu want to raise the rule by 1.5em: this would make it strike through the title. 1.5ex is more sensible.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{xcolor, lipsum}
\usepackage[calcwidth]{titlesec}

\definecolor{carmine}{rgb}{0.68, 0.0, 0.09}

\titleformat{\section}[block] {\Large\sffamily\bfseries}
{\color{carmine}\S\,\thesection}{0.5em}{\color{black}}
%This creates a rule that matches the full page's width, rather than the title's
[\vspace{-1.5ex}\rule{\titlewidth}{0.2pt}]

\begin{document}

\section{Introduction.}

\lipsum[1-1]

\end{document}


Note: If you do have multiline section titles, there exists a workaround to make titles hang with the block style.

• This is just what I was looking for. Thank you for your answer and your feedback, @Bernard! – awb Jan 30 '18 at 23:42