# How to remove the space in an item, due to starting with a \marginpar?

The following picture (MWE follows below) demonstrates the problem. There is a space at the beginning of the list when it begins with a \marginpar. I appreciate that something must be on the line directly after the \item so I tried \mbox and a zero width \makebox, but this didn't help.

My original intention was to ask with \reversemarginpar but I noticed the same affect happens with simply \marginpar.

%%%%%%%%%%%% MWE %%%%%%%%%%%%%%%%%%%%

\documentclass{article}

\usepackage{marginnote}

\begin{document}
\begin{enumerate}

\item Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item \mbox{}\marginpar{(A space)} Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item \makebox[0pt][l]{}\marginpar{(A space)} Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item Some text \marginpar{(OK, but not nice code)} to see what happens when we get to a second line and if there is a space at the fron or not.

\end{enumerate}
\end{document}

• This is not specific to \marginpar either - it's just they way that TeX works. In 2 and 3, you start the text after \item with an \mbox which is a "thing" - even though it's an invisible thing with zero width. If you follow that by white space, big surprise, - LaTeX actually typesets that white space! On the other hand in 4, \marginpar doesn't typeset anything in the main output (it just puts a box in the margin) so the string of whitespace characters (one before it, one after) is merged into a single space when it is typeset. – alephzero Aug 22 '17 at 18:35

i would redefine marginpar and then not care for spaces after its use:

\documentclass{article}

%\usepackage{marginnote} % not needed
\let\oldmarginpar\marginpar
\renewcommand{\marginpar}[1]{%
\leavevmode%
\oldmarginpar{#1}%
\ignorespacesafterend\ignorespaces%
}

\begin{document}
\begin{enumerate}

\item Some text to see what happens when we get to a second line and if there is a space at the front or not.

\item \marginpar{(A space)}Some text to see what happens when we get to a second line and if there is a space at the front or not.

\item \marginpar{(A space)} Some text to see what happens when we get to a second line and if there is a space at the front or not.

\item \marginpar{(OK, but not nice code)}    to see what happens when we get to a second line and if there is a space at the front or not.

\end{enumerate}
\end{document}


You can get lost of that space, by leaving out that space after the argument of \marginnote (\marginnote{margin stuff}Other text). So your MWE should look like:

\documentclass{article}

\usepackage{marginnote}

\begin{document}
\begin{enumerate}

\item Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item \mbox{}\marginpar{(A space)}Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item \makebox[0pt][l]{}\marginpar{(A space)}Some text to see what happens when we get to a second line and if there is a space at the fron or not.

\item Some text \marginpar{(OK, but not nice code)}to see what happens when we get to a second line and if there is a space at the fron or not.

\end{enumerate}
\end{document}


• IMO this is a better answer. Redefining \makepar is trying to make TeX work a different way from how Knuth designed it. Even if you succeed for one command, there will be another, and another, and another - and then your redefinitions might start interacting with each other ... ! Much better just to learn how to use it the way it was intended to be used, and don't put a space in the input where you don't want one in the output. – alephzero Aug 22 '17 at 18:40
• @alephzero totally agreed. Learning how to use correct syntax is the preferable way here, because the same issue could arise from almost every macro taking one or more arguments... – Skillmon likes topanswers.xyz Aug 22 '17 at 19:25