# Memoir: put heading in margin using Expl3 or xcoffins

In the memoir class manual, there is the following code for putting section headings in the margin:

A less traditional style is to put the whole heading into the margin. I have done this here for a \paragraph heading (which is not otherwise used in this manual). The code is:

\newcommand{\marginbox}[1]{%
\parbox[t][0pt]{6em}{\itshape\raggedleft\leavevmode #1}}
{\llap{\marginbox{#1}\kern0.5em}}}
\setparaindent{0em}
\setparahook{\setsecnumformat{\csname the##1\endcsname\ }}
\paragraph{Hang the whole heading in the margin}%


The macro \marginbox puts its argument, raggedleft, into a zero height \parbox of width 6em, aligned at the top. The \marginhead macro puts its argument into a \marginbox and puts the \marginbox 0.5em to the left. The \paragraph head style is then set to use \marginhead to typeset the heading. The format for the number is reset via \setparahook and \setsecnumformat.

I’ve adapted this to quasi-Expl3, and referencing the dim variables I used in the page layout, thus:

\cs_new:Npn \jcsres_marginbox:n #1
{
\parbox[t][0pt]
{ \c_jcsres_marginwidth_dim }
{ \raggedleft \leavevmode \jcsres_section_style:n {#1} }
}
{
{ \llap { \jcsres_marginbox:n{#1} \kern \c_jcsres_marginspace_dim } }
}


(Irrelevant details elided. Yes, I could have copied the definition of \mode_leave_vertical: from l3trial/xfont/xfss.dtx, and used \tex_kern:D instead of \kern, but that would not have aided clarity.)

I’m trying to customize this a bit more, and I’m trying to use the LaTeX3 tools to that end. So…

What are the Expl3 equivalents of \parbox and \llap? I’ve found a few functions in l3box that almost-but-not-quite fit:

• Corresponding to this use of \parbox I see \hbox_to_wd:nn and \vbox_to_zero:n, but can I combine them to achieve the \parbox effect of 0-height box of given width? and
• I’ve found \hbox_overlap_left:n which seems to correspond to \llap, but can I combine the Expl3 functions the same way as the Memoir example code does the LaTeX2e functions?

Also, this application feels like a job for coffins, either at the l3coffins or xcoffins layer, rather than explicitly playing with boxes; but I don’t see a way to say, “Put this box top-aligned with, and 0.8 em to the left of, the next paragraph.” Is my intuition leading me astray, or can I actually make the code cleaner using coffins?

Later steps (and reasons why I really want this):
• As in my post to comp.text.tex several years ago, “[memoir] \marginpar and marginal section headings”: I’d like to be able to attach other bits of text to heading.
• See also Re-displaying section headings after page-breaks; I’d like to repeat these headings after page breaks, perhaps with a note saying, “(Continued)”, and the current code is just complicated enough to scare me.

• A complete example would make it easier for people to help. Right now, before anybody can even try out an idea, they have to reconstruct a suitable document from the fragments you've provided. Because that is time-consuming and boring, people are more likely to help you if you don't make them go through it before they get to the interesting puzzle-bit. – cfr Aug 19 '15 at 23:30
• I've completed your example. Honestly, why leave people instructions on which bits of code to move where rather than just providing it in a form people can copy-paste-compile? Just to annoy your readers? Especially since the code to be moved is not even formatted as code in the first place.... Grrrr... – cfr Aug 20 '15 at 0:54
• @cfr, after some thought, I’ve removed the MWE in my rewrite of the question. I’m asking about the concepts, not for someone to write working code for me; if I get an answer that addresses the concepts without code examples I’m fluent enough to make that work. – J. C. Salomon Aug 21 '15 at 15:28