Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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