summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/repeatindex/repeatindex.sty
blob: 62ab240c4f5d7775872028f902eab5226b835ebd (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
%%
%% repeatindex.sty
%%
%% Copyright 2001  Harald Harders (h.harders@tu-bs.de)
%% License is LPPL.
%%
%%
%% This Package repeats item of an index if a page or column
%% break occurs within a list of subitems. 
%% This helps to find out to which main item a subitem belongs.
%%
%% Example:
%%
%%    Main Item A, 1, 3, 6             Main Item A...
%%       Subitem a, 1                      Subitem g, 1
%%       Subitem b, 1                      Subitem h, 1
%%       Subitem c, 2                      Subitem i, 1
%%       Subitem d, 3                      Subitem j, 1
%%       Subitem d, 1                  Main Item B, 3, 6
%%       Subitem e, 4                      Subitem a, 3
%%       Subitem f, 2                      Subitem b, 4
%%
%% To get that behaviour you have to include the package into your
%% document using  \usepackage{repeatindex}, and you have to produce
%% the .ind file in a special format in which the entries of the \item
%% are enclosed in [...] (example:   \item [Main Item A], 1, 3, 6  ).
%% This can be achieved using repeatindex.ist:
%%     makeindex -s repeatindex  file
%%
%% Problems:
%% - When an item is repeated a small vertical space is included two
%%   items later
%% - If the last item is a subitem and the only entry on the last page
%%   of the index the repeation is omitted
%%
%% This package has not been tested much, yet. Please help me to find
%% more problems and if possible solutions to my problems.
%% I have had much trouble in solving the problem that the code
%% defined in afterpage is executed with a delay.
%%
%% Harald Harders, 2001/10/13
%% h.harders@tu-bs.de
%%
\ProvidesPackage{repeatindex}[2001/10/13 v0.01 Repeats index entries]
% Write text at page and column breaks
\RequirePackage{afterpage}
% Include Index stuff
\RequirePackage{makeidx}

% Command that replaces all contents of afterpage
\long\def\hhafterpage#1{%
  \gdef\AP@{{#1\par}}%
  \global\output{%
    \the\AP@output
    \ifnum\outputpenalty>-\@Mi
      \global\output\expandafter{\the\AP@output}%
      \aftergroup\AP@@
    \fi}%
}

% Entry to be repeated
% temporary variable
\def\@entry{}%
% final variable
\def\@@entry{}%

\def\@hhempty{\@empty}

\def\entryprefix{\itshape}
\def\entrypostfix{\dots}

\AtBeginDocument{%
  % Save original item commands
  \let\orig@idxitem\@idxitem
  \let\orig@subitem\subitem
  \let\orig@subsubitem\subsubitem
  %
  % New \item command, has to be used "\item [entry], pages"
  \renewcommand{\@idxitem}[1][\@empty]{%
    % call original command
    \orig@idxitem #1%
    % save entry in \@arg
    \def\@arg{#1}%
    \ifx\@arg\@hhempty
    \else
      % if entry not empty (which is the case if called by \subitem or
      % \subsubitem) copy entry to \@entry
      \xdef\@entry{#1}%
      % Delete the contents to be repeated
      \hhafterpage{}%
    \fi
  }%
  %
  % New \subitem command, to be used as usual
  \renewcommand{\subitem}{%
    % call original command
    \orig@subitem
    % set the contents to be repeated on the next page
    \expandafter\hhafterpage\expandafter{\expandafter\par\expandafter{%
        \expandafter\expandafter\entryprefix\@@entry\entrypostfix}}%
    % copy \@entry to \@@entry. This is done to delay the entry to be
    % put onto the page, due to a strange behaviour of afterpage (some
    % text of the next page is executed before the afterpage text is
    % put onto the page).
    \xdef\@@entry{\@entry}%
  }%
  %
  % New \subsubitem command, to be used as usual
  \renewcommand{\subsubitem}{%
    % call original command
    \orig@subsubitem
    % set the contents to be repeated on the next page
    \expandafter\hhafterpage\expandafter{\expandafter\par\expandafter{%
        \expandafter\expandafter\itshape\@@entry\dots}}%
    % copy \@entry to \@@entry. This is done to delay the entry to be
    % put onto the page, due to a strange behaviour of afterpage (some
    % text of the next page is executed before the afterpage text is
    % put onto the page).
    \xdef\@@entry{\@entry}%
  }%
  %
  % Copy original end of the theindex environment
  \let\orig@endtheindex\endtheindex
  % Redefine end of the theindex environment
  \renewcommand{\endtheindex}{%
    % switch off repeation
    \hhafterpage{}%
    % call original command
    \orig@endtheindex
  }%
%
}

\endinput