6

I am trying to typeset my captions in margin using marginpar and captionof. Here is what I get by the MWE. Note the wrong caption numbers. Wrong caption numbering

The first caption is included using macro \fixedmarginpar and the second caption using \marginpar. The figure number gets incremented by 2 for every caption included using \fixedmarginpar.

Please read vspace-in-marginpar-adds-unwanted-vertical-space for the \fixedmarginpar macro used in MWE. This macro sets the caption in a box, and offsets the vertical alignment depending on the height of the box. The caption number is incremented by two because this macro sets a box with caption for calculating the height (\captionof is called for the first time) and then sets it in the marginpar (\captionof is called again). Whats a good workaround?

\documentclass{scrreprt}
\usepackage{calc}
%https://tex.stackexchange.com/questions/78519/vspace-in-marginpar-adds-unwanted-vertical-space
\newcommand{\fixedmarginpar}[2][0pt]{%
  \setbox0=\vtop{#2}\marginpar{\vspace{\dimexpr-\ht0+#1}#2}%
}
\begin{document}
\listoffigures

\fixedmarginpar{\captionof{figure}{A}}
\marginpar{\captionof{figure}{A}}
\end{document}
devendra
  • 2,818

1 Answers1

10

The figure number is stepped every time twice, because you're typesetting the caption twice. You can avoid this by typesetting it only once:

\newcommand{\fixedmarginpar}[2][0pt]{%
  \setbox0=\vtop{\parindent=0pt \hsize=\marginparwidth#2}%
  \marginpar{\vspace{\dimexpr-\ht0+#1}\box0 }%
}

In this way the box used for the measuring is not retypeset, but directly used.

egreg
  • 1,121,712