10

Problem

I'm trying to create fancy section heading using titlesec. I need to put the section name into a \parbox to have line breaks. I want the parbox to extend to the far right margin, but I don't know how to achieve this.

MWE

\documentclass[draft]{article}
\pagestyle{empty}
\usepackage[explicit]{titlesec}
\usepackage{xcolor,lipsum}
%% Section
\titleformat{\section}[block]%
  {\normalfont\sffamily}%
  {\colorbox{black}{\parbox[c][17pt][c]{30pt}{%
      \hfil\color{white}\Large\thesection\hfil}}}%
  {0.15cm}%
  {%
   \raisebox{-3pt}{\parbox{10.5cm}{\hspace*{0.7pt}\color{black}\Large\sffamily#1\\[-3.5pt]%
   \color{black}\footnotesize\textbf\dotfill}}%
  }
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{1.3ex plus .2ex}

\begin{document}

\section{Ridiculously overly extermely long section title, that definitely spans several lines.}

\lipsum[2]

\end{document}

Output

enter image description here

  • 1
    You can try \textwidth or \linewdith. – Sigur Nov 27 '13 at 21:39
  • ... or \hsize (link) – masu Nov 27 '13 at 21:39
7

You need to use \linewidth-30pt-1.5mm-2\fboxsep-2\fboxrule as your \parbox width. This removes the width of the numbered box (30pt), the \colorbox boundaries which is related to a \framebox (2\fboxrule and 2\fboxsep) as well as the 0.15cm gap between the box and the title from the \parbox width measurement:

enter image description here

\documentclass[draft]{article}
\pagestyle{empty}
\usepackage[explicit]{titlesec}
\usepackage{xcolor,lipsum}
%% Section
\titleformat{\section}[block]%
  {\normalfont\sffamily}%
  {\colorbox{black}{\parbox[c][17pt][c]{30pt}{%
      \hfil\color{white}\Large\thesection\hfil}}}%
  {0.15cm}%
  {%
   \raisebox{-3pt}{\parbox{\dimexpr\linewidth-30pt-1.5mm-2\fboxrule-2\fboxsep}{\raggedright\color{black}\Large\sffamily#1\\[-3.5pt]%
   \color{black}\footnotesize\textbf\dotfill}}%
  }
\titlespacing*{\section}
  {0pt}{3.5ex plus 1ex minus .2ex}{1.3ex plus .2ex}

\begin{document}

\section{Ridiculously overly extermely long section title, that definitely spans several lines.}

\lipsum[2]

\end{document}
  • Thank you very much for your answer, but now I get an Overfull \hbox (6.0pt too wide)... and the title sticks out to the right. – Henri Menke Nov 27 '13 at 21:50
  • 1
    You'll notice that I added \raggedright to the title so that it is not justified (removing the bizarre \hspace*{0.7pt}). I don't receive any overfull \hboxes (using ShareLaTeX. – Werner Nov 27 '13 at 21:52
  • the heading would look a bit better if the lines weren't so far apart, and the box for the "reversed" number were somewhat taller, maybe spanning from the cap height of the top line to the baseline of the second. (although that might make it look overly tall for a one-line header. experiments with multiple lines in the header would be helpful.) – barbara beeton Nov 27 '13 at 21:53
  • I got it: \colorbox is also an \fbox, so the correct width of the \parbox is \dimexpr\linewidth-30pt-1.5mm-2\fboxrule-2\fboxsep. – Henri Menke Nov 27 '13 at 21:59
  • @HenriMenke: Yes... sorry, I'll update my answer. – Werner Nov 27 '13 at 22:00

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.