4

I would like section numbers to be placed in the margin. I found a solution here (Theorem Name/Numbering in Margin) which works with a caveat: when the section title is very long, the title text line does not break. Explicit breaks (using \\ or \newline) don't work either.

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage[showframe=true]{geometry}

\titleformat{\section}%
            {\Large\sffamily\bfseries}% format
            {\llap{% label
               \thesection\hskip 12pt}#1}%
            {0pt}% horizontal sep
            {}% before

\begin{document}
 \section{A long long long long long long long long long long long long long long long title}
 Normal text.
\end{document}

I don't fully understand what \llap does, but I'm more interested in a solution that keeps the section numbers in the margin but allows the section title to line break. Using KOMA classes is fine, though I understand that may mean not using titlesec.

Hugh
  • 2,374
  • 17
  • 30

2 Answers2

4

You're putting #1 in the wrong place: the title is the last argument. Without the explicit option (that is usually unnecessary) the code should be

\documentclass{article}
\usepackage{titlesec}
\usepackage[showframe=true]{geometry}

\titleformat{\section}%
  {\Large\sffamily\bfseries}% format
  {\makebox[0pt][r]{\thesection\hspace{12pt}}}% label
  {0pt}% horizontal sep
  {}% title

\begin{document}
 \section{A long long long long long long long long long long long long long long long title}
 Normal text.
\end{document}

If you prefer having explicit, the last line should be

  {#1}% title

enter image description here

Don't use \llap if you don't know how it works. Prefer \makebox[0pt][r]{...}: box with zero width, content pushed to its right margin.

egreg
  • 1,121,712
3

Maybe something like this... The \llap has nothing to do with the problem. But what I did change was to take #1 and place it inside of a \parbox.

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage[showframe=true]{geometry}

\titleformat{\section}%
            {\Large\sffamily\bfseries}% format
            {\llap{% label
               \thesection\hskip 12pt}\parbox[t]{\textwidth}{#1}}%
            {0pt}% horizontal sep
            {}% before

\begin{document}
 \section{A long long long long long long long long long long long long long long long title}
 Normal text.
\end{document}

enter image description here