4

Joseph Wright's CV source contains a snippet of code that produces an offset color block behind section titles using the titlesec package, like so:

\documentclass[11pt]{article}
\usepackage{lipsum,xcolor}
\usepackage{titlesec}
\titleformat{\section}{\Large}{}{0 em}
  {%
    \begingroup
      \color{gray!30}%
      \titleline{\leaders\hrule height 0.6 em\hfill\kern 0 pt\relax}%
    \endgroup
    \nobreak
    \vspace{-1.2 em}%
    \nobreak
  }
\begin{document}
\section{Lorem Ipsum}
\lipsum[6]
\end{document}

image for mwe

However, when section numbering is added via \thesection in the \titleformat command, this nice effect seems to break:

image for broken mwe

How could this code be modified such that section numbering doesn't cause any alignment issues? It'd be nice if this could be achieved while still using the titlesec package.

3

Here's one possibility, using the explicit option for titlesec; the example shows the definitions needed for numbered and unnumbered sections:

\documentclass[11pt]{article}
\usepackage{lipsum,xcolor}
\usepackage[explicit]{titlesec}

\titleformat{\section}{\Large}{}{0em}
  {%
    \begingroup
      \color{gray!30}%
      \titleline{\leaders\hrule height 0.6 em\hfill\kern 0 pt\relax}%
    \endgroup\vskip-1.2em\thesection\hskip0.5em#1
    \nobreak
  }
\titleformat{name=\section,numberless}{\Large}{}{0em}
  {%
    \begingroup
      \color{gray!30}%
      \titleline{\leaders\hrule height 0.6 em\hfill\kern 0 pt\relax}%
    \endgroup\vskip-1.2em#1
    \nobreak
  }

\begin{document}
\section{Lorem Ipsum}
\lipsum[6]
\section*{Lorem Ipsum}
\lipsum[6]
\end{document}

enter image description here

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.