1

I would like to create something that looks like this:desired look

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:

current attempt

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?

3

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} 

enter image description here

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.