Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

2 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
Thanks, this kind of command was exactly was I was looking for –  Jörg Oct 22 '12 at 9:23
add comment

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}

Nulla malesuada porttitor
diam.\marginpar{\setbox0=\hbox{A}\vspace{\dimexpr-\ht0}A
\setbox0=\hbox{A}$-$\the\ht0\  vspace offset}

\end{document}

Sample output

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.