\ProvidesFile{bibliographie_dans_la_marge.sty}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Je redéfinit la commande \cite pour qu'elle mette les références
%% bibliographiques entre parenthèses et en exposant, avec le texte
%% dans la marge, en bas (s'il n'y a plus de place sur une page, les
%% notes sont reportées sur la suivante. Il est possible de regroupper
%% les citations, par exemple \cite{toto,tutu}, elles apparaîtrons
%% alors séparées par des virgules. Il est possible de citer la même
%% référence plusieures fois, elle portera toujours le même numéro et
%% le texte ne figurera dans la marge que la première fois. 
%% Il faut que BiBTeX produise un fichier *.bbl au bon format et que
%% celui-ci soit inclus au bon endroit : on pourra utiliser le style
%% bibliographie_dans_la_marge.bst (une modification de amsalpha.bst)
%% et faire figurer dans le préambule du document 
%%   \usepackage{afterbegindocument}
%%   \AfterBeginDocument{%
%%     \bibliographystyle{margin}%
%%     \nocite{*}%
%%     \bibliography{livres}%
%%   }
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Vincent Zoonekynd (zoonek@math.jussieu.fr)
%% Juin 1998
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Ce fichier (et ceux qui l'accompagnent) est distribué librement,
%% sans aucune garantie. Vous pouvez le modifier sous réserve de
%% changer son nom et de renseigner vos modifications.
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{bibliographie_dans_la_marge}
%\RequirePackage{widemargin} 
\RequirePackage{ifvide}
\RequirePackage{amstext}

%%
%% pseudo-bug : les commandes \bibliography doivent
%% être après le \begin{document}. Il convient donc d'utiliser 
%% \AfterBeginDocument
%%

%%
%% BUGS : 
%%  - manque de commentaires
%%  - pas de liste des fonctions et variables
%%  - pas de documentation, pas de fichier *.dtx, pas d'exemple, etc.
%%

\newinsert\biblio@margin
%\dimen\biblio=\maxdimen % ?????????
\dimen\biblio@margin=0.2\vsize
\count\biblio@margin=0
\skip\biblio@margin=0pt
\newcounter{biblio@margin@numero}
\renewcommand{\thebiblio@margin@numero}{\arabic{biblio@margin@numero}}
\newcommand{\bibliomarginstyle}{\footnotesize}
\newcommand{\bibliomarginskipamount}{\bigskipamount}

% Il y a toutefois un « léger » problème : la commande \insert
% n'a aucun effet quand elle est entourée d'accolades 
% en mode mathématique (avec le mode mathématique seul ou les 
% accolades seules, ça marche).
\newcommand{\bibliomargin}[2]{%
  \expandafter\def\csname biblio@margin@cite@#1\endcsname{%
    \stepcounter{biblio@margin@numero}%
    \thebiblio@margin@numero
    \expandafter\global\expandafter\edef\csname biblio@margin@cite@#1\endcsname{%
      \thebiblio@margin@numero}%
    }%
  \expandafter\def\csname biblio@margin@text@#1\endcsname{%
    \insert\biblio@margin{%
      \vskip \bibliomarginskipamount
      \noindent\parbox{\marginparwidth}{%
        \parindent=0pt
        \bibliomarginstyle
        \ensuremath{{}^{(\csname biblio@margin@cite@#1\endcsname)}} #2}}%
    \expandafter\global\expandafter\edef\csname biblio@margin@text@#1\endcsname{\relax}}%
  }

\def\biblio@margin@cite@aux#1{\immediate\write\@auxout{\string\citation{#1}}}
\def\biblio@margin@cite@text#1{\csname biblio@margin@text@#1\endcsname}
\def\biblio@margin@cite@number#1{\expandafter\ifx\expandafter\relax
  \csname biblio@margin@cite@#1\endcsname 
  \textbf{?}% 
  \G@refundefinedtrue
  \@latex@warning{Citation `#1' on page \thepage \space undefined}%
  \else \csname biblio@margin@cite@#1\endcsname\fi}

\newtoks\biblio@margin@tok@cite
\def\cite#1{%
  \edef\biblio@margin@cite@tmp{#1}% Nécessaire car la virgule peut être un
                     % caractère actif.
  \biblio@margin@tok@cite={}%
  \expandafter\biblio@margin@cite\biblio@margin@cite@tmp,@\ensuremath{^{(\the\biblio@margin@tok@cite)}}}%
\def\biblio@margin@cite#1,#2@{%
  \global\biblio@margin@tok@cite\expandafter{\the\biblio@margin@tok@cite\biblio@margin@cite@number{#1}}%
  \biblio@margin@cite@aux{#1}%
  \biblio@margin@cite@text{#1}%
  \ifvide{#2}{}{%
      \global\biblio@margin@tok@cite\expandafter{\the\biblio@margin@tok@cite,}%
      \biblio@margin@cite#2@}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\@outputpage{%
\begingroup           % the \endgroup is put in by \aftergroup
  \let \protect \noexpand
  \@resetactivechars
  \let\-\@dischyph
  \let\'\@acci\let\`\@accii\let\=\@acciii
  \let\\\@normalcr
  \let\par\@@par          %% 15 Sep 87
  \shipout \vbox{%
    \set@typeset@protect
    \aftergroup \endgroup
    \aftergroup \set@typeset@protect
                                % correct? or just restore by ending
                                % the group?
  \if@specialpage
    \global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
  \fi
  \if@twoside
    \ifodd\count\z@ \let\@thehead\@oddhead \let\@thefoot\@oddfoot
         \let\@themargin\oddsidemargin
    \else \let\@thehead\@evenhead
       \let\@thefoot\@evenfoot \let\@themargin\evensidemargin
    \fi
  \fi
  \reset@font 
  \normalsize
  \baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
    \@begindvi
    \vskip \topmargin
    \moveright\@themargin \vbox {%
      \setbox\@tempboxa \vbox to\headheight{%
        \vfil
        \color@hbox
          \normalcolor
          \hb@xt@\textwidth {%
            \let \label \@gobble
            \let \index \@gobble
            \let \glossary \@gobble %% 21 Jun 91
            \@thehead
            }%
        \color@endbox
        }%                        %% 22 Feb 87
      \dp\@tempboxa \z@
      \box\@tempboxa
      \vskip \headsep
      \box\@outputbox
      %
        %%%%%% \begin{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      \rlap{\hbox{\hspace{\hsize}%
          \vbox to 0pt{%
            \vss
            \ifvoid\biblio@margin\else
            \hbox{%
              \hspace{\marginparsep}%
              \hspace{-3.2pt}\vrule width 0.4pt\hspace{2.8pt}%
              \vbox{%
                \vskip -\bibliomarginskipamount
                \box\biblio@margin}}%
            \fi
            }}}%
        %%%%%% \end{MODIF} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %
      \baselineskip \footskip
      \color@hbox
        \normalcolor
        \hb@xt@\textwidth{%
          \let \label \@gobble
          \let \index \@gobble      %% 22 Feb 87
          \let \glossary \@gobble   %% 21 Jun 91
          \@thefoot
          }%
      \color@endbox
      }%
    }%
\global \@colht \textheight
\stepcounter{page}%
\let\firstmark\botmark
}


