# How do I get section headings to extend into the margin (using the Memoir class)?

I’m writing a document that uses a rather small text block (\textwidth), with the margin holding footnotes, figures and various other material. Because of the narrow text block, section headings frequently span two lines. However, they can often physically fit on one line – if they are allowed to extend into the marginal area.

So, how do I get the headings to extend into the margins (while still being broken if they extend too much, i.e., more than \marginparsep + \marginparwidth)? I’m using the Memoir class.

Here’s a simple example:

\documentclass[11pt,oneside,article]{memoir}

\setlrmarginsandblock{3cm}{9cm}{*}
\setmarginnotes{1.5em}{5.5cm}{\onelineskip}
\checkandfixthelayout[nearest]

\usepackage{lipsum}

\begin{document}

\section{This heading is too long to fit on one line}

Foo. \marginpar{\lipsum[4]}\lipsum[1-3]

\end{document}

-
Are you working one-sided or two-sided? If two-sided, what do you want to do on even pages? – Brent.Longborough Oct 26 '11 at 18:48
@Brent.Longborough: The document is one-sided, as per my example code. (Well, in my real document I use twoside + some magic to make it mostly act like a one-sided document, but that’s not important here.) – Karl Ove Hufthammer Oct 26 '11 at 18:59
Fastest solution might be to use xparse to redefine \section and friends (xparse makes it easy to still have the extra arguments), and then use this to combine it with \makebox construction Gonzalo use in the titlesec solution. (untested) – daleif Oct 26 '11 at 19:14

This is the memoir way:

\newcommand{\extendedsec}[1]{\noindent
\makebox[0pt][l]{\parbox[t]{\textwidth}{\Large\bfseries\raggedright#1}}}


Change \textwidth into what suits you best. I wouldn't fill the whole line.

If you want to fill the whole line, then

\newcommand{\extendedsec}[1]{\noindent
\makebox[0pt][l]{\parbox[t]%
{\dimexpr\textwidth+\marginparsep+\marginparwidth\relax}%
{\Large\bfseries\raggedright#1}}}


A similar trick is described on page 106 of the manual for memoir

-
Thanks. I don’t understand why it works (why can setsecheadstyle take a command that takes an argument?), but it does extend the heading into the margin (if one replaces \textwidth with something larger). However, it also seems to add some space to the left of the heading. – Karl Ove Hufthammer Oct 26 '11 at 20:27
@KarlOveHufthammer Yes, I forgot a \noindent – egreg Oct 26 '11 at 20:35
+1 just for the first line ;-) – Brent.Longborough Oct 26 '11 at 21:00
@egreg OK. Now it’s perfect. :-) – Karl Ove Hufthammer Oct 27 '11 at 14:10

You can use the titlesec package; in the following example I used a \parbox of width \textwidth+\marginparwidth+\marginparsep to format the heading for sections:

\documentclass[11pt,oneside,article]{memoir}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\setlrmarginsandblock{3cm}{9cm}{*}
\setmarginnotes{1.5em}{5.5cm}{\onelineskip}
\checkandfixthelayout[nearest]

\titleformat{\section}
{\normalfont\Large\bfseries}{}{0pt}
{\makebox[\dimexpr\linewidth][l]{%
\parbox{\dimexpr\textwidth+\marginparwidth+\marginparsep\relax}
{\raggedright\thesection\hspace{1em}#1}}}

\begin{document}

\section{This heading fits now on one single line}

Foo. \marginpar{\lipsum[4]}\lipsum[1]

\section{This heading is too long to fit on one line and will span two lines}

Foo. \marginpar{\lipsum[4]}\lipsum[1]

\end{document}


-
@Karl: Note the use of the explicit package option to titlesec. It allows you to "grab hold of" the sectional title by means of the argument #1 do with it what you want, as in @Gonzalo's answer. – Werner Oct 26 '11 at 17:54
Thanks. Although this does seem to work, the titlesec documentation explicitly says that it’s not compatible with the ‘Memoir’ class, so I’m worried there may be some hidden issues with this solution. – Karl Ove Hufthammer Oct 26 '11 at 18:38
do consider making the multi-line headings ragged right to avoid uneven spacing. – barbara beeton Oct 26 '11 at 22:22
@barbara beeton: done. – Gonzalo Medina Oct 26 '11 at 22:25