[Tex/LaTex] vspace in marginpar adds unwanted vertical space

marginparspacing

I am trying to finetune the position of marginpars using \vspace. However, the result is not as expected and even if I include vspace{0pt} the marginpar get's pushed down, as the MWE below illustrates. How do I get rid of this unwanted space?

I am not sure if this question is related. Is there an equivalent to \topskip for marginpars?

\documentclass{scrartcl}

\setlength{\textwidth}{60mm}
\setlength{\marginparwidth}{50mm}

\begin{document}

Nulla malesuada porttitor diam.\marginpar{No vspace command}

\vspace{5ex}

Nulla malesuada porttitor diam.\marginpar{\vspace{0pt}A 0pt vspace offset}

\end{document}

enter image description here

Best Answer

A \vspace command at the beginning of \marginpar establishes the skip as the first object in the box, so the vertical alignment will be the top of this space.

You can measure the height of the margin note and back up by that amount.

\documentclass{scrartcl}
\usepackage{color}
\setlength{\textwidth}{60mm}
\setlength{\marginparwidth}{50mm}
\newcommand{\fixedmarginpar}[2]{%
  \setbox0=\vtop{#2}\marginpar{\vspace{-\ht0}\vspace{#1}#2}%
}

\begin{document}

Nulla malesuada porttitor diam.%
\llap{\color{red}\vrule width 6cm height 0pt depth .4pt}%
\rlap{\color{red}\vrule width 5cm height 2pt depth .4pt}%
\fixedmarginpar{-2pt}{No vspace command}

\end{document}

The red rules are just to show the baselines and see that the marginpar is correctly raised by 2pt.

enter image description here

Related Question