# Automatically move marginal notes that exceed the page border to next page

I'm writing my thesis with classicthesis. It defines the \graffito command for marginal notes as follows:

% Graffiti as in GKP's book "Concrete Mathematics"
% thanks to Lorenzo Pantieri and Enrico Gregorio
\def\graffito@setup{%
%   \slshape\footnotesize% this was due to \slhape in above book
\itshape\footnotesize\leavevmode\color{Black}%
\parindent=0pt \lineskip=0pt \lineskiplimit=0pt %
\tolerance=2000 \hyphenpenalty=300 \exhyphenpenalty=300%
\doublehyphendemerits=100000%
\finalhyphendemerits=\doublehyphendemerits}
%\DeclareRobustCommand{\graffito}[1]{\marginpar%
% [\graffito@setup\raggedleft\hspace{0pt}{#1}]%
% {\graffito@setup\raggedright\hspace{0pt}{#1}}}

\RequirePackage{mparhack} % get marginpar right

\let\oldmarginpar\marginpar
\renewcommand{\marginpar}[2][]{% always add optional parameter, make it empty by default
\if\relax\detokenize{#1}\relax% optional parameter empty, in all normal document calls
\oldmarginpar[\graffito@setup\raggedleft\hspace{0pt}{#2}]{\graffito@setup\raggedright\hspace{0pt}#2}%
\else%two parameters, let them use their styling
\oldmarginpar[{#1}]{#2}%
\fi%
}


and I added the \graffitox on top to include an optional vertical offset (original thread: Including default parameter into \def, code adopted from user egreg):

\makeatletter
\usepackage{suffix}
\newcommand{\graffitox}[1]{%
\marginpar
[\graffitox@setup{\graffitooffset}\raggedleft\hspace{0pt}#1]
{\graffitox@setup{\graffitooffset}\raggedright\hspace{0pt}#1}%
}
\WithSuffix\newcommand\graffitox*[1]{%
\marginpar
[\graffitox@setup{\graffitoaftercaptionoffset}\raggedleft\hspace{0pt}#1]
{\graffitox@setup{\graffitoaftercaptionoffset}\raggedright\hspace{0pt}#1}%
}
\newcommand\graffitox@setup[1]{%
\vspace{#1}%
\parindent=0pt \lineskip=0pt \lineskiplimit=0pt
\tolerance=2000 \hyphenpenalty=300 \exhyphenpenalty=300
\doublehyphendemerits=100000
\finalhyphendemerits=\doublehyphendemerits
\itshape\footnotesize
\leavevmode\color{black} %
}
\makeatother


I insert marginal notes in the beginning of paragraphs, but for notes that are longer than a single line, I sometimes arrive at the following situation where the note exceeds the main text:

This isn't too bad if the note is short, but very unappealing for longer notes. I consequently check each note manually and insert a "\clearpage" before the "\graffitox" command to move the entire paragraph to the next page. However, this requires quite some manual effort whenever something in the overall document changes.

Can this process be automatised? I.e.: if the marginal note is longer than the paragraph segment on that page, replace "\graffitox{...}" with "\clearpage\graffitox{...}"? I appreciate any help in facilitating this - my present problem is to obtain the measurements for the calculation (remaining vertical space until end of main text, vertical space required by marginal note, etc.).

It would be great to find a solution using that can be wrapped into the \graffitox command to spare me from substantial changes to the whole thesis.

• This is not how classicthesis (re)defines \marginpar. Mind telling me where this code came from? Aug 4, 2020 at 2:42
• Well spotted, @PhilipPirrip! I totally forgot that I added a definition on top of the original (that was when I started writing the thesis, a long time ago...). I modified my original post to include both definitions. Aug 4, 2020 at 14:02
• You could find some inspiration here tex.stackexchange.com/questions/24071. Maybe but just allowing page breaks in paragraphs after a certain number of lines. Or ping the author of the first answer there, I'm sure he can help. Aug 10, 2020 at 12:31

Essentially the graffitox macro uses the standard LaTeX \marginpar command to set the note. If the note is positioned too long for the page it just keeps on typesetting, as in your example.
I understand that you manually check the output and if the note is too long shift the paragraph text and note to the next page. Have you thought of adding some negative vertical space before the note, such as \vspace{-2\baselineskip} and not shift the paragraph text?
If you have many long marginal notes the memoir class includes a \sidebar command which inserts marginal notes, starting at the top of the page but continuing on to the next page if they are too long for the current page.