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.

     \tikz[overlay] \shade[left color=black!20,right color=white] (0,-1ex) rectangle (\textwidth,1em);}%    
    {3em}% <---- what do I need here???

\section{Here is my section title}

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?


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



  {\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



\section{Here is my section title}

| 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.