% \CheckSum{260}
% \iffalse meta-comment
%
% marginnote.dtx
%
%% Copyright (c) Markus Kohm  2005-2006
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%%
%% The author of the initial version of these work was 
%% Markus Kohm <komascript@gmx.info>
%% 
%% This work consists of the files marginnote.dtx, marginnote.ins,
%% and REAMDE.
%%
%
%<*driver>
\ProvidesFile{marginnote.dtx}[%
%</driver>
%<package>\ProvidesPackage{marginnote}[%
%<*driver|package>
  2006/02/06 v1.0a non floating margin notes for LaTeX]
%</driver|package>
%<*driver>
\documentclass{ltxdoc}
\begin{document}
\RecordChanges
\DocInput{marginnote.dtx}
\end{document}
%</driver>
%\fi
%
% \GetFileInfo{marginnote.dtx}
%
% \title{Non-Floating Margin Notes with \textsf{marginnote}
%   Package\thanks{This file has version number \fileversion, last revised
%     \filedate.}}
% \author{Markus Kohm\thanks{Email: \texttt{komascript@gmx.info}}}
% \date{\filedate}
% \maketitle
%
% \begin{abstract}
%   In \LaTeX{} the command \cs{marginpar}\oarg{left}\marg{right} might be
%   used to create a note in the margin. But there is a problem with this
%   command: it creates a special kind of float. For this it cannot be used
%   e.g. at floats or footnotes. Package \textsl{marginnote} supports another
%   command \cs{marginnote} to create notes in the margin. This does not use a
%   kind of float and for this does not have the disadvantage of
%   \cs{marginpar}. But there might be other problems \dots
% \end{abstract}
%
%
% \section{How to Use \textsf{marginnote} Package}
%
% First of all you have to load. You may use:
% \begin{verbatim}
% \usepackage{marginnote}
% \end{verbatim}\vskip-\baselineskip
% to do so.
%
% \DescribeMacro{\marginnote}
% The command \cs{marginnote}\oarg{left}\marg{right}\oarg{voffset} my be used
% to set a margin note using \textsf{marginnote}. The first optional argument
% and the mandatory argument are same using \cs{marginpar} from the \LaTeX{}
% kernel. Even \cs{reversemarginpar} will be considered. The note \meta{left}
% or \meta{right} will be put at the current vertical position. Second
% optional argument \meta{voffset} may be used to adjust the vertical position
% of the margin note. Use a negativ dimension to move it up or a positiv
% dimension to move it down.
%
% \DescribeMacro{\marginnoteleftadjust}
% \DescribeMacro{\marginnoterightadjust}
% At some environments, e.g. \texttt{framed} from the \textsf{framed} package
% the horizontal placement of the margin notes are not correct. In this case
% you may redefine \cs{marginnoteleftadjust} and \cs{marginnoterightadjust} to
% fix this. Note that these are macros not lengths! So you have to use
% \cs{renewcommand}, \cs{def} or \cs{let} to change them. You may e.g. use
% \begin{verbatim}
% \begingroup
%   \makeatletter
%   \g@addto@macro\framed{%
%     \let\marginnoteleftadjust\FrameSep
%     \let\marginnoterightadjust\FrameSep
%   }
% \endgroup
% \end{verbatim}\vskip-\baselineskip
% at your preamble after loading package \textsf{framed} to fix the problem
% using \texttt{framed} environment.
%
% \DescribeMacro{\raggedleftmarginnote}
% \DescribeMacro{\raggedrightmarginnote}
% These macros define how the margin note will be aligned. The defaults are:
% \begin{itemize}
% \item align margin notes at the left margin right to the margin,
% \item align margin notes at the right margin left to the margin.
% \end{itemize}
% You may change this using \cs{renewcommand}, e.g. use^^A
% \changes{v1.0a}{2006/02/06}{Example to macros \cs{raggedleftmarginnote} and
%   \cs{raggedrightmarginnote} at documentation fixed [thanks to Susumu
%   Tanimura].}
% \begin{verbatim}
% \renewcommand*{\raggedleftmarginnote}{}
% \renewcommand*{\raggedrightmarginnote}{\centering}
% \end{verbatim}\vskip-\baselineskip
% to get justified text at the left and centered text at the right margin.
%
% \DescribeMacro{\marginfont}
% This macro defines the font that will be used to set margin notes. The
% default is \cs{normalcolor}. You may use \cs{renewcommand} to change this,
% e.g. use
% \begin{verbatim}
% \renewcommand*{\marginfont}{\color{red}\sffamily}
% \end{verbatim}\vskip-\baselineskip
% to get red colored margin notes in sans serif font family. You need to load
% e.g. package \textsf{color} to use \cs{color}.
%
%
% \section{Known Problems Using \textsf{marginnote}}
%
% At double side layout (e.g. using class option \texttt{twoside})
% \cs{marginnote} needs to know the number of the current page to decide
% wether the page is odd or even and so wether to use left or right
% margin. \LaTeX{} uses an asynchronous output. Because of this counter
% \texttt{page} should not be used to get the number of the current page
% unless you are at page head or foot. To solve the problem
% \textsf{marginnote} uses a mechanism similar to labels. But this means, that
% the correct margin won't be known at this \LaTeX{} run but only at the
% next. So after adding or deleting a margin note or after each change of page
% break you need two \LaTeX{} runs to get all margins right.
%
% The command \cs{marginnote} uses \cs{strut} and \cs{vadjust} to put the
% margin note at the correct position. But under some circumstances this may
% fail. You may adjust te vertical position of the margin note using the
% second optional argument of \cs{marginnote}. Sometimes even the text outside
% \cs{marginnote} will be moved because of using \cs{marginnote}. There's
% currently no other solution for this problem then moving the \cs{marginnote}
% command.
%
% Note: The margin note will be placed at the current vertical line. This
% means, if you are using two \cs{marginnote} commands at the same line, they
% will be put on the same place. This is not a bug but a feature!
%
% No page break may occure inside a margin note created with \cs{marginnote}.
%
% \cs{marginnote} somewhat different from \cs{marginpar} if used immediate
% after \cs{item}. This is not a bug, it's a feature!
%
% With math \cs{marginnote} may work or may not depending on the math
% environment.
%
%
% \StopEventually{\PrintIndex\PrintChanges}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% First process the options (no options are defined).
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
% \begin{macro}{\newmarginnote}
%   We need a macro to define a new note at the \texttt{aux} file. This will
%   be done using the mechanism of \LaTeX{} that is used for
%   \cs{newlabel}. But we use another prefix. This will result in the usual
%   ``Labels(s) may have changed. Rerun to get cross-references right.'' if a
%   margin note is new or have moved to another page.
%    \begin{macrocode}
\newcommand*{\newmarginnote}{\@newl@bel{mn}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mn@margintest}
% \begin{macro}{\@mn@thispage}
% \begin{macro}{\@mn@atthispage}
% \begin{macro}{mn@abspage}
%   Macro \cs{@margintest} does the complete test, which margin to use. The
%   result may be found at \cs{if@tempswa}. To avoid changes on the last page
%   if there is a new note on the first page, try to count the notes by
%   page. We know that this can not be successfull, but never the less it may
%   be a good try. \cs{@mn@thispage} saves the page number of the last usage
%   of \cs{@mn@margintest}. \cs{@mn@atthispage} saves the number of margin
%   note at this page. But we need to know the absolut page number to do
%   this. So we increase the absolut page number \texttt{mn@abspage} at every
%   \cs{@outputpage}.
%    \begin{macrocode}
\newcommand*{\@mn@thispage}{}
\let\@mn@thispage\@empty
\newcounter{mn@abspage}
\AtBeginDocument{\setcounter{mn@abspage}{1}%
  \g@addto@macro\@outputpage{\stepcounter{mn@abspage}}}
\newcommand*{\@mn@margintest}{%
%    \end{macrocode}
%   Number of the next margin note at this page.
%    \begin{macrocode}
  \expandafter\ifx\csname @mn@thispage\endcsname\@empty
    \gdef\@mn@atthispage{1}%
  \else\expandafter\ifnum \@mn@thispage=\value{mn@abspage}%
      \begingroup
        \@tempcnta\@mn@atthispage\advance\@tempcnta by \@ne
        \xdef\@mn@atthispage{\the\@tempcnta}%
      \endgroup
    \else
      \gdef\@mn@atthispage{1}%
    \fi
  \fi
  \xdef\@mn@thispage{\themn@abspage}%
%    \end{macrocode}
%   Use the number of the page and the number of the margin note at this page
%   to save the real number of this page at the \texttt{aux} file.
%    \begin{macrocode}
  \protected@write\@auxout{\let\themn@abspage\relax}{%
    \string\newmarginnote{note.\@mn@thispage.\@mn@atthispage}{\themn@abspage}%
  }%
%    \end{macrocode}
%   If we are not in two side mode, we are on a odd page.
%    \begin{macrocode}
  \if@twoside
%    \end{macrocode}
%   If the margin note label was not defined, it seams to be new. In this case
%   the absolut page number will be used for the test instead of the saved
%   real page number.
%    \begin{macrocode}
    \@ifundefined{mn@note.\@mn@thispage.\@mn@atthispage}{%
      \PackageInfo{marginnote}{Suggest that margin
        note \@mn@thispage.\@mn@atthispage\space will be on\MessageBreak
        absolute page \themn@abspage.\MessageBreak
        This may be wrong}%
      \ifodd\value{mn@abspage}\@tempswatrue\else\@tempswafalse\fi
    }{%
      \PackageInfo{marginnote}{Margin note \@mn@thispage.\@mn@atthispage\space
        is on absolute page \@nameuse{mn@note.\@mn@thispage.\@mn@atthispage}}%
      \ifodd\@nameuse{mn@note.\@mn@thispage.\@mn@atthispage}
        \@tempswatrue
      \else
        \@tempswafalse
      \fi
    }%
  \else
    \PackageInfo{marginnote}{right page because not two side mode}%
    \@tempswatrue
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\marginnote}
% \begin{macro}{\@mn@marginnote}
% \begin{macro}{\@mn@@marginnote}
% \begin{macro}{\@mn@@@marginnote}
%   Command \cs{marginnote} is the main macro of the package. The others are
%   helpers to manage the optional arguments.
%    \begin{macrocode}
\newcommand*{\marginnote}{%
  \@dblarg\@mn@marginnote
}
\newcommand{\@mn@marginnote}[2][]{%
  \ifhmode
    \@bsphack
    \begingroup
    \ifdim\@savsk>\z@\else
      \def\:{\@xifnch}\expandafter\def\: { \futurelet\@let@token\@ifnch}%
    \fi
  \else
    \begingroup
  \fi
  \@ifnextchar [{\@mn@@marginnote[{#1}]{#2}}{\@mn@@marginnote[{#1}]{#2}[\z@]}%
}
\newcommand{\@mn@@marginnote}{}
\def\@mn@@marginnote[#1]#2[#3]{%
  \endgroup
%    \end{macrocode}
%   In horizontal mode the space hack of the \LaTeX{} kernel will be used. In
%   vertical mode this should not be used.
%    \begin{macrocode}
  \ifhmode
    \@mn@@@marginnote[{#1}]{#2}[{#3}]%
    \@esphack
  \else
    \@mn@@@marginnote[{#1}]{#2}[{#3}]%
  \fi
}
\newcommand{\@mn@@@marginnote}{}
\def\@mn@@@marginnote[#1]#2[#3]{%
%    \end{macrocode}
%   All changes (but change of counters that are global because of using the
%   \LaTeX{} commands to change them an \cs{gdef} and \cs{xdef}) should be
%   local. In h-mode a \cs{strut} will be used to fix base line. The margin
%   note will be put to vertical list using \cs{vadjust}. This also means that
%   wie are one line to deep. This will be corrected later using negativ kern.
%   In v-mode wie use a special kind of vbox to simply set everything. Math
%   mode should behave like v-mode. And if we are just after an item we have
%   to leave v-mode first.
%    \begin{macrocode}
   \begingroup
     \ifmmode\strut\let\@tempa\vadjust\else
       \if@inlabel\leavevmode\fi
       \ifhmode\strut\let\@tempa\vadjust\else\let\@tempa\mn@vlap\fi
     \fi
     \@tempa{%
%    \end{macrocode}
%   Everything will be put upwards using a vbox with zero height and depth and
%   \cs{vss}. At this box the margin test will be done. If cs{reversemargin}
%   was used, the logic switchs. Then the note will be places to the margin.
%    \begin{macrocode}
       \vbox to\z@{%
         \vss
         \@mn@margintest
         \if@reversemargin\if@tempswa
             \@tempswafalse
           \else
             \@tempswatrue
         \fi\fi
         \if@tempswa
           \rlap{\kern\textwidth\kern\marginparsep\kern\marginnoterightadjust
             \vbox to\z@{\kern #3
               \vbox to\z@{%
                 \hsize\marginparwidth
%    \end{macrocode}
%   Here's the correction of the vertical position. The rest is simple.
%    \begin{macrocode}
                 \kern-\baselineskip\kern-\parskip
                 \marginfont\raggedrightmarginnote\hspace{\z@}\strut#2\endgraf
                 \vss}%
               \vss}%
           }%
         \else
           \llap{%
             \vbox to\z@{\kern #3
               \vbox to\z@{%
                 \hsize\marginparwidth
%    \end{macrocode}
%   Same like above for left margins.
%    \begin{macrocode}
                 \kern-\baselineskip\kern-\parskip
                 \marginfont\raggedleftmarginnote\hspace{\z@}\strut#1\endgraf
                 \vss}%
               \vss}%
             \kern\marginparsep\kern\marginnoteleftadjust
           }%
         \fi
       }%
     }%
   \endgroup
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\marginnoterightadjust}
% \begin{macro}{\marginnoteleftadjust}
% These may be used to define an automatic horizontal adjust. The default is
% zero.
%    \begin{macrocode}
\newcommand*{\marginnoterightadjust}{}
\newcommand*{\marginnoteleftadjust}{}
\let\marginnoterightadjust\z@
\let\marginnoteleftadjust\z@
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mn@vlap}
% This macro is used to set a vertical box without size at vertical mode.
%    \begin{macrocode}
\newcommand{\mn@vlap}[1]{%
  \setbox\@tempboxa\vbox to \ht\strutbox{#1\vss}%
  \box\@tempboxa\vskip-\baselineskip\ifhmode\typeout{FALSCH!}\fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marginfont}
%  \changes{v1.0a}{2006/02/06}{Use \cs{providecommand} to define it.}
% \begin{macro}{\raggedleftmarginnote}
% \begin{macro}{\raggedrightmarginnote}
%   These are very simple. A class may also define \cs{marginfont}. Use this
%   if available. I don't use \cs{let} for the definitions of the ragged
%   macros, so the meaning may change loading e.g. package \textsf{ragged2e}.
%    \begin{macrocode}
\providecommand*{\marginfont}{}
\newcommand*{\raggedleftmarginnote}{\raggedleft}
\newcommand*{\raggedrightmarginnote}{\raggedright}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \Finale
%
\endinput
%
% end of `marginnote.dtx'
%
% \iffalse
%%% Local Variables:
%%% mode: doc-tex
%%% mode: font-lock
%%% Text-master: t
%%% End:
% \fi