summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/misc/ut-backref.sty
blob: 89358c5db60bebfd151a3571377d0ce1118632db (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
%%
%% This is file `ut-backref.sty',
%%
%% Most of the code in here is taken from:
%%
%% File: backref.dtx Copyright (C) 1995, 1996, 1997 David Carlisle,
%%       Sebastian Rahtz
%% 
%% 
\def\fileversion{1.2}
\def\filedate{1998/11/03}
\ProvidesPackage{ut-backref}[\filedate:
  bibliographical back referencing, \fileversion]

%%% User-Interface

\long\def\page@backref#1#2#3{#1}
\long\def\section@backref#1#2#3{#2}
\long\def\hyper@section@backref#1#2#3{\hyperlink{#3}{#2}}
\long\def\hyper@page@backref#1#2#3{\hyperlink{page.#1}{#1}}
\DeclareOption{pageref}{%
  \def\backref{\small(cited on p\thinspace}%
  \def\backrefs{\small(cited on pp\thinspace}%
  \def\backrefclose{)}%
  \def\backrefsclose{)}%
  \let\backrefxxx\page@backref
}
\DeclareOption{ref}{%
  \def\backref{\small(cited in section\thinspace}%
  \def\backrefs{\small(cited in sections\thinspace}%
  \def\backrefclose{)}%
  \def\backrefsclose{)}%
  \let\backrefxxx\section@backref
}
\DeclareOption{hyperref}{%
  \def\backref{}%
  \def\backrefs{}%
  \def\backrefclose{}%
  \def\backrefsclose{}%
  \let\backrefxxx\hyper@section@backref
}
\DeclareOption{hyperpageref}{%
  \def\backref{}%
  \def\backrefs{}%
  \def\backrefclose{}%
  \def\backrefsclose{}%
  \let\backrefxxx\hyper@page@backref
}
\ExecuteOptions{pageref}
\ProcessOptions

%%% 

\newcommand\loopcmd{}
\newcommand\br@last{}

%%% a standard loop with `delimited arguments'. \end is used
%%% only as an end marker here. The argument will be
%%% expanded, so you'll get the meaning of e.g. \backrefxxx as
%%% last element.
\def\@getlast#1,#2\end{%
%        \typeout{   checking elem #1}%
        \if!#2! % test if ! equals !, i.e. if #2 is empty
        \edef\br@last{#1}
%        \typeout{Last elem is: \br@last}%
        \let\loopcmd\relax
        \else
        \def\loopcmd{\@getlast#2\end}%
        \fi
        \loopcmd
}%

%%% A wrapper adding `,' and `\end' so that we can check these
%%% in \@getlast
\newcommand\getlast[1]{\expandafter\@getlast#1,\end}

\newcounter{br@el@mcnt}

%%% this uses LaTeX's \@for ... \do loop. See ltcntrl.dtx for details.
\newcommand\lengthof[1]{%
    \setcounter{br@el@mcnt}{0}%
    \@for\testitem:=#1\do
        {\stepcounter{br@el@mcnt}}
%    \typeout{length of #1 is: \arabic{br@el@mcnt}}
    }

%%%

\@ifundefined{newblock}{\def\newblock{\par}}{}

\def\xcitedOn#1{%
\@ifundefined{br@#1}{%
\typeout{Warning: No page list for reference `#1'.}%
}{% At least one citation, find out how many
\newblock% newline if "open" format used
\edef\x{\csname br@#1\endcsname}% These are the citations
\lengthof{\x}% number of citations will be in br@el@mcnt
\ifnum\arabic{br@el@mcnt}>1% More than one citation?
\backrefs%
\x%
\backrefsclose%
\else% exactly one citation
\backref%
\x%
\backrefclose%
\fi%
}% end of if defined
}% end of \def\xcitedOn

\def\xbibitem{\@ifnextchar[{\@xlbibitem}{\@xbibitem}}
\def\@xlbibitem[#1]#2#3\par{%
 \oldb[#1]{#2}#3\xcitedOn{#2}
 \par
}%
\def\@xbibitem#1#2\par{%
 \oldb{#1}#2\xcitedOn{#1}
 \par
}
\def\backcite#1#2{% #1 = list of labels, #2 = page, section, text
  \@for\x:=#1\do{% for each label in list
    \edef\br@label{\thinspace\protect\backrefxxx#2}%
    \edef\br@list{\csname br@\x\endcsname}%
    \getlast{\br@list}%
%    \typeout{label \meaning\br@label, last \meaning\br@last}
    \ifx\br@label\br@last
    \relax\else%
   \edef\x{\expandafter\@firstofone\x\@empty}%
   \expandafter\protected@xdef\csname br@\x\endcsname{%
   \expandafter\ifx\csname br@\x\endcsname\relax\else
     \csname br@\x\endcsname,\fi\thinspace\protect\backrefxxx#2}%
   \fi
   }}
\def\@currentHref{}
\AtBeginDocument{%
  \let\oldb\bibitem
  \let\bibitem\xbibitem
  \let\oldbibl\thebibliography
  \def\thebibliography{\@starttoc{brf}{}\oldbibl}
  \@ifundefined{NAT@parse}{%
   \global\let\old@citex\@citex
   \global\let\@citex\x@citex
   }{\typeout{** backref set up for natbib ***}}
}
\def\x@citex[#1]#2{%
  \old@citex[#1]{#2}%
  \hy@backout{#2}%
}
\def\hy@backout#1{%
  \@bsphack
  \ifx\@empty\@currentlabel
    \protected@write\@auxout{}%
    {\string\@writefile{brf}%
      {\string\backcite{#1}{{\thepage}{(document)}{Doc-Start}}}}%
  \else
   \protected@write\@auxout{}%
   {\string\@writefile{brf}%
     {\string\backcite{#1}{{\thepage}{\@currentlabel}{\@currentHref}}}}%
  \fi
   \@esphack
}
\endinput
%%
%% End of file `backref.sty'.