mdwref.dtx: Rearrange the machinery for later extension.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 22 Jul 2022 10:34:48 +0000 (11:34 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 22 Jul 2022 10:34:48 +0000 (11:34 +0100)
Rather than having `\xref$SUFFIX' just store a string, make it be a
macro which actually does the typesetting.  For simple strings, this
indirects through `\formatxref' as before, but that's now a bit better
factored.

mdwref.dtx

index fe386b0..d7d24e0 100644 (file)
 %\newcommand{\Xref}[1]{\xref[\toupper]{#1}}
 % \end{listing}
 %
-% \DescribeMacro\formatxref
-% The reference itself is typeset by calling
-% \syntax{"\\formatxref{"<mangle>"}{"<string>"}{"<label>"}"}, which can do as
-% it pleases: the \<mangle> token is from the |\xref| invocation; the
-% \<string> is category of thing being referred to (as established by
-% |\defxref| below); and \<label> is the label, again from |\xref| .  The
-% default behaviour is to print
-% \syntax{<mangle>"{"<string>"}~\\ref{"<label>"}"}, but this can be
-% overridden.
-% (Not quite true: in fact, the default does something better if
-% \package{hyperref} is detected, but the idea is basically the same.)
-%
 % All that remains is to define the strings to be typeset for various kinds
 % of labels.
 % \DescribeMacro\defxref
 % \]]
 % \end{grammar}
 % The \<prefix> is what you put on the front of your labels; the \<string> is
-% the string to be typeset by |\xref|.
+% the string to be typeset by |\xref|.  Such references are typeset using
+% |\formatxref|, described below.
+%
+% \DescribeMacro\formatxref
+% Simple references are typeset by calling
+% \syntax{"\\formatxref{"<mangle>"}{"<string>"}{"<label>"}"}, which can do as
+% it pleases: the \<mangle> token is from the |\xref| invocation; the
+% \<string> is category of thing being referred to (as established by
+% |\defxref| below); and \<label> is the label, again from |\xref| .  The
+% default behaviour is to print
+% \syntax{<mangle>"{"<string>"}~\\ref{"<label>"}"}, but this can be
+% overridden.
+% (Not quite true: in fact, the default does something better if
+% \package{hyperref} is detected, but the idea is basically the same.)
 %
 % A number of useful prefixes are already defined, following my usual
 % preferences; they're shown in \xref{tab:defs}.
 %
 % \begin{macro}{\defxref}
 % Defining prefixes is easy.  We store the text for each prefix in a macro
-% called \syntax{"\\xref$"<prefix>}.
+% called \syntax{"\\xref$"<prefix>}, which takes two arguments: a \<mangle>
+% token (or |\relax|), and the reference name.
 %    \begin{macrocode}
 \def\defxref#1#2{%
-  \expandafter\def\csname xref$#1\endcsname{#2}%
+  {\expandafter\def\csname xref$#1\endcsname##1##2{\formatxref{##1}{#2}{##2}}}
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}{\xrefdispatch}
+%    \begin{macrocode}
+\def\xref@hyper#1{\hyperref[#1]}
+\def\xrefdispatch#1{%
+  \ifx\hyperref\@@undefined \def\@tempa{#1\@gobble\ref}%
+  \else \def\@tempa{#1\xref@hyper{\ref*}}%
+  \fi \@tempa%
 }
 %    \end{macrocode}
 % \end{macro}
 % \begin{macro}{\formatxref}
 % Output a cross-reference in the right way.
 %    \begin{macrocode}
-\def\formatxref#1#2#3{%
-  \ifx\hyperref\@@undefined #1{#2}~\ref{#3}%
-  \else \hyperref[#3]{#1{#2}~\ref*{#3}}\fi%
-}
+\def\formatxref{\xrefdispatch\xref@formatsimple}
+\def\xref@formatsimple#1#2#3#4#5{#1{#5}{#3{#4}~#2{#5}}}
 \def\xref@fallback#1{\formatxref\relax{?\texttt{#1}}{#1}}
 %    \end{macrocode}
 % \end{macro}
         {The ref name's prefix `#2' is unknown: either it's been mistyped %
          or there's a missing \string\defxref somewhere.}%
      \xref@fallback{#2:#3}%
-    \else%
-      \toks@\expandafter{\@tempa}%
-      \edef\next@##1{##1{\the\toks@}}%
-      \next@{\formatxref{#1}}{#2:#3}%
+    \else \@tempa{#1}{#2:#3}%
     \fi%
   \fi%
 }