locallabel.sty 4.3 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
%  Locallabel
%
%  Copyright (C) 2001, 2002, 2003 Didier Rmy
%
%  Author         : Didier Remy 
%  Version        : 1.1.1
%  Bug Reports    : to author
%  Web Site       : http://pauillac.inria.fr/~remy/latex/
% 
%  Locallabel 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, or (at your option)
%  any later version.
%  
%  Locallabel 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 
%  (http://pauillac.inria.fr/~remy/license/GPL).
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  File locallabel.sty (LaTeX macros)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%

%% Identification
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{locallabel}
         [2001/23/02 v0.92 Locallabel]

%% Preliminary declarations

%% Options

%% More declarations

%% We use two counters: The global counter is incremented at each reset.
%% Its value is the ``group'' of a local. 
%% The local counter is the last numeric value of a bound label in the
%% current group.  The value of a label #1 is globally set to 
%% \csname llb@\the\c@llb@global-#1\endcsname
%% The global command \csname llb@\the\c@llb@global-#1*\endcsname is 
%% use to ensure that a label is only bound once. Usually a label is
%% bound and declared at the same time with \llabel. It may also be bound in
%% advance,  with \lbind, for instance so as to control the numbering.
%% Then, another \llabel must be used to declare it in the text.
%% If no \lbind has been used before, the \llabel calls \lbind implicitlt. 

\newcounter{llb@global}
\newcounter{llb@local}

\newcommand \llb@find [1]
  {\expandafter \ifx \csname llb@\the\c@llb@global-#1\endcsname \relax
     \message {*** Local label #1 undefined in this context}%
     \edef \llb@current {#1??}%
   \else 
     \edef \llb@current {\csname llb@\the\c@llb@global-#1\endcsname}%
   \fi}

\newcommand \llb@make [1]
  {\expandafter \ifx \csname llb@\the\c@llb@global-#1\endcsname \relax
     \stepcounter{llb@local}\relax \expandafter 
     \xdef \csname llb@\the\c@llb@global-#1\endcsname {\the\c@llb@local}%
     \edef \llb@current {\the\c@llb@local}%
   \else
     \expandafter \ifx \csname llb@\the\c@llb@global-#1*\endcsname \relax
       \message {*** Local label #1 already defined in this countext!}%
       \edef \llb@current {\csname llb@\the\c@llb@global-#1\endcsname ??}%
     \else
       \expandafter \global \expandafter \let
          \csname llb@\the\c@llb@global-#1*\endcsname \relax
     \edef \llb@current {\csname llb@\the\c@llb@global-#1\endcsname}
     \fi 
   \fi
}

%%% Redefine those macros to change typsetting

\newcommand \thelocallabel {\the \c@llb@local}

\newcommand \LlabelTypeset [1] {(\textrm {\bfseries #1})}
\newcommand \LrefTypeset [1] {(\textrm {#1)}}
\newcommand \glabel [1]{\LlabelTypeset{\softtarget {#1}{#1}}}
\newcommand \gref [1]{\LrefTypeset{\softlink {#1}{#1}}}

%%% To reset all local labels---which just increment a global prefix.
\newcommand \locallabelreset[1][0]%
  {\stepcounter {llb@global}\setcounter {llb@local}{#1}}

%%% Make a new local label, typeset it, and bind to the given name
\def \llb@relax {\relax}
\newcommand {\llabel}[2][\relax]%
  {\llb@make{#2}%
   \def \@test {#1}\ifx \@test\llb@relax\else
     \edef \@currentlabel {\the\c@llb@local}%
     \def \@test {#1}\ifx \@test\empty \def \@test{#2}\fi
     \label{\@test}%
   \fi%
   \LlabelTypeset {\softtarget{llb@\the\c@llb@global-#2}{\llb@current}}}

%%% Retreive the local label of given name and type set it.
\newcommand \lref [1]
  {\llb@find {#1}%
   \LrefTypeset {\softlink {llb@\the\c@llb@global-#1}{\llb@current}}}

%%% Make a new local label and bind it to the given name but do not typeset
%%% it. Typesetting may then be done with \llabel non locally. Useful to
%%% control the order of numberring.
\newcommand \lbind [1]
  {\llb@make {#1}%
   \expandafter \global \expandafter 
      \let \csname llb@\the\c@llb@global-#1*\endcsname \empty}
   
\AtBeginDocument {%
  \@ifundefined{softlink}{\let \softlink \@secondoftwo}{}%
  \@ifundefined{softtarget}{\let \softtarget \@secondoftwo}{}%
}