mdwref.dtx: Rearrange the machinery for later extension.
[mdwtools] / mdwref.dtx
1 % \begin{meta-comment} <general public licence>
2 %%
3 %% mdwref package -- slightly fancy cross-referencing stuff
4 %% Copyright (c) 2007, 2019 Mark Wooding
5 %%
6 %% This file is part of the `mdwtools' LaTeX package collection.
7 %%
8 %% `mdwtools' is free software: you can redistribute it and/or modify it
9 %% under the terms of the GNU General Public License as published by the
10 %% Free Software Foundation; either version 2 of the License, or (at your
11 %% option) any later version.
12 %%
13 %% `mdwtools' is distributed in the hope that it will be useful, but
14 %% WITHOUT ANY WARRANTY; without even the implied warranty of
15 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 %% General Public License for more details.
17 %%
18 %% You should have received a copy of the GNU General Public License
19 %% along with `mdwtools'. If not, write to the Free Software Foundation,
20 %% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 %%
22 % \end{meta-comment}
23 %
24 % \begin{meta-comment} <Package preambles>
25 %<+package>\NeedsTeXFormat{LaTeX2e}
26 %<+package>\ProvidesPackage{mdwref}
27 %<+package> [2020/09/06 1.14.0 Cross-referencing]
28 % \end{meta-comment}
29 %
30 % ^^A\CheckSum{96}
31 %% \CharacterTable
32 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
33 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
34 %% Digits \0\1\2\3\4\5\6\7\8\9
35 %% Exclamation \! Double quote \" Hash (number) \#
36 %% Dollar \$ Percent \% Ampersand \&
37 %% Acute accent \' Left paren \( Right paren \)
38 %% Asterisk \* Plus \+ Comma \,
39 %% Minus \- Point \. Solidus \/
40 %% Colon \: Semicolon \; Less than \<
41 %% Equals \= Greater than \> Question mark \?
42 %% Commercial at \@ Left bracket \[ Backslash \\
43 %% Right bracket \] Circumflex \^ Underscore \_
44 %% Grave accent \` Left brace \{ Vertical bar \|
45 %% Right brace \} Tilde \~}
46 %%
47 %
48 % \begin{meta-comment}
49 %
50 %<*driver>
51 \input{mdwtools}
52 \describespackage{mdwref}
53 \usepackage{mdwtab}
54 \mdwdoc
55 %</driver>
56 %
57 % \end{meta-comment}
58 %
59 %^^A-------------------------------------------------------------------------
60 %
61 % \section{User guide}
62 %
63 % I always name my cross-reference labels with a prefix telling me what kind
64 % of thing they are. A figure might be |fig:foo| or a table |tab:bar|. When
65 % I refer to the thing, then, I basically have to repeat myself:
66 % `|see table~\ref{tab:bar}|'. Kinda silly.
67 %
68 % \DescribeMacro\xref
69 % The |\xref| command understands my prefixing system. I can say
70 % `|\xref{tab:bar}|' and it inserts a reference to `table~4', for example.
71 % This is, of course, useless if you want to put the reference at the
72 % beginning of a sentence: `Table~4 shows\dots'.
73 % \DescribeMacro\Xref
74 % The |\Xref| command (note the initial capital) handles this properly, so
75 % you just type `|\Xref{tab:bar} shows|\dots'.
76 %
77 % The full syntax of the |\xref| command is like this.
78 % \begin{grammar}
79 % <xref-command> ::= \[[
80 % "\\xref"
81 % \[ "[" <mangle> "]" \]
82 % "{" <reference> "}"
83 % \]]
84 % \end{grammar}
85 % The optional \<mangle> argument is a command to be applied to the generated
86 % text: it \emph{must} be a single token. Rather than printing `table', or
87 % whatever, it prints \syntax{<mangle>"{table}"}.
88 % The most obvious application of this is the |\Xref| command, which uses a
89 % helper |\toupper|.
90 % \DescribeMacro\toupper
91 % The call \syntax{"\\toupper{"<stuff>"}"} typesets \<stuff> with the first
92 % character in uppercase. So |\Xref| is defined simply as\footnote{Modulo
93 % the fact that the author is a dreadful \TeX\ hacker.}
94 % \begin{listing}
95 %\newcommand{\Xref}[1]{\xref[\toupper]{#1}}
96 % \end{listing}
97 %
98 % All that remains is to define the strings to be typeset for various kinds
99 % of labels.
100 % \DescribeMacro\defxref
101 % For this, we use the |\defxref| command:
102 % \begin{grammar}
103 % <definition> ::= \[[
104 % "\\defxref"
105 % "{" <prefix> "}"
106 % "{" <string> "}"
107 % \]]
108 % \end{grammar}
109 % The \<prefix> is what you put on the front of your labels; the \<string> is
110 % the string to be typeset by |\xref|. Such references are typeset using
111 % |\formatxref|, described below.
112 %
113 % \DescribeMacro\formatxref
114 % Simple references are typeset by calling
115 % \syntax{"\\formatxref{"<mangle>"}{"<string>"}{"<label>"}"}, which can do as
116 % it pleases: the \<mangle> token is from the |\xref| invocation; the
117 % \<string> is category of thing being referred to (as established by
118 % |\defxref| below); and \<label> is the label, again from |\xref| . The
119 % default behaviour is to print
120 % \syntax{<mangle>"{"<string>"}~\\ref{"<label>"}"}, but this can be
121 % overridden.
122 % (Not quite true: in fact, the default does something better if
123 % \package{hyperref} is detected, but the idea is basically the same.)
124 %
125 % A number of useful prefixes are already defined, following my usual
126 % preferences; they're shown in \xref{tab:defs}.
127 % \begin{table}
128 % \begin{tabular}[C]{ll} \hlx*{hv}
129 % \textbf{Prefix} & \textbf{Text} \\ \hlx{vhv}
130 % \texttt{ch} & chapter $n$ \\
131 % \texttt{app} & appendix $n$ \\
132 % \texttt{sec} & section $n$ \\
133 % \texttt{def} & definition $n$ \\
134 % \texttt{th} & theorem $n$ \\
135 % \texttt{lem} & lemma $n$ \\
136 % \texttt{prop} & proposition $n$ \\
137 % \texttt{cor} & corollary $n$ \\
138 % \texttt{fig} & figure $n$ \\
139 % \texttt{tab} & table $n$ \\
140 % \texttt{eq} & equation $n$ \\
141 % \texttt{i} & item $n$ \\
142 % \texttt{ex} & exercise $n$ \\
143 % \hlx*{vh}\end{tabular}
144 % \caption{Predefined reference prefixes}
145 % \label{tab:defs}
146 % \end{table}
147 %
148 % \implementation
149 % \section{Implementation}
150 %
151 % \begin{macrocode}
152 %<*package>
153 % \end{macrocode}
154 %
155 % The following quark will be useful.
156 % \begin{macrocode}
157 \def\q@delim{\q@delim}
158 % \end{macrocode}
159 %
160 % \begin{macro}{\defxref}
161 % Defining prefixes is easy. We store the text for each prefix in a macro
162 % called \syntax{"\\xref$"<prefix>}, which takes two arguments: a \<mangle>
163 % token (or |\relax|), and the reference name.
164 % \begin{macrocode}
165 \def\defxref#1#2{%
166 {\expandafter\def\csname xref$#1\endcsname##1##2{\formatxref{##1}{#2}{##2}}}
167 % \end{macrocode}
168 % \end{macro}
169 %
170 % \begin{macro}{\xrefdispatch}
171 % \begin{macrocode}
172 \def\xref@hyper#1{\hyperref[#1]}
173 \def\xrefdispatch#1{%
174 \ifx\hyperref\@@undefined \def\@tempa{#1\@gobble\ref}%
175 \else \def\@tempa{#1\xref@hyper{\ref*}}%
176 \fi \@tempa%
177 }
178 % \end{macrocode}
179 % \end{macro}
180 %
181 % \begin{macro}{\formatxref}
182 % Output a cross-reference in the right way.
183 % \begin{macrocode}
184 \def\formatxref{\xrefdispatch\xref@formatsimple}
185 \def\xref@formatsimple#1#2#3#4#5{#1{#5}{#3{#4}~#2{#5}}}
186 \def\xref@fallback#1{\formatxref\relax{?\texttt{#1}}{#1}}
187 % \end{macrocode}
188 % \end{macro}
189 %
190 % \begin{macro}{\xref}
191 % We're meant to typeset a reference. The first job is to see whether
192 % there's an optional argument. If so, grab it; otherwise |\relax| will do.
193 % \begin{macrocode}
194 \def\xref{\@ifnextchar[\xref@{\xref@[\relax]}}
195 \def\xref@[#1]#2{\xref@@{#1}#2:\q@delim:\q@delim:\q@delim\q@delim}
196 % \end{macrocode}
197 % Right; now we abuse \TeX's argument parser to pick apart the reference
198 % label, which ought to have the form \syntax{<prefix>":"<suffix>}.
199 % \begin{macrocode}
200 \def\xref@@#1#2:#3:\q@delim#4\q@delim\q@delim{%
201 % \end{macrocode}
202 % So, |#1| is the optional command, or |\relax|. |#2| should be the
203 % prefix, and |#3| the suffix. However, if the string doesn't have any
204 % colons in, then |#3| will be |\q@delim|. This is easy to check for using
205 % |\ifx|.
206 % \begin{macrocode}
207 \def\@tempa{#2}\def\@tempb{#3}%
208 \ifx\@tempb\q@delim%
209 \PackageError{xref}{Bad ref syntax}%
210 {A reference name doesn't contain a `:'-delimited prefix. Did you %
211 mean to use plain \string\ref here?}%
212 \xref@fallback{#2}%
213 \else%
214 \expandafter\let\expandafter\@tempa\csname xref$#2\endcsname%
215 \ifx\@tempa\relax%
216 \PackageError{xref}{Unknown ref kind `#2'}%
217 {The ref name's prefix `#2' is unknown: either it's been mistyped %
218 or there's a missing \string\defxref somewhere.}%
219 \xref@fallback{#2:#3}%
220 \else \@tempa{#1}{#2:#3}%
221 \fi%
222 \fi%
223 }
224 % \end{macrocode}
225 % \end{macro}
226 %
227 % \begin{macro}{\toupper}
228 % That's the difficult stuff done. Uppercasing is a matter of picking out
229 % the first letter and passing it to \TeX's |\uppercase| primitive.
230 % \begin{macrocode}
231 \def\toupper#1{\toupper@#1}
232 \def\toupper@#1{\uppercase{#1}}
233 % \end{macrocode}
234 % \end{macro}
235 %
236 % \begin{macro}{\Xref}
237 % As promised, |\Xref| is very easy.
238 % \begin{macrocode}
239 \def\Xref{\xref[\toupper]}
240 % \end{macrocode}
241 % \end{macro}
242 %
243 % Now all that remains is to initialize the table of prefix strings.
244 % \begin{macrocode}
245 \defxref{ch}{chapter}
246 \defxref{app}{appendix}
247 \defxref{sec}{section}
248 \defxref{def}{definition}
249 \defxref{th}{theorem}
250 \defxref{lem}{lemma}
251 \defxref{prop}{proposition}
252 \defxref{cor}{corollary}
253 \defxref{fig}{figure}
254 \defxref{tab}{table}
255 \defxref{eq}{equation}
256 \defxref{i}{item}
257 \defxref{ex}{exercise}
258 % \end{macrocode}
259 % And we're done!
260 % \begin{macrocode}
261 %</package>
262 % \end{macrocode}
263 % \nopagebreak
264 %
265 % \hfill Mark Wooding, \today
266 %
267 % \Finale
268 %
269 \endinput