# Use \marginnote without affecting spacing?

In a large document, I have been using annotation commands based on \marginnote{}. Now I have noticed, that marginnote has unintended effects on spacing, e.g. by causing the first paragraph of a section to be indented, when it shouldn't be.

In principle I could redefine my command to always add \leavevmode, but then I'd have to track down all instances of the command. Plus, my macro behaves differently depending on \ifvmode (see MWE). I can't use \marginpar instead, as the commands have been used in floats, and for some reason result in "lost floats" if I select marginpar vs marginnote based on \ifinner.

Is there some way to wrap, redefine or replace marginnote, such that it doesn't interfer with paragraph indentation or spacing, that doesn't require changing how the command is invoked throughout the document?

There are some related questions, but they all were ultimately answered in a manner that required changing the invocations throughout the document.

## MWE

\makeatletter
\documentclass[draft]{article}

\usepackage{marginnote}
\usepackage{ifdraft}
\usepackage{lipsum}

\newif\if@use@leavevmode@hack

\newcounter{commentnote}
\newcommand{\commentnote}[2][0pt]{%
\newif\if@cnmark
\unless\ifvmode
\unskip
\@cnmarktrue
\stepcounter{commentnote}%
\else
\if@use@leavevmode@hack
\leavevmode
\fi
\fi
\ifdraft{%
\if@cnmark\textsuperscript{\thecommentnote}\fi
\marginnote{\if@cnmark\textsuperscript{\thecommentnote}\,\fi#2}[#1]%
}{}%
}

\begin{document}

\section{Without \textbackslash leavevmode}

\sloppy

\commentnote{Note (1)}
This text should not be indented.

The \verb|\unless\ifvmode\unskip|
was introduced to allow
\commentnote{Note(2)}
typesetting notes on separate lines without adding
\% characters at the end of each preceding line,
and in order to use \verb|\commentnote|
in vertical mode to create unmarked comments.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}

It is acceptable to use multiple notes before a paragraph.
Inserting \verb|\leavevmode| would cause spurious numbering.

\section{With \textbackslash leavevmode}
\@use@leavevmode@hacktrue

\commentnote{Foo}%
Using \verb|\leavevmode| leads to correct indentation,
but then requires manually removing newlines between
\verb|\commentnote| and paragraphs, and adding \%
characters at the end of each invocation.

\commentnote{Note(3)}%
\commentnote[\baselineskip]{Note (4)}%
\commentnote[2\baselineskip]{Note (5)}%
Furthermore, it leads to spurious numbering of out-of-paragraph notes.

\end{document}

• just end the command definition with \ignorespaces and you will not need the % – David Carlisle Mar 20 '19 at 16:55
• \newif\if@cnmark should not be inside the definition, as you just want to declare \newif once, in the preamble. – David Carlisle Mar 20 '19 at 16:58

\makeatletter
\documentclass[draft]{article}

\usepackage{marginnote}
\usepackage{ifdraft}
\usepackage{lipsum}

\newtoks\zzz
\newif\if@cnmark

\newcounter{commentnote}
\newcommand{\commentnote}[2][0pt]{%
\ifvmode
\@cnmarkfalse
\else
\unskip
\@cnmarktrue
\refstepcounter{commentnote}%
\fi
\ifdraft{%
\if@cnmark\textsuperscript{\thecommentnote}\fi
{\let\everypar\zzz\marginnote{\if@cnmark\textsuperscript{\thecommentnote}\,\fi#2}[#1]%
}{}}%
\ignorespaces}

\begin{document}

\section{Without \textbackslash leavevmode}

\sloppy

\commentnote{Note (1)}
This text should not be indented.

The \verb|\unless\ifvmode\unskip|
was introduced to allow
\commentnote{Note(2)}
typesetting notes on separate lines without adding
\% characters at the end of each preceding line,
and in order to use \verb|\commentnote|
in vertical mode to create unmarked comments.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}

It is acceptable to use multiple notes before a paragraph.
Inserting \verb|\leavevmode| would cause spurious numbering.

\section{With \textbackslash leavevmode}

\commentnote{Foo}
Using \verb|\leavevmode| leads to correct indentation,
but then requires manually removing newlines between
\verb|\commentnote| and paragraphs, and adding \%
characters at the end of each invocation.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}
Furthermore, it leads to spurious numbering of out-of-paragraph notes.

\end{document}

• Thanks! This does improve the "use@leavevmode@hack" version. However it doesn't change anything in the upper part. The first paragraph of each section is still, incorrectly, indented :/ I also don't understand, why \ignorespaces works as it does here; I would expect it to (unwantedly) remove the space after the "1" mark for "Note(2)", but it doesn't. – kdb Mar 20 '19 at 17:26
• @kdb the first para under each of the section headings is not indented, see the image I posted. – David Carlisle Mar 20 '19 at 18:26
• Might be a local installation issue then; When compiled on my work laptop, they were indented, on my home laptop they are not. – kdb Mar 21 '19 at 10:24
• @kdb add \listfiles and then compare the file lists in the log, probably an old copy of marginnote? – David Carlisle Mar 21 '19 at 10:27
• I came across an trickier case: Is there any possibility to avoid the invisible paragraph adding extra spacing for \section{TITLE} \marginnote{NOTE} \subsection{TITLE}? I can only come up with "put the \commentnote into the section title", but then I'd have to use the \section[TITLE]{TITLE \commentnote{NOTE}} pattern everywhere. – kdb Mar 22 '19 at 0:37