4

I would like to use titlesec to have gradient shaded section titles that extend into the left margin. I would like the section number and the gradient to extend into the left margin, but would like the section title text to be aligned with the document text.

\documentclass{article}
\usepackage{tikz}
\usepackage[nobottomtitles*]{titlesec}
\usepackage{blindtext}
\titleformat{\section}[block]%              
    {\Large\bfseries
     \tikz[overlay] \shade[left color=black!20,right color=white] (0,-1ex) rectangle (\textwidth,1em);}%    
    {\thesection}%                   
    {3em}% <---- what do I need here???
    {}
\titlespacing*{\section}{-5em}{*1}{*1}

\begin{document}
\section{Here is my section title}
\blindtext
\end{document}

It seems as though I would need something like 5em-<sep>-<labelwidth> but I don't know how to accomplish that. I would like the result to work regardless of the font or font size used.

Question: How can I, in a font independent way, have the section title text aligned with the text of the document body?

| improve this question | | | | |
3

It is almost easier to construct the section title with the aid of titlesec's explicit option. This allows you to use #1 as the <title> of \section{<title>}:

enter image description here

\documentclass{article}

\usepackage{tikz}
\usepackage[nobottomtitles*,explicit]{titlesec}
\usepackage{lipsum}

\titleformat{\section}[block]
  {\Large\bfseries}
  {}
  {0pt}
  {\hspace{-5em}% Move into margin
   \tikz[overlay] \shade[left color=black!20, right color=white] (0,-1ex) rectangle (\dimexpr\textwidth+5em,1em);% Set background shaded rectangle
   \makebox[5em][l]{\thesection}#1}% Set number + title

\titlespacing*{\section}{0pt}{*1}{*1}

\begin{document}

\section{Here is my section title}
\lipsum[1]

\end{document}
| improve this answer | | | | |
  • Remove explicit and #1: it will do the same. – egreg May 27 '16 at 8:43

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.