% mparhack.sty
% fixes wrong placed marginpars (on cost of introducing labels for them)
% only for single column mode
%
% Stefan Ulrich <ulrich@cis.uni-muenchen.de> with
% Tom Sgouros <tomfool@top.gso.uri.edu> and Hilmar Schlegel
% <hshlgaii@mailszrz.zrz.tu-berlin.de> (for the 209 version).
%
% Eventually there will be a unified version compatible with
% both 209 and 2e (as soon as I get to it ;-)
%
%% This program is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License
%% as published by the Free Software Foundation; either version 2
%% of the License, or (at your option) any later version.
%% 
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.
%% 
%% You should have received a copy of the GNU General Public License
%% along with this program; if not, write to the Free Software
%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
%%

% Just in case I'm using AmS-LaTeX.
\edef\TheAtCode{\the\catcode`\@}
\catcode`\@=11

\def\@secondoftwo#1#2{#2}

\newcounter{mp@cnt}

\newcommand\mp@setref[3]{%
  \ifx#1\relax
    0 % just a dummy
  \else
   \expandafter#2#1%
  \fi
}
\newcommand\mp@pageref[1]{%
    \expandafter\mp@setref\csname r@#1\endcsname
    \@secondoftwo{#1}%
}

%%% from the 209 definition of \@addmarginpar

\def\@addmarginpar{%
%%% --- added:
    \stepcounter{mp@cnt}%
    \label{@mp@lab:\themp@cnt}%
%%% --- end addition
    \@next\@marbox\@currlist{\@cons\@freelist\@marbox
    \@cons\@freelist\@currbox}\@latexbug\@tempcnta\@ne
    \if@twocolumn
        \if@firstcolumn \@tempcnta\m@ne \fi
    \else
      \if@mparswitch
%%% --- substituted:
%         \ifodd\c@page \else\@tempcnta\m@ne \fi
%%% --- by:
         \@ifundefined{r@@mp@lab:\themp@cnt}%
          {\ifodd\c@page \else\@tempcnta\m@ne \fi}%
          {\ifodd\mp@pageref{@mp@lab:\themp@cnt}%
            \else\@tempcnta\m@ne \fi}%
%%% --- end substitution
      \fi
      \if@reversemargin \@tempcnta -\@tempcnta \fi
    \fi
    \ifnum\@tempcnta <\z@  \global\setbox\@marbox\box\@currbox \fi
    \@tempdima\@mparbottom \advance\@tempdima -\@pageht
       \advance\@tempdima\ht\@marbox \ifdim\@tempdima >\z@
       \@@warning{Marginpar on page \thepage\space moved}\else\@tempdima\z@ \fi
    \global\@mparbottom\@pageht \global\advance\@mparbottom\@tempdima
       \global\advance\@mparbottom\dp\@marbox
       \global\advance\@mparbottom\marginparpush
    \advance\@tempdima -\ht\@marbox
    \global\ht\@marbox\z@ \global\dp\@marbox\z@
    \vskip -\@pagedp \vskip\@tempdima\nointerlineskip
    \hbox to\columnwidth
      {\ifnum \@tempcnta >\z@
          \hskip\columnwidth \hskip\marginparsep
        \else \hskip -\marginparsep \hskip -\marginparwidth \fi
       \box\@marbox \hss}\nobreak   %% RmS 91/06/21 \nobreak added
    \vskip -\@tempdima
    \nointerlineskip
    \hbox{\vrule \@height\z@ \@width\z@ \@depth\@pagedp}}

\catcode`\@=\TheAtCode\relax
