# vspace in marginpar adds unwanted vertical space

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}


-
I'm not sure why you want to "fix" what seems already correct. At least in the first example. –  egreg Oct 21 '12 at 12:34
@egreg I don't want to fix 0pt offset, but the problem occurs if I want to offset something by just a few pt. –  Jörg Oct 21 '12 at 16:03

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}

\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.

-
Thanks, this kind of command was exactly was I was looking for –  Jörg Oct 22 '12 at 9:23

You will see the same effect in standard LaTeX with the article class.

The contents of \marginpar are put in a \vtop box, which aligns the baseline of the first box it contains with the current baseline. Inserting a \vspace includes new vertical material and so changes which baseline is used. The \vspace{0pt} results in a box of zero height above the text and so its baseline, which is the the top of the next line of text, gets aligned with the current baseline.

The package lua-visual-debug run under lualatex will demonstrate this clearly for you.

Thus you need to specify a negative \vspace to get zero effect. In your case \vspace{-6.83331pt}, which can be found via \setbox0=\hbox{A}\the\ht0 as below:

\documentclass{article}

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

\begin{document}
\thispagestyle{empty}

\setbox0=\hbox{A}$-$\the\ht0\  vspace offset}