[Tex/LaTex] Titlesec & titles extending into margin

horizontal alignmentsectioningtitlesec

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?

Best Answer

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}
Related Question