| 1 |
%% file: index.tex - creating an index - TeXsis 2.18 |
| 2 |
% @(#) $Id: index.tex,v 18.0 2000/05/16 22:37:11 myers Exp $ |
| 3 |
%======================================================================* |
| 4 |
% index.tex -- Macros for creating an index using MakeIndex |
| 5 |
% |
| 6 |
% Use \index{key} or \idx{word} to make an entry in the index file |
| 7 |
% [\idx{word} is more or less the same as ``word\index{word}'']. |
| 8 |
% At the end of the job the file \jobname.idx is the raw index file, |
| 9 |
% ready to be fed into the MakeIndex program to create the index. |
| 10 |
% The MakeIndex output file \jobname.ind is then fed back through TeX with |
| 11 |
% the output formatting macros below to print the final formatted index. |
| 12 |
% |
| 13 |
% To process the index file with MakeIndex the command is simply |
| 14 |
% |
| 15 |
% makeindex jobname.idx |
| 16 |
% |
| 17 |
% You can leave off the .idx extension for the raw file and makeindex |
| 18 |
% will assume it. This will produce the processed index file, called |
| 19 |
% \jobname.ind. To print the index you can either include it in |
| 20 |
% another pass of the full document with ``\input \jobname.ind'' or |
| 21 |
% print it separately by adding ``\input index'' to the begining of the |
| 22 |
% \jobname.ind file and running it through TeX. |
| 23 |
% |
| 24 |
% If you set \markindextrue then indexed words selected with \idx |
| 25 |
% will appear in the printed output inside a ruled box. This is |
| 26 |
% useful for draft copies when you are putting together the index, |
| 27 |
% but obviously you do not want to use it in a final copy. |
| 28 |
% The box does not interfere with or change the line spacing. |
| 29 |
% |
| 30 |
% These macros are completely compatible with Plain TeX, although |
| 31 |
% they can also be used with TeXsis (some of the macros used below |
| 32 |
% are a part of TeXsis). Note however that these macros support output |
| 33 |
% from MakeIndex in it's default LaTeX mode. You don't need a separate |
| 34 |
% style file for MakeIndex! |
| 35 |
% |
| 36 |
% For more information about how to make full use of MakeIndex read |
| 37 |
% the man page for makeindex(1) and/or the paper ``Index Preparation |
| 38 |
% and Processing'' by Pehong Chen and Michale A. Harrison, |
| 39 |
% Software: Practice and Experience, Vol 19, No. 9, Sep. 1988, pp. 897-915 |
| 40 |
% (a copy is available at ftp://ftp.texsis.org/texsis/index/index.tex ) |
| 41 |
% |
| 42 |
% It is possible to make more than one index by creating a few extra |
| 43 |
% macros based on the ones below. See the documentation at the end |
| 44 |
% of this file for details. |
| 45 |
% |
| 46 |
%======================================================================* |
| 47 |
% (C) Copyright 1992, 1999, 2000 by Eric Myers and Frank Paige. |
| 48 |
% This file is a part of TeXsis. Distribution and/or modifications |
| 49 |
% are allowed under the terms of the LaTeX Project Public License (LPPL). |
| 50 |
% See the file COPYING or ftp://ftp.texsis.org/texsis/LPPL |
| 51 |
%======================================================================* |
| 52 |
\message{- indexing macros} |
| 53 |
\catcode`@=11 % @ is a letter here to hide workings |
| 54 |
|
| 55 |
\newif\ifmarkindex \markindexfalse % put proof marks in text? |
| 56 |
\newif\ifmakeindex \makeindextrue % set false to suppress .idx output |
| 57 |
|
| 58 |
%---------------------------* |
| 59 |
% Index Entries: |
| 60 |
% |
| 61 |
% \idx{word} is the quickest way to make an index entry. |
| 62 |
% The word is put in the text and an index entry is made at the |
| 63 |
% same time. It is the same as typing ``word\index{word}''. |
| 64 |
% If \markintdextrue is selected the word appears in the text with a |
| 65 |
% box around it so you know it has been included in the index. |
| 66 |
|
| 67 |
|
| 68 |
\def\idx#1{% include a word in the index output file |
| 69 |
#1\index{#1}% % put word in text and \index it |
| 70 |
\ifmarkindex % proof marks? |
| 71 |
\llap{\lower\jot\vbox to 0pt{\vss % box it |
| 72 |
\tightboxit{\loosebox{\phantom{#1}}}}\hskip-\jot}% |
| 73 |
\fi} % put a ruled box around word |
| 74 |
|
| 75 |
|
| 76 |
% \index{key} makes an index entry for the given key. |
| 77 |
% Since MakeIndex can use special characters like @ | ! and " in the key |
| 78 |
% we must make sure that they are printed correctly to the raw index file. |
| 79 |
|
| 80 |
\def\index{% make an index entry for a given key |
| 81 |
\begingroup % turn MakeIndex special characters to letters |
| 82 |
\catcode`@=11 \catcode`"=11 \catcode`!=11 \catcode`|=11 \catcode`-=11 |
| 83 |
\@index} |
| 84 |
|
| 85 |
\def\@index#1{% get the index entry and write it to the file |
| 86 |
\endgroup % special characters back to before |
| 87 |
\ifmakeindex % .idx file output enabled? |
| 88 |
\indexwrite\indexout{\indexentry{#1}}% write entry to file |
| 89 |
\fi} |
| 90 |
|
| 91 |
|
| 92 |
% \loosebox put a loose box around a box. |
| 93 |
% \tightboxit draws a rule box around the box #1 without any added space |
| 94 |
|
| 95 |
\def\loosebox#1{% |
| 96 |
\vbox{\vskip\jot |
| 97 |
\hbox{\hskip\jot #1\hskip\jot}% |
| 98 |
\vskip\jot}} |
| 99 |
|
| 100 |
\def\tightboxit#1{\vbox{\hrule\hbox{\vrule\vbox{#1}\vrule}\hrule}} |
| 101 |
|
| 102 |
%---------------------------* |
| 103 |
% Index Formatting (printing the processed index file) |
| 104 |
|
| 105 |
|
| 106 |
% The index is begun by \theindex or \begin{theindex} and ended |
| 107 |
% with \endtheindex or \end{theindex} |
| 108 |
|
| 109 |
|
| 110 |
\def\theindex{% begin a formatted index |
| 111 |
\begingroup |
| 112 |
\parskip = 0pt % no extra space between items |
| 113 |
\def\item{\indexitem{0pt}}% % level 0 item separator |
| 114 |
\def\subitem{\indexitem{20pt}}% % level 1 item separator |
| 115 |
\def\subsubitem{\indexitem{30pt}}% % level 2 item separator |
| 116 |
\def\indexspace{\medskip}% % intergroup vertical space |
| 117 |
\def\setcounter##1##2{\global\csname ##1no\endcsname=##2}% to set page counter |
| 118 |
\def\bold##1{{\bf ##1}}% % boldface for |bold |
| 119 |
\def\italic##1{{\it ##1}}% % italics for |italic |
| 120 |
\def\slanted##1{{\sl ##1}}% % slanted for |slanted |
| 121 |
\def\see##1##2{{\it see} ##1}% % italics for |see |
| 122 |
\def\seealso##1{{\it see also}##1}%% italics for |seealso |
| 123 |
\def\also##1{{\it see also}##1}% % italics for |also |
| 124 |
} |
| 125 |
|
| 126 |
\def\indexitem#1{% how to make an entry for an item in the index |
| 127 |
\vskip 0pt % force vertical mode |
| 128 |
\hangindent=30pt\hangafter=1 % hanging indent for extra lines |
| 129 |
\noindent\hskip #1} % level N item separator |
| 130 |
|
| 131 |
\def\endtheindex{% end a formatted index |
| 132 |
\endgroup} |
| 133 |
|
| 134 |
|
| 135 |
% If you don't have MakeIndex you can still make a sorted index. |
| 136 |
% (Even though it may not look as good. If you are really going to |
| 137 |
% do it right, do get MakeIndex). The Unix sort(1) command or a similar |
| 138 |
% program can be used to sort the \jobname.idx file. You can then |
| 139 |
% edit the file to collect all the page numbers for a key into |
| 140 |
% one \indexentry, like ``\indexentry{foo}{29,47-48}'' , and \indexentry |
| 141 |
% will format the items as best it can. |
| 142 |
|
| 143 |
|
| 144 |
\def\indexentry#1#2{% prints an index entry (from the raw file) |
| 145 |
\indexitem{0pt}#1, #2} % keyword, page number |
| 146 |
|
| 147 |
|
| 148 |
%-------------------------* |
| 149 |
% Simple \begin .. \end support (like in LaTeX): There is a conspiracy |
| 150 |
% to preserve job security for typists by making sure one has to |
| 151 |
% type \begin{thing} and \end{thing} everywhere. In TeXsis it's just |
| 152 |
% \thing and \endthing, so here we just make \begin{thing} become \thing |
| 153 |
% and \end{thing} become \endthing. Then we can use MakeIndex in it's |
| 154 |
% default mode, even though that is for LaTeX. |
| 155 |
% Note: we don't check that you begin and end the same thing. After all, |
| 156 |
% you might want to say \begin{midfigure} followed by \end{figure}. |
| 157 |
|
| 158 |
|
| 159 |
\def\begin#1{% \begin{foo} just becomes \foo |
| 160 |
\begingroup % changes to \end are temporary |
| 161 |
\let\end=\endbegin % now \end will end the \begin |
| 162 |
\expandafter\ifx\csname #1\endcsname\relax\relax % is \thing defined? |
| 163 |
\def\next{\beginerror{#1}}% NO: flag an error |
| 164 |
\else % |
| 165 |
\def\next{\csname #1\endcsname}% YES: just invoke it |
| 166 |
\fi\next} |
| 167 |
|
| 168 |
\def\endbegin#1{% \end{foo} just becomes \endfoo |
| 169 |
\endgroup % now \end goes back to normal |
| 170 |
\expandafter\ifx\csname end#1\endcsname\relax\relax % is \endthing defined? |
| 171 |
\def\next{\begingroup\beginerror{end#1}}% % NO: flag an error |
| 172 |
\else % |
| 173 |
\def\next{\csname end#1\endcsname}% YES: just invoke it |
| 174 |
\fi\next} |
| 175 |
|
| 176 |
|
| 177 |
\newhelp\beginhelp{begin: |
| 178 |
The \string\begin\space or \string\end\space marked above is for a^^M% |
| 179 |
non-existant environment. Check for spelling errors and such.} |
| 180 |
|
| 181 |
|
| 182 |
\def\beginerror#1{% \begin{foo} or \end{foo} fail if no \foo or \endfoo |
| 183 |
\endgroup % make sure \end is back to normal |
| 184 |
\errhelp=\beginhelp % longer help message |
| 185 |
\newlinechar=`\^^M % ^^M is line break |
| 186 |
\errmessage{Undefined environment for \string\begin\space or \string\end}} |
| 187 |
|
| 188 |
|
| 189 |
%---------------------------* |
| 190 |
% Index file output: |
| 191 |
% |
| 192 |
|
| 193 |
\newwrite\indexout % raw index output file |
| 194 |
|
| 195 |
|
| 196 |
% \indexinit will only get called once. It opens the output file, |
| 197 |
% write an identifying comment to it, and then undefines itself. |
| 198 |
|
| 199 |
\def\indexinit{% open and initialize raw index file |
| 200 |
\immediate\openout\indexout=\jobname.idx % open file as \jobname.idx |
| 201 |
\immediate\write\indexout{\@comment Raw index file ``\jobname.idx''}% |
| 202 |
\immediate\write\indexout{\@comment ====================================}% |
| 203 |
\gdef\indexinit{\relax}} % so we initialize only once |
| 204 |
|
| 205 |
% \@comment is for writing % to a file. It expands to '% '. |
| 206 |
|
| 207 |
{\catcode`\%=11 \gdef\@comment{% }} |
| 208 |
|
| 209 |
|
| 210 |
% \indexwrite writes an entry to the raw index file named by #1. Argument |
| 211 |
% #2 is the \indexentry{foo}, so it is not expanded (which is why this is so |
| 212 |
% complicated), while \@indexpage is the page number, which *is* expanded. |
| 213 |
|
| 214 |
\def\indexwrite#1#2{% % write entry to raw index file |
| 215 |
\indexinit % open output file if not already |
| 216 |
\def\@finwrite{\write#1}% % to write to index output file (#1) |
| 217 |
\begingroup % begin collecting tokens on stack |
| 218 |
\aftergroup\@finwrite % put \write\indexout on stack |
| 219 |
\aftergroup{\relax % put { on stack |
| 220 |
\@stackNX#2\@endstack % puts \noexpand\token... on stack |
| 221 |
\aftergroup\@indexpage % expanded {enclosed} page number |
| 222 |
\aftergroup}\relax % put } on stack |
| 223 |
\endgroup} % now dump from stack to \write |
| 224 |
|
| 225 |
\def\@indexpage{{\folio}} % to get the {enclosed} page number |
| 226 |
|
| 227 |
% Saying \@stackNX <stuff>\@endstack will take all the tokens |
| 228 |
% in <stuff> and put them on the stack, with every control sequence |
| 229 |
% prefaced by \noexpand. \indexwrite uses this for unexpanded write. |
| 230 |
|
| 231 |
\def\@stackNX{\futurelet\next\@stackswitch} |
| 232 |
|
| 233 |
\def\@stackswitch{% decides what to do with various types of tokens |
| 234 |
\ifx\next\@endstack\relax % if \@endstack then we'll quit |
| 235 |
\else\ifcat\noexpand\next\@stoken % if token is a space, |
| 236 |
\aftergroup\space\let\next=\@eatnext% write \space and eat token |
| 237 |
\else\ifcat\noexpand\next\bgroup % if token is \bgroup |
| 238 |
\aftergroup{\let\next=\@eatnext % write { and eat it |
| 239 |
\else\ifcat\noexpand\next\egroup % if token is \egroup |
| 240 |
\aftergroup}\let\next=\@eatnext % write a } and eat it |
| 241 |
\else % otherwize |
| 242 |
\let\next=\@stacktoken % just copy to the stack with \NX |
| 243 |
\fi\fi\fi\fi % end all \if's |
| 244 |
\next} % do it to the token |
| 245 |
|
| 246 |
\def\\{\global\let\@stoken= }\\ |
| 247 |
|
| 248 |
% \@eatnext ignores the \next token and goes on to the |
| 249 |
% one following in the argument list |
| 250 |
|
| 251 |
\def\@eatnext{\afterassignment\@stackNX\let\next= } |
| 252 |
|
| 253 |
% \@stacktoken copies one token to the stack with \aftergroup, |
| 254 |
% then calls \@stackNX to get the one following. |
| 255 |
|
| 256 |
\long\def\@stacktoken#1{% write token to stack, unexpanded |
| 257 |
\ifcat\noexpand#1\relax % if token is a control sequence |
| 258 |
\aftergroup\noexpand % preface with \noexpand |
| 259 |
\else\ifcat\noexpand#1\noexpand~\relax % if token is an active character |
| 260 |
\aftergroup\noexpand % preface with \noexpand |
| 261 |
\fi\fi % |
| 262 |
\aftergroup#1\relax % put the next token on the list |
| 263 |
\@stackNX} % do next token from argument... |
| 264 |
|
| 265 |
\def\@endstack\@endstack{} % just marks the end of token list |
| 266 |
|
| 267 |
\catcode`@=12 % @ goes back to \other, not a letter |
| 268 |
|
| 269 |
%---------------------------* |
| 270 |
% MULTIPLE INDICES: |
| 271 |
% |
| 272 |
% If you want more than one index I can't guess how you will want to |
| 273 |
% use them, so all I can do is tell you how to use the macros above to |
| 274 |
% create another (or several other) additional indices: |
| 275 |
% |
| 276 |
% 1) Create another output file and open it, with something like: |
| 277 |
% |
| 278 |
% \newwrite\otherindexout |
| 279 |
% \immediate\openout\otherindexout=other.idx |
| 280 |
% |
| 281 |
% 2) Define a macro to make the entry to the second index, following |
| 282 |
% the pattern of \index and \@index above, but using the other index |
| 283 |
% file (but don't use @ in a macro name). For example: |
| 284 |
% |
| 285 |
% \def\OtherIndex{\begingroup % special characters letters |
| 286 |
% \catcode`@=11 \catcode`"=11 \catcode`!=11 \catcode`|=11 |
| 287 |
% \catcode`-=11 |
| 288 |
% \Oindex} |
| 289 |
% |
| 290 |
% \def\Oindex#1{\endgroup % special characters return |
| 291 |
% \indexwrite\otherindexout{\indexentry{#1}}} |
| 292 |
% |
| 293 |
% Then saying \OtherIndex{word} will make an entry into the alternate |
| 294 |
% index for ``word''. |
| 295 |
% |
| 296 |
%>>> EOF index.tex <<< |