
%         M A R N . S T Y
%
%  Alternative marginal notes in LaTeX.
%  This is a quick conversion of my
%  Version 1.0      Copyright (c) 1993  Donald Arseneau
%  for latex2.09 -> 2e.  Caveat Emptor!
%
%  Instructions follow \endinput.
%
%-------------------------------
%

% (mis)used LaTeX registers:
% \@tempdima         Amazingly, only used in normal \@addmarginpar
% \@tempdimb         in \@marnoutput, gives \hsize in effect when note made
% \@ovri             depth of strutbox when note was made
% \@beginparpenalty  number of notes already at left of line
% \@endparpenalty    number of notes already at right of line
% \if@nmbrlist       says there are notes here already.
% \if@tempswa        says a note occurs in shifted text
% \if@rjfield        note is on the right (NOT that it is right-justified!)
% \@mparbottom       proper meaning, but only for notes in right margin
% \@mparbottoml      new length for left margin notes bottom
%
% \marn is used in the text to declare a marginal note.  It makes
% an attempt to place the note properly on the right or left
% in case this text never gets on the outer vertical list.  If it
% does appear on the page list, the output routine will be invoked
% and \@marnoutput will disassemble the note by unwrapping:
% \hbox {\kern \kern \hbox{note} \hss} or
% \hbox {\hss \hbox{note} \kern \kern}
% to get the \hbox{note}.
%
% \marn* prints this particular note in the margin opposite to what
% is normal. * and \reversemarn are cumulative, when both are given
% the note goes back to its normal position!
%
% What side is "normal" is not apparent until the page/columns are decided,
% so the information is written to the .aux file for use next time.
% (Simply relying on the output routine would get only outer-page notes
% on the proper side and would hurt normal page breaking.)
%
% The note is typeset in "\marnfont", which, by default, is \normalsize.
%
\def\marn{\protect\@p@marn}% make robust

\def\@p@marn{\begingroup % \@marn will end with \endgroup
    \@ifstar{\edef\@reversemarn{-\@reversemarn}\@marn}{\@marn}%
}

\def\@marn#1{\leavevmode \@bsphack \strut
 \global\advance\@marn@count\@ne
 \if@filesw \@marn@log\@marn@count \fi % record location for use next time
 \@tempswafalse % use "tempswa" test to say if text lines will be shifted
 \ifnum\parshape>\z@ \@tempswatrue \fi
 \ifdim\hangindent=\z@\else \@tempswatrue \fi
 \vadjust
 {\if@tempswa \penalty-\@marnpenalty \fi % flag shifted text
  \@ovri\dp\strutbox \kern-\@ovri
  \setbox\z@\hbox to\hsize
  {\ifnum
   \@reversemarn % {-} from global switch and/or *
   \@ifundefined{@marn@\the\@marn@count @}% if location logged last time,
   {}{\@nameuse{@marn@\the\@marn@count @}}% placement based on last run
   \@ne>\z@ % +, put note on right
 %      \kern\hsize \kern\marginparsep% John Burt Monkeyed with this
 %      \hbox{\marnstyle{\marnfont \ignorespaces #1\unskip}}\hss
       \hss \hbox{\marnstyle{\marnfont \ignorespaces #1\unskip}}%
       \kern\marginparsep \kern\hsize
   \else    % -, put note on left
       \hss \hbox{\marnstyle{\marnfont \ignorespaces #1\unskip}}%
       \kern\marginparsep \kern\hsize
   \fi % hint for disassembly: note on right ends with glue; on left, kern
  }\ht\z@\z@ \dp\z@\@ovri \box\z@ % smash the box, and put it
  \penalty\@marnpenalty % to execute output routine
 }% end \vadjust
 \@esphack \endgroup
}

\def\marnfont{\normalsize}
\def\marnstyle{}

\newbox\@marnbox
\newdimen\@mparbottoml \global\@mparbottoml\z@ % left-hand mparbottom

% Define the output handler for the new marginal notes.  This will
% try to combine note boxes with the line of text and with other
% notes on that same line.  The vertical list made by \marn is:
%         [line of text]
%         {possible penalties/skips, including \postmarnpenalty}
%         {penalty -marnpenalty if text is being shifted}
%         {kern -depth of strut}
%         [note line]
%         {penalty marnpenalty} <-- break occurs here
%         {other notes, interlinepenalty, widowpenalty, baselineskip,...}
%
% The [note line] box has width=\hsize, height=0pt, and depth=strut depth;
% it contains [kern kern [the note] hss] if the note belongs on the right, or
% [hss [the note] kern kern] if the note goes on the left.
%
% The aim is to combine the note with the line of text, to give the new line
% of text
%
% [ [hss [notes] kern] [line] [kern [notes] hss] kern z kern -y kern x]
%   ------ wd=0 ------        ------ wd=0 ------
%
% where the outer box has the same width as [line] of text, and the 3
% final kerns record the number of notes on each side:
%         x = number of notes on right
%         y = number of notes on left
%         z = y-z (to restore 0 displacement)
%
% If the actual [line] was shifted (list environment) then a phoney [line]
% is constructed and raised by the strut depth.  Finally, \postmarnpenalty
% is appended to counteract the extra penalty 10000 that will follow.
%
% Note that the output routine never gets to see notes in \parbox, \minipage,
% or floats, so multiple notes may overprint in those places.  \marn still
% formats them on the proper side though (after the first run).
%
\def\@marnoutput{%
 \setbox\@cclv\vbox{\unvbox\@cclv}%  measure natural page size
 \@pagedp\dp\@cclv \@pageht\ht\@cclv
           \ifvoid\footins \else
             \advance \@pageht \ht\footins
             \advance \@pageht \skip\footins
             \advance \@pageht \dp\footins
           \fi
           \ifvbox \@kludgeins
             \ifdim \wd\@kludgeins=\z@
               \advance \@pageht \ht\@kludgeins
             \fi
           \fi
           \@reinserts
 \unvbox\@cclv             % regurgitate the page (or column) so far
 \setbox\@tempboxa\lastbox % grab note-line box off list
 \@ovri\dp\@tempboxa       % record strut depth in effect when note was made
 \@tempdimb\wd\@tempboxa   % record \hsize in effect when note was made
 \setbox\@tempboxa\hbox    % simplify note box
 {\unhbox\@tempboxa        % extract contents of note-line
  \ifdim\lastkern>\z@      % note is on left because line ends with a kern
     \unkern \unkern       % remove hsize & marginparsep
     \global\setbox\@marnbox\lastbox % grab note box off list
     \aftergroup\@rjfieldfalse
     \global\let\@marnbottom\@mparbottoml
  \else                    % note is on right, and line ends with glue
     \unskip               % remove \hss
     \global\setbox\@marnbox\lastbox % grab note, unshifted
     \aftergroup\@rjfieldtrue
     \global\let\@marnbottom\@mparbottom
  \fi
 }% end \hbox.  set "\aftergroup" switches. \@tempboxa now contains junk
 \unkern % remove \kern -\dp\strutbox from vertical list
 \@nmbrlistfalse \@tempswafalse    % initialize switches
 \ifnum\lastpenalty=-\@marnpenalty % flags that text is shifted; can't grab it;
 %  ...but can still grab previous note if there are multiple notes here.
    \unpenalty
    \@tempswatrue % use "tempswa" switch to remember that text is shifted
 \fi
 \setbox\@cclv\vbox{}% null holding list
 \@marn@passthru % put junk in box cclv; maybe set \if@nmbrlist = true
 \if@nmbrlist % in a cluster of notes
    \global\@marnbottom\@oldmparbottom % value before this set of notes
 \else % remember values now for later
    \xdef\@oldmparbottom{\noexpand\if@rjfield \the\@mparbottom
        \noexpand\else \the\@mparbottoml \noexpand\fi}%
 \fi
 \if@tempswa % If shifted text, but not multiple notes
    \if@nmbrlist\else % then don't try to grab line of text; fake it instead.
       \penalty\@M % hide box from \lastbox
 \fi\fi
 \setbox\@tempboxa\lastbox
 \ifvoid\@tempboxa % make raised empty box instead of lastbox
    \setbox\@tempboxa\hbox to\@tempdimb % \hsize.  See \@tempdimb above.
      {\vrule depth\@ovri width\z@ \hss}% the line must not end with a kern
    \kern-\@ovri % move up to overlap real line above
 \fi
 \setbox\@cclv\vbox
  {\wd\@tempboxa\@tempdimb
   \setbox\@tempboxa\hbox to\@tempdimb  % rebox line of text
   \bgroup
     \if@nmbrlist % there are multiple notes
        \unhbox\@tempboxa % spill out old line
        \@endparpenalty\lastkern  \unkern % grab kerns that count the notes
        \@beginparpenalty-\lastkern \unkern \unkern
        \ifnum\@beginparpenalty=\z@
           \ifnum\@endparpenalty=\z@ % no previous notes: oops, unboxed too much
   \egroup % end box now, then restart box with empty notes
   \setbox\@tempboxa\hbox to\@tempdimb\bgroup \@marninit
        \fi\fi
     \else    % I already know it is the first note
        \@marninit
     \fi   % \if@nmbrlist
     \if@rjfield   % new marn is on the right
        \@marn@doright
     \else       % new marn on the left
        \@marn@doleft
     \fi
     \count@\@beginparpenalty \advance\count@-\@endparpenalty
     \kern\count@ sp\kern-\@beginparpenalty sp\kern\@endparpenalty sp
   \egroup % end \@tempboxa (the line with notes attached)
   \box\@tempboxa % put the line
   \unvbox\@cclv  % replace the stuff passed-thru
 }% end \setbox\@cclv=\vbox
 \unvbox\@cclv   % put the whole list down without new baselineskips
 \penalty\postmarnpenalty % flag that notes already made; allow breaking.
}% end \@marnoutput


% \Combinemarns combines successive marginal notes from the same line of
% text.  This default version combines notes in a horizontal list separated
% bu commas:  a,b,c.  It should be redefined for different formatting; see
% the suggestions in the instructions below.
%
% \Combinemarns has no explicit parameters, but it can make use of
%   \@tempboxa (\hbox contains the previous notes on this side of this line)
%   \@marn@already (the number of notes already attached) (Don't Change!)
%   \@marnbox (the new note to attach)
%   \if@rjfield (is note on right?) (Changing it will not move it!)
%   \@pageht, \@pagedp (natural height/depth of page material)
%   \@marnbottom (Location of the bottom of the previous margin note)
% When finished, \@tempboxa should contain the combined notes.
% \Combinemarns may also update \@marnbottom (globally) which will
% update \@mparbottom or \@mparbottoml appropriately to avoid collisions
% between notes.  This default version keeps notes at their own line, even
% if a \marginpar will print over them.

\def\Combinemarns{\setbox\@tempboxa\hbox
  {\marnfont
   \unhbox\@tempboxa % spill out old notes (if any)
   \ifnum\@marn@already>\z@ % There are already notes on the line
      \unskip ,\space
   \fi
   \box\@marnbox % add the new note
  }% end new note(s) box
}


% The next few definitions are just parts of \@marnoutput.

\def\@restorefoot{\advance\@pageht\ht\footins \advance\@pageht\skip\footins
    \advance\@pagedp\dp\footins \insert\footins{\unvbox\footins}}
% \@specialoutput could be shortened by putting \@restorefoot in it, but
% I didn't do that.

\def\@marninit{% initialize empty marn boxes on both sided of a text line
 \llap{\hbox{}\kern\marginparsep}%
 \box\@tempboxa
 \rlap{\kern\marginparsep\hbox{}}%
 \@endparpenalty\z@ \@beginparpenalty\z@
}

\def\@marn@doright{% add the note to the right of the line
        \setbox\@tempboxa\lastbox % right note(s) in \rlap
        \let\@marn@already\@endparpenalty
        \rlap    % repackage the \rlap
        {\unhbox\@tempboxa
         \unskip % remove old \hss
         \setbox\@tempboxa\lastbox % grab note box (the kern stays)
         \Combinemarns     % add new note
         \@marn@setbottom
         \ht\@tempboxa\z@ \dp\@tempboxa\z@
         \box\@tempboxa    % replace the notes
        }% the line is done, except for the marker kerns
        \advance\@endparpenalty\@ne
}

\def\@marn@doleft{% add the note to the left of the line
        \setbox\@cclv\lastbox % get right note(s)
        \setbox\z@\lastbox    % text
        \setbox\@tempboxa\lastbox % left note(s) in \llap
        \setbox\@tempboxa\hbox % unwrap \llap
        {\unhbox\@tempboxa
         \unkern % remove marginparsep
         \setbox\@tempboxa\lastbox % grab note box to reveal:
         \unskip % remove \hss
         \unhbox\@tempboxa % replace the notes in tempboxa
        }% \@tempboxa now contains just the previous notes
        \let\@marn@already\@beginparpenalty
        \Combinemarns         % add new note to left
        \@marn@setbottom
        \ht\@tempboxa\z@ \dp\@tempboxa\z@
        \llap{\box\@tempboxa \kern\marginparsep}% put left notes
        \box\z@ \box\@cclv % the text and right notes
        \advance\@beginparpenalty\@ne
}


% Declare \reversemarn to have all \marn notes placed on the opposite side
% from normal.
%
\def\@reversemarn{}
\def\reversemarn{\def\@reversemarn{-}}

\newcount \@marn@count \global\@marn@count=0

% \postmarnpenalty: A penalty put after a note line.  Unfortunately,
% TeX inserts \penalty 10000 after every call to the output routine,
% so we must re-allow breaking without knowing what penalty might
% follow the 10000: none, 0, \clubpenalty....  As a compromise, we
% insert \penalty73; unless \samepage is in effect where we use 10073.
% In shifted environments like {list}, it flags that the there are
% notes already present so the previous line can indeed be grabbed.
% It should be unique, and its value shouldn't hurt formatting too
% much.
%
\def\postmarnpenalty {\ifnum\interlinepenalty=\@M 10\fi 073 }

% \@marn@passthru goes through penalties, kerns, and vskips, removing
% them from the vertical list and saving them in box 255 (\@cclv).
% Penalties of 10000 are not copied.
% If it ever sees a penalty of \postmarnpenalty it sets the switch
% \if@nmbrlist = true, signifying that the line of "text" above can
% be grabbed after all, because it already has notes.  Temporary variables
% \@tempa, \@tempcnta, \skip@ are changed.
%
\def\@marn@passthru{% % go through junk, putting it in box cclv
 \let\@tempa\relax
 \ifnum\lastpenalty=\z@
    \ifdim\lastskip=\z@skip\else % not perfect: ignores \vfill !
      \skip@\lastskip
      \setbox\@cclv\vbox{\vskip\skip@ \unvbox\@cclv}%
      \let\@tempa\@marn@passthru
    \fi \unskip
 \else
    \let\@tempa\@marn@passthru
    \ifnum\lastpenalty=\postmarnpenalty
      \@nmbrlisttrue
    \else
      \@tempcnta\lastpenalty
      \ifnum\@tempcnta<\@M
         \setbox\@cclv\vbox{\penalty\@tempcnta \unvbox\@cclv}%
      \fi
    \fi \unpenalty
 \fi \@tempa
}

\def\@marn@setbottom{%% now update \@marnbottom; executed after \Combinemarns
  \global\@marnbottom\@pageht
  \global\advance\@marnbottom\dp\@tempboxa
  \global\advance\@marnbottom\marginparpush}

% The next thing to do is to make ordinary \marginpar notes smarter,
% by using the same sort of label/ref scheme to position notes in the
% correct margin based on the previous run, and by handling \@mparbottom
% better to avoid vertical collisions in both margins.

\def\@addmarginpar{%
 \@next \@marbox\@currlist
    {\@cons\@freelist\@marbox\@cons\@freelist\@currbox}\@latexbug
 \global\advance\@mpar@count\m@ne
 \@ifundefined{@marn@\the\@mpar@count @}% was location logged last time?
 {\@tempcnta\@ne % NO: use original LaTeX logic
  \if@twocolumn
     \if@firstcolumn \@tempcnta\m@ne \fi
  \else
     \if@mparswitch \ifodd\c@page\else \@tempcnta\m@ne \fi \fi
     \if@reversemargin \@tempcnta -\@tempcnta \fi
  \fi }%
 {\@tempcnta %    YES: use record from last time to decide side.
    \@nameuse{@marn@\the\@mpar@count @}%
    \if@reversemargin -\fi \@ne }%
 \ifnum\@tempcnta <\z@
    \global\setbox\@marbox\box\@currbox \global\let\@marnbottom\@mparbottoml
 \else
    \global\let\@marnbottom\@mparbottom
 \fi
 \@tempdima\@marnbottom \advance\@tempdima -\@pageht
 \advance\@tempdima\ht\@marbox
 \ifdim\@tempdima >\z@
    \@@warning{Marginpar on page \thepage\space moved}%
 \else
    \@tempdima\z@
 \fi
 \global\@marnbottom\@pageht \global\advance\@marnbottom\@tempdima
 \global\advance\@marnbottom\dp\@marbox
 \global\advance\@marnbottom\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
     \if@filesw % record where this is for use next time:
        \@marn@log\@mpar@count
     \fi
     \box\@marbox \hss
 }\nobreak   %% RmS 91/06/21 \nobreak added
 \vskip -\@tempdima
 \nointerlineskip
 \hbox{\vrule \@height\z@ \@width\z@ \@depth\@pagedp}}

% More of LaTeX's output macros need changing for the vertical crash
% avoidance to work.

% Change \@opcol to not reset \@mparbottom--a task done by \@makecol now;

\def\@tempa#1\global\@mparbottom\z@#2\@opcol{\def\@opcol{#1#2}}
\expandafter\@tempa\@opcol\@opcol

% Redefine \reversemarginpar to not lose marginpar bottom positions.

\def\reversemarginpar{\@reversemargintrue}
\def\normalmarginpar{\@reversemarginfalse}

\def\raggedbottom{%
  \def\@L@textbottom{\vskip \z@ \@plus.0001fil}\let\@L@texttop\relax}
\def\flushbottom{\let\@L@textbottom\relax \let\@L@texttop\relax}
\let\@L@texttop\@texttop
\let\@L@textbottom\@textbottom
\def\@texttop{\@marn@texttop\@L@texttop}
\def\@textbottom{\@L@textbottom \@marn@textbot}

% Change \@makecol to reset both left and right marginpar bottom positions,
\toks@{%
   \global\advance\@mparbottom-\@colroom
   \global\advance\@mparbottom\marginparpush
   \ifdim\@mparbottom<\z@ \global\@mparbottom\z@ \fi
   \global\advance\@mparbottoml-\@colroom
   \global\advance\@mparbottoml\marginparpush
   \ifdim\@mparbottoml<\z@ \global\@mparbottoml\z@ \fi
}
\@temptokena\expandafter{\@makecol}
\edef\@makecol{\the\toks@\the\@temptokena}

\def\marn@col{}

% Insert \@marnoutput into existing LaTeX output routine.

\edef\non@marn@output{\the\output}
\output{\ifnum\outputpenalty=\@marnpenalty
  \let\@specialoutput\@marnoutput
  \outputpenalty=-\@Miii 
  \fi \non@marn@output}

\def\@marnpenalty{-10009 }

% Here are the macros to log the actual position of a note, and to read
% that position back from the .aux file.

\def\@marn@log{% special definition for first invocation of \marn
  \write\@auxout{\string\@ifundefined{@marn@loc}%
  {\string\@namedef{@marn@loc}{\string\@gobbletwo}}{}}%
  \ifnum \@auxout=\@mainaux\else
     \immediate\write\@mainaux{\string\@ifundefined{@marn@loc}%
        {\string\@namedef{@marn@loc}{\string\@gobbletwo}}{}}%
  \fi
  \global\let\@marn@log\norm@marn@log % use normal from now on
  \@marn@log}

% writes  \@marn@loc{number}{-}   or  \@marn@loc{number}{}
\def\norm@marn@log#1{% normal definition for logging placement
  \edef\@tempa{\string\@marn@loc{\the#1}}%
  \write\@auxout \expandafter{\@tempa {\ifodd\c@page\else -\fi}}%
}

% write identifier in .aux file to signal beginning of left/right column
%
\def\@marn@texttop{\if@filesw \begingroup
   \edef\@tempa{%
   \write\@auxout{\def\string\@marn@col
   {\if@twocolumn \if@firstcolumn\string-\else\string+\fi \fi
   }}}\@tempa
 \endgroup \fi
}

% write identifier in .aux file to say column is finished
%
\def\@marn@textbot{\if@filesw \write\@auxout{\def\string\@marn@col{}}\fi}

% \@marn@loc is executed in the .aux file to remember the positioning
% of each marginal note from the previous run.
% #1 = identity of marginal note
% #2 = {-} | {}   ( - for even pages)
%
\def\@marn@loc#1#2{\expandafter\xdef\csname @marn@#1@\endcsname
   {\ifx\@marn@col\@empty    % no column info, so use parity of page no.
       \if@mparswitch #2\fi  % switch sides: set true by twosided.sty
    \else % use column info stored by \@marn@texttop
       \@marn@col
    \fi}%
}

\newcount \@mpar@count \global\@mpar@count=0

% Handle older LaTeX versions:
\@ifundefined{@@warning}{\let\@@warning\@warning}{}


\endinput

%
%         M A R N . S T Y
%
%  Version 1.0      Copyright (c) 1993  Donald Arseneau
%

   Marn.sty changes the way LaTeX deals with positioning of
   \marginpar notes on the left or right.  Instead of guessing   This is
   and sometimes being wrong, as LaTeX normally does, and in     a sample
   lieu of forcing page breaks to force notes where we expect    marginpar.
   them, marn.sty uses a method akin to \pageref for deciding
   whether to put a note on the left or right, based on
   information from the previous run.

   A whole new class of marginal notes is introduced to
   co-exist with \marginpar.  The new notes, which use the
   command \marn, will be combined in some way when multiple
   \marn notes appear on the same line of text.  They will also
   never cause the dreaded "too many floats" error. Thus they
   are approprite for frequent short notes rather than for
   paragraphs.  The default is for multiple \marn notes to be
   combined horizontally, separated by commas.  An example is    a,b,c
   shown in the right margin.

   The positioning of \marn and \marginpar notes is governed by
   the following rules:

   \marginpar notes:
     IF two-column                                               twocolumn:
                                                                 base posi-
        IF in left column                                        tion on col-
           note on left                                          umn number.
        ELSE
           note on right
        FI

     ELSE (one-column)                                           onecolumn:
                                                                 base posi-
        IF switching sides (for two-sided printing)              tion on
           IF page number is odd (right-hand page)               page num-
              note on right                                      ber
           ELSE (even page: facing page)
              note on left
           FI
        ELSE (notes stay on one side)
           note on right
        FI

     FI

     IF reversal declared by \reversemarginpar                reverse
        reverse side; right <--> left                         after
     FI                                                       \reverse-
                                                                 marginpar

   These rules are exactly as documented in L. Lamport's
   book, LaTeX: A document preparation system, except that
   the \reversemarn reversal applies to both one- and
   two-column styles whereas it used to apply only to
   one-column style. The only other difference is that
   these rules really are obeyed!  Caution is advised when
   using \reversemarginpar and two-column format: notes
   from both columns must share the "gutter" and they may
   over-print.

   Placement of \marn notes is slightly different, but it is
   so similar, I won't repeat all the rules.  The left/right
   placement is the same except that

    o Reversal is declared by \reversemarn instead of            \reversemarn
      \reversemarginpar (so \marn and \marginpar notes can
      avoid each other by having just one of them reversed).
      There is no \normalmarn command; use grouping instead,
      or \begin{reversemarn} ... \end{reversemarn}.

    o An individual note can be moved to the opposite margin     \marn*
!     by using \marn*{!}.  The `*' reversal is in addition
      to the \reversemarn switch, so {\reversemarn\marn*{note}}  note
      produces a normally-positioned note.

   Unlike \marginpar, \marn has no optional argument.

   Notes from \marn occuring in floats will still appear in
   the print-out in the proper margin.  Notes in double-column
   floats will be placed according to the rules for one-column
   printing.  However, multiple \marn-s on the same line of
   text will not be combined properly, but will overprint;
   likely making them illegible.  A sequence of \marn \marginpar
   \marn on one line of text may produce bad results, with some
   overprinting.  Unless \Combinemarns is changed, \marn notes
   may overprint with \marginpar notes.  The sensible approach
   is to declare \reversemarginpar OR \reversemarn so the two
   classes of notes use opposite margins.  Even with \marginpar
   out of the way, a line of text with \marn \marginpar \marn
   may give un-combined, overprinted \marn notes.

   Sometimes \marn notes will simply vanish.  This happens when
   the \marn command appears buried too deeply in "boxes" or
   math mode.  Typical places for this to happen are in one-line
   captions, in complex formulas, in tabular, array, or eqnarray
   environments.  Minipages, parboxes and floats will still cause
   \marginpar notes to vanish while \marn notes will appear,
   although they can't be combined properly.

              Customizing the Note Style of \marn
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   There are several ways to change the appearence of \marn
   notes. All notes are typeset using a declaration "\marnfont"  \marnfont
   which has the default meaning "\normalsize".  You can change
   it with \renewcommand; e.g.,

    \renewcommand{\marnfont}{%
     \renewcommand{\baselinestretch}{1}%
     \tiny \scriptsize
    }

   Normally, notes are written on one line, so changing
   \baselinestretch would have no effect.  However, there
   are ways to more drastically alter the formatting---
   \marnstyle and \Combinemarns                                 \marnstyle

    \renewcommand{\marnstyle}[1]{%
      \begin{minipage}{\marginparwidth}
      #1\par
      \end{minipage}}

   This example \marnstyle causes \marn notes to behave much
   like \marginpar notes.  In it, changing \baselinestretch
   would be useful.  The \marnstyle command takes one parameter:
   the note text.  The default \marnstyle does nothing, so the
   text is printed in simple lr mode.  Anything but a very
   simple \marnstyle requires an understanding of how the notes  getting
   are placed and combined, which requires study of this file's  tricky
   definitions, examples, and implementation comments.

   Probably the most useful, but alas the most difficult, note   \Combinemarns
   style command is \Combinemarns.  The default way to combine   (capital C)
   multiple notes is on one line separated by commas.  Three     a,b,c
   other definitions for \Combinemarns are given here: one for
   combining notes on *two* lines separated by commas, one for   1,2,3
   stacking the notes vertically, and a third one for making a   4,5
   vertically centered paragraph (of 1, 2 or more lines) with
   the notes separated by commas.  To use one of these formats,
   move the appropriate example up to above \endinput, or include
   it in a separate style file.  These examples can also serve
   as starting-points for local modifications.

%-----------------------------
% Another way to combine notes: horizontally on two lines; good for
% many numbers as notes.  Individual notes will not split over both
% lines.
%
% 1       1       1,2       1,2        1,2
%         2       3         3,4        a long note
%

% This version puts notes in   \hbox{vcenter{two lines of notes}}.
% Since it is a pain to dismantle such a vbox, the old notes are saved
% in their own \hbox.

% Like the default combination style, this version keeps notes at their
% own line, even if a \marginpar will print over them.

\@ifundefined{@old@rnotes}{
\newbox\@old@rnotes  \global\setbox\@old@rnotes\hbox{}
\newbox\@old@lnotes  \global\setbox\@old@lnotes\hbox{}
}{}

\def\Combinemarns{%
\setbox\@tempboxa\hbox
  {$\vcenter
    {\chardef\@tempa\@nameuse{@old@\if@rjfield r\else l\fi notes}%
     \@parboxrestore
     \marnfont
     \global\setbox\@tempa\hbox
     {\ifnum\@marn@already>\z@
         \unhbox\@tempa
         \discretionary{}{}{\hbox{,\space}}%
      \fi
      \box\@marnbox
     }% 1000 > 100*(1/.5)^3  so two lines better than one
%    %% for \exhyphenpenalty=-1000,  \__skip=0 plus .5\hsize
     \hsize\marginparwidth \linepenalty\z@
     \if@rjfield % note is on right, so LEFT justify it
        \leftskip\z@skip \rightskip \z@ plus.5\hsize
        \parshape \thr@@ \z@\hsize \z@\hsize \z@ 9\hsize
     \else
        \rightskip\z@skip \leftskip \z@ plus.5\hsize
        \parshape \thr@@ \z@\hsize \z@\hsize -8\hsize 9\hsize
     \fi \parfillskip\z@skip
     \exhyphenpenalty-\@m \pretolerance\m@ne \tolerance\@m
     \noindent \unhcopy\@tempa \@@par
  }$}% end new note(s) box
}
% For numeric notes there are no descenders, so use .8 spacing.
\def\marnfont{\def\baselinestretch{.8}\scriptsize}
%-----------------------------


%-----------------------------
%                 This version puts notes in
%                 \parbox[c]{ragged notes}.           one note,
%                 Since it is a pain to dismantle     another
%                 such a box, the old notes are       note, and
%                 saved in their own \hbox.           a third.
%    Left hand    Vertical spacing is the same
%    notes are    as for \marginpar: the note
%  raggedleft.    (or bunch of notes) will be
%                 lowered to avoid overprinting.

\@ifundefined{@old@rnotes}{
\newbox\@old@rnotes  \global\setbox\@old@rnotes\hbox{}
\newbox\@old@lnotes  \global\setbox\@old@lnotes\hbox{}
}{}
 
\def\Combinemarns{%
\setbox\@tempboxa\hbox
  {$\vcenter
    {\chardef\@tempa\@nameuse{@old@\if@rjfield r\else l\fi notes}%
     \@parboxrestore
     \marnfont
     \global\setbox\@tempa\hbox
     {\ifnum\@marn@already>\z@
         \unhbox\@tempa,\space
      \fi
      \unhbox\@marnbox
     }%
     \hsize\marginparwidth \parfillskip\z@skip
     \rightskip\z@skip \leftskip \z@ plus.5\hsize minus .5\hsize
     \if@rjfield % note is on right, so LEFT justify it
        \rightskip\leftskip \leftskip\z@skip
     \fi \sloppy
     \noindent \unhcopy\@tempa \@@par
  }$}% end new note(s) box, now adjust vertical position
  \@tempdima\ht\@tempboxa
  \advance\@tempdima -\@pageht \advance\@tempdima\@marnbottom
  \ifdim\@tempdima >\z@
     \@@warning{Marginnote on page \thepage\space moved}%
     \setbox\@tempboxa\hbox{\lower\@tempdima\box\@tempboxa}% lower the \vbox
  \fi
}
\def\marnfont{\def\baselinestretch{1}\scriptsize\normalsize}% for e.g.
%-----------------------------


%-----------------------------
% Yet another method: stack notes
% vertically.  This one checks for     note
% overprinting by comparing            another note
% \@mparbottom with \@pageht, just     and a third
% like \@addmarginpar does.
% The same effect could basically
% be achieved with \marginpar.

\def\Combinemarns{%
  \setbox\@tempboxa\hbox
  {\unhbox\@tempboxa % contains just a \vbox
   \ifnum\@marn@already>\z@
      \setbox\@tempboxa\lastbox
   \fi \unkern
   \setbox\@tempboxa\vbox
   {\marnfont
    \ifnum\@marn@already>\z@
       \prevdepth \dp\@tempboxa
       \unvbox\@tempboxa
    \fi
    \hbox to\marginparwidth
     {\if@rjfield \box\@marnbox \hss \else \hss \box\@marnbox \fi}%
   }%
   \@tempdima\ht\@tempboxa
   \advance\@tempdima -\@pageht \advance\@tempdima\@marnbottom
   \ifdim\@tempdima >\z@
      \@@warning{Marginnote on page \thepage\space moved}%
   \else
      \@tempdima\z@
   \fi  \lower\@tempdima\box\@tempboxa % put the \vbox
  }% end \hbox  \@tempboxa
}% end \Combinemarns

\def\marnfont{\def\baselinestretch{1}\scriptsize}
%-----------------------------




