1 % \begin{meta-comment} <general public licence>
3 %% at package -- support for `@' commands'
4 %% Copyright (c) 1995, 1996, 2002, 2020 Mark Wooding
6 %% This program is free software; you can redistribute it and/or modify
7 %% it under the terms of the GNU General Public License as published by
8 %% the Free Software Foundation; either version 2 of the License, or
9 %% (at your option) any later version.
11 %% This program is distributed in the hope that it will be useful,
12 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
13 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 %% GNU General Public License for more details.
16 %% You should have received a copy of the GNU General Public License
17 %% along with this program; if not, write to the Free Software
18 %% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 % \begin{meta-comment} <Package preamble>
23 %<+package>\NeedsTeXFormat{LaTeX2e}
24 %<+package>\ProvidesPackage{at}
25 %<+package> [2020/09/06 1.14.0 @-command support]
30 %% {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
31 %% 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
32 %% Digits \0\1\2\3\4\5\6\7\8\9
33 %% Exclamation \! Double quote \" Hash (number) \#
34 %% Dollar \$ Percent \% Ampersand \&
35 %% Acute accent \' Left paren \( Right paren \)
36 %% Asterisk \* Plus \+ Comma \,
37 %% Minus \- Point \. Solidus \/
38 %% Colon \: Semicolon \; Less than \<
39 %% Equals \= Greater than \> Question mark \?
40 %% Commercial at \@ Left bracket \[ Backslash \\
41 %% Right bracket \] Circumflex \^ Underscore \_
42 %% Grave accent \` Left brace \{ Vertical bar \|
43 %% Right brace \} Tilde \~}
46 % \begin{meta-comment} <driver>
52 \def\athyphen{@\mbox{-}}
55 \atdef at{\package{at}}
64 % \section{User guide}
66 % The @at\ package is an attempt to remove a lot of tedious typing that
67 % ends up in \LaTeX\ documents, by expanding the number of short command
68 % names available. The new command names begin with the `|@|' character,
69 % rather than the conventional `|\|', so you can tell them apart.
71 % The package provides some general commands for defining @-commands, and
72 % then uses them to define some fairly simple ones which will be useful to
75 % The rules for @-command names aren't terribly complex:
77 % \item If the first character of the name is a letter, then the command name
78 % consists of all characters up to, but not including, the first
79 % nonletter. Spaces following the command name are ignored.
80 % \item If the first character of the name is a backslash, then the @-command
81 % name consists of the control sequence introduced by the backslash.
82 % \item Otherwise, the command name consists only of that first character.
83 % Spaces following the name are not ignored, unless that character
84 % was itself a space character.
87 % Usually, digits are not considered to be letters. However, the
88 % \package{at} package will consider digits to be letters if you give it the
89 % \textsf{digits} option in the |\usepackage| command. (Note that this
90 % only affects the \package{at} package; it won't change the characters
91 % allowed in normal command names.)
93 % \DescribeMacro{\atallowdigits}
94 % \DescribeMacro{\atdisallowdigits}
95 % You can enable and disable digits being considered as letters dynamically.
96 % The |\atallowdigits| command allows digits to be used as letters;
97 % |\atdisallowdigits| prevents this. Both declarations follow \LaTeX's
98 % usual scoping rules. Both of these commands have corresponding
99 % environments with the same names (without the leading `|\|', obviously).
101 % \subsection{Defining \athyphen commands}
103 % \DescribeMacro{\newatcommand}
104 % \DescribeMacro{\renewatcommand}
105 % The |\newatcommand| command will define a new @-command using a syntax
106 % similar to |\newcommand|. For example, you could define
108 %\newatcommand c[1]{\chapter{#1}}
110 % to make @.{"@c{"<name>"}"} equivalent to @.{"\\chapter{"<name>"}"}.
112 % A |\renewatcommand| is also provided to redefine existing commands, should
115 % \DescribeMacro{\atdef}
116 % For \TeX\ hackers, the |\atdef| command defines @-commands using a syntax
117 % similar to \TeX's built-in |\def|.
119 % As an example, the following command makes @.{"@/"<text>"/"} write its
120 % argument \<text> in italics:
122 %\atdef/#1/{\textit{#1}}
124 % The real implementation of the |@/|\dots|/| command is a bit more
125 % complex, and is given in the next section.
127 % You can use all of \TeX's features for defining the syntax of your
128 % command. (See chapter~20 of @/The \TeX book/ for more details.)
130 % \DescribeMacro{\atlet}
131 % Since |\atdef| is provided to behave similarly to |\def|, @at\ provides
132 % |\atlet| which works similarly to |\let|. For example you can say
136 % to allow the short |@!| to behave exactly like |\index|.
138 % Note that all commands defined using these commands are robust even if you
139 % use fragile commands in their definitions. Unless you start doing very
140 % strange things, @-commands never need |\protect|ing.
142 % \subsection{Predefined \athyphen commands}
144 % A small number of hopefully useful commands are provided by default.
145 % These are described in the table below:
147 % \bigskip \begin{center} \begin{tabular}{lp{3in}} \hline
148 % \bf Command & \bf Meaning \\ \hline
149 % @.{"@@"} & Typesets an `@@' character. \\
150 % @.{"@/"<text>"/"} & In text (LR or paragraph) mode, typesets its
151 % argument emphasised. In maths mode, it
152 % always chooses italics. \\
153 % @.{"@*"<text>"*"} & Typesets its argument \<text> in bold. \\
154 % @.{"@i{"<text>"}"} & Equivalent to `@.{"\\index{"<text>"}"}'. \\
155 % @.{"@I{"<text>"}"} & As for |@i|, but also writes its argument
156 % to the document. \\ \hline
157 % \end{tabular} \end{center} \bigskip
159 % Package writers should not rely on any predefined @-commands -- they're
160 % provided for users, and users should be able to redefine them without
161 % fear of messing anything up. (This includes the `standard' commands
162 % provided by the @at\ package, by the way. They're provided in the vague
163 % hope that they might be useful, and as examples.)
167 % \section{Implementation}
173 % \subsection{Options handling}
175 % We need a switch to say whether digits should be allowed. Since this
176 % is a user thing, I'll avoid |\newif| and just define the thing by hand.
179 \def\atallowdigits{\let\ifat@digits\iftrue}
180 \def\atdisallowdigits{\let\ifat@digits\iffalse}
183 % Now define the options.
186 \DeclareOption{digits}{\atallowdigits}
187 \DeclareOption{nodigits}{\atdisallowdigits}
188 \ExecuteOptions{nodigits}
192 % \subsection{How the commands work}
194 % Obviously we make the `@@' character active. It inspects the next
195 % character (or argument, actually -- it can be enclosed in braces for
196 % longer commands, although this is a bit futile), and builds the command
199 % The |\at| command is equivalent to the active `@@' character always.
202 % \subsection{Converting command names}
204 % We need to be able to read an @-command name, and convert it to a normal
205 % \TeX\ control sequence. First, we declare some control sequences for
206 % braces, which we need later.
220 % I'll set up some helper routines now, to help me read the command
221 % names. The way this works is that we |\futurelet| the token into
222 % |\@let@token|. These routines will then sort out what to do next.
224 % \begin{macro}{\at@test}
226 % Given an |\if|\dots\ test, does its first or second argument.
229 \def\at@test#1\then{%
230 #1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi%
236 % \begin{macro}{\at@ifcat}
238 % Checks the category code of the current character. If it matches the
239 % argument, it does its second argument, otherwise it does the third.
242 \def\at@ifcat#1{\at@test\ifcat#1\noexpand\@let@token\then}
247 % \begin{macro}{\at@ifletter}
249 % This routine tests the token to see if it's a letter, and if so adds
250 % it to the token list and does the first argument; otherwise it does the
251 % second argument. It accepts digits as letters if the switch is turned
254 % There's some fun later, so I'll describe this slowly. First, we compare
255 % the category code to a letter, and if we have a match, we know we're done;
256 % we need to pick up the letter as an argument. If the catcode is `other',
257 % we must compare with numbers to see if it's in range.
260 \def\at@ifletter#1#2{%
262 {\at@ifletter@ii{#1}}%
264 {\at@ifletter@i{#1}{#2}}%
270 % Right. It's `other' (so it's safe to handle as a macro argument) and we
271 % need to know if it's a digit. This is a little tricky: I use |\if| to
272 % compare two characters. The first character is~`1' or~`0' depending on the
273 % `digit' switch; the second is~`1' or~`x' depending on whether it's actually
274 % a digit. They'll only match if everything's worked out OK.
277 \def\at@ifletter@i#1#2#3{%
279 \ifat@digits1\else0\fi%
280 \ifnum`#3<`0x\else\ifnum`#3>`9x\else1\fi\fi%
282 {\at@ifletter@ii{#1}{#3}}%
287 % Right; we have the character, so add it to the list and carry on.
290 \def\at@ifletter@ii#1#2{\toks@\expandafter{\the\toks@#2}#1}
295 % Now we define the command name reading routines. We have @/almost/ the
296 % same behaviour as \TeX, although we can't support `|%|' characters for
297 % reasons to do with \TeX's tokenising algorithm.
299 % \begin{macro}{\at@read@name}
301 % The routine which actually reads the command name works as follows:
303 % \item Have a peek at the next character. If it's a left or right brace,
304 % then use the appropriate character.
305 % \item If the character is not a letter, just use the character (or whole
307 % \item Finally, if it's a letter, keep reading letters until we find one
311 % First, we do some setting up and read the first character
314 \def\at@read@name#1{%
317 \futurelet\@let@token\at@rn@i%
321 % Next, sort out what to do, based on the category code.
325 \def\@tempa{\afterassignment\at@rn@iv\let\@let@token= }%
327 {\futurelet\@let@token\at@rn@iii}%
329 {\toks@\expandafter{\at@lb}\@tempa}%
331 {\toks@\expandafter{\at@rb}\@tempa}%
341 % Most types of tokens can be fiddled using |\string|.
345 \toks@\expandafter{\string#1}%
350 % We've found a letter, so we should check for another one.
355 {\futurelet\@let@token\at@rn@iii}%
356 {\@ifnextchar.\at@rn@iv\at@rn@iv}%
360 % Finally, we need to pass the real string, as an argument, to the
361 % macro. We make |\@let@token| relax, since it might be something which will
362 % upset \TeX\ later, e.g., a |#| character.
366 \let\@let@token\relax%
367 \expandafter\at@next\csname at.\the\toks@\endcsname%
373 % \begin{macro}{\at@cmdname}
375 % Given a control sequence, work out which @-command it came from.
378 \def\at@cmdname#1{\expandafter\at@cmdname@i\string#1\@@foo}
381 % Now extract the trailing bits.
384 \def\at@cmdname@i#1.#2\@@foo{#2}
389 % \begin{macro}{\at@decode}
391 % The |\at@decode| macro takes an extracted @-command name, and tries to
392 % execute the correct control sequence derived from it.
396 \at@test\ifx#1\relax\then{%
397 \PackageError{at}{Unknown @-command `@\at@cmdname#1'}{%
398 The @-command you typed wasn't recognised, so I've ignored it.
408 % \begin{macro}{\@at}
410 % We'd like a measure of compatibility with @p{amsmath}. The @-commands
411 % provided by @p{amsmath} work only in maths mode, so this gives us a way of
412 % distinguishing. If the control sequence |\Iat| is defined, and we're in
413 % maths mode, we'll call that instead of doing our own thing.
417 \def\@tempa{\at@read@name\at@decode}%
418 \ifmmode\ifx\Iat\not@@defined\else%
428 % \subsection{Defining new commands}
430 % \begin{macro}{\at@buildcmd}
432 % First, we define a command to build these other commands:
435 \def\at@buildcmd#1#2{%
436 \expandafter\def\csname\expandafter
437 \@gobble\string#1@decode\endcsname##1{#2##1}%
439 \noexpand\at@read@name%
440 \expandafter\noexpand%
441 \csname\expandafter\@gobble\string#1@decode\endcsname%
448 % \begin{macro}{\newatcommand}
449 % \begin{macro}{\renewatcommand}
450 % \begin{macro}{\provideatcommand}
451 % \begin{macro}{\atdef}
452 % \begin{macro}{\atshow}
454 % Now we define the various operations on @-commands.
457 \at@buildcmd\newatcommand\newcommand
458 \at@buildcmd\renewatcommand\renewcommand
459 \at@buildcmd\provideatcommand\providecommand
460 \at@buildcmd\atdef\def
461 \at@buildcmd\atshow\show
470 % \begin{macro}{\atlet}
472 % |\atlet| is rather harder than the others, because we want to allow people
473 % to say things like @.{"\\atlet"<name>"=@"<name>}. The following hacking
474 % does the trick. I'm trying very hard to duplicate |\let|'s behaviour with
475 % respect to space tokens here, to avoid any surprises, although there
476 % probably will be some differences. In particular, |\afterassignment|
477 % won't work in any sensible way.
479 % First, we read the name of the @-command we're defining. We also open
480 % a group, to stop messing other people up, and make `@@' into an `other'
481 % token, so that it doesn't irritatingly look like its meaning as a control
488 \at@read@name\atlet@i%
492 % Put the name into a scratch macro for later use. Now see if there's an
493 % equals sign up ahead. If not, this will gobble any spaces in between the
494 % @-command name and the argument.
499 \@ifnextchar=\atlet@ii{\atlet@ii=}%
503 % Now we gobble the equals sign (whatever catcode it is), and peek at the
504 % next token up ahead using |\let| with no following space.
507 \def\atlet@ii#1{\afterassignment\atlet@iii\global\let\at@gnext=}
510 % The control sequence |\at@gnext| is now |\let| to be whatever we want the
511 % @-command to be, unless it's picked up an `@@' sign. If it has, we've
512 % eaten the |@| token, so just read the name and pass it on. Otherwise,
513 % we can |\let| the @-command directly to |\at@gnext|. There's some
514 % nastiness here to make |\the\toks@| expand before we close the group and
515 % restore its previous definition.
519 \if @\noexpand\at@gnext%
520 \expandafter\at@read@name\expandafter\atlet@iv%
522 \expandafter\endgroup%
523 \expandafter\let\at@temp= \at@gnext%
528 % We've read the source @-command name, so just copy the definitions over.
532 \expandafter\endgroup%
533 \expandafter\let\at@temp=#1%
540 % \subsection{Robustness of \athyphen commands}
542 % We want all @-commands to be robust. We could leave them all being
543 % fragile, although making robust @-commands would then be almost impossible.
544 % There are two problems which we must face:
548 % \item The `|\@at|' command which scans the @-command name is (very)
549 % fragile. I could have used |\DeclareRobustCommand| for it (and in
550 % fact I did in an earlier version), but that doesn't help the other
553 % \item The `name' of the @-command may contain active characters or control
554 % sequences, which will be expanded at the wrong time unless we do
555 % something about it now.
559 % We must also be careful not to introduce extra space characters into any
560 % files written, because spaces are significant in @-commands. Finally,
561 % we have a minor problem in that most auxiliary files are read in with
562 % the `@@' character set to be a letter.
566 % Following the example of \LaTeX's `short' command handling, we'll define
567 % |\at| to decide what to do depending on what |\protect| looks like. If
568 % we're typesetting, we just call |\@at| (above) and expect it to cope.
569 % Otherwise we call |\at@protect|, which scoops up the |\fi| and the |\@at|,
570 % and inserts other magic.
573 \def\at{\ifx\protect\@typeset@protect\else\at@protect\fi\@at}
578 % \begin{macro}{\at@protect}
580 % Since we gobbled the |\fi| from the above, we must put that back. We then
581 % need to do things which are more complicated. If |\protect| is behaving
582 % like |\string|, then we do one sort of protection. Otherwise, we assume
583 % that |\protect| is being like |\noexpand|.
586 \def\at@protect\fi#1{%
589 \expandafter\at@protect@string%
591 \expandafter\at@protect@noexpand%
598 % \begin{macro}{\at@protect@string}
600 % When |\protect| is |\string|, we don't need to be able to recover the
601 % original text particularly accurately -- it's for the user to look at.
602 % Therefore, we just output a $|@|_{11}$ and use |\string| on the next
603 % token. This must be sufficient, since we only allow multi-token command
604 % names if the first token is a letter (code~11).
607 \def\at@protect@string{@\string}
612 % \begin{macro}{\at@protect@noexpand}
614 % This is a little more complex, since we're still expecting to be executed
615 % properly at some stage. However, there's a cheeky dodge we can employ
616 % since the |\at| command is thoroughly robustified (or at least it will be
617 % by the time we've finished this). All |\@unexpandable@protect| does
618 % is confer repeated robustness on a fragile command. Since our command
619 % is robust, we don't need this and we can get away with just using a
620 % single |\noexpand|, both for the |\@at@| command and the following token
621 % (which we must robustify, because no-one else can do it for us -- if
622 % anyone tries, they end up using the |@\protect| command which is rather
625 % I'll give the definition, and then examine how this expands in various
629 \def\at@protect@noexpand{\noexpand\@at@ @\noexpand}
633 % A few points, before we go into the main examination of the protection.
634 % I've inserted a $|@|_{11}$ token, which is gobbled by |\@at@| when the
635 % thing is finally expanded fully. This prevents following space tokens
636 % in an |\input| file from being swallowed because they follow a control
637 % sequence. (I can't use the normal $|@|_{13}$ token, because when files
638 % like the |.aux| file are read in, |@| is given code~11 by
641 % \setbox0\hbox{|@at@|}
642 % Now for a description of why this works. When |\at| is expanded, it works
643 % out that |\protect| is either |\noexpand| or |\@unexpandable@protect|, and
644 % becomes |\at@protect@noexpand|. Because of the |\noexpand| tokens, this
645 % stops being expanded once it reaches $\fbox{\box0}\,|@|_{11}\,x$ (where
646 % $x$ is the token immediately following the $|@|_{13}$ character). If this
647 % is expanded again, for example in another |\edef|, or in a |\write| or a
648 % |\mark|, the |\@at@| wakes up, gobbles the following |@| (whatever catcode
649 % it is -- there may be intervening |\write| and |\input| commands) and
650 % becomes |\at|, and the whole thing can start over again.
655 % \subsection{Enabling and disabling @-commands}
657 % \begin{macro}{\aton}
659 % We define the |\aton| command to enable all of our magic. We store
660 % the old catcode in the |\atoff| command, make `@@' active, and make it
665 \ifnum\catcode`\@=\active\else%
666 \edef\atoff{\catcode`\noexpand\@\the\catcode`\@}%
669 \lowercase{\let~\at}%
676 % \begin{macro}{\atoff}
678 % The |\atoff| command makes `@@' do the stuff it's meant to. We remember
679 % the old catcode and revert to it. This is largely unnecessary.
682 \def\atoff{\catcode`\@12}
687 % \begin{macro}{\makeatother}
689 % Now we make our active `@@' the default outside of package files.
692 \let\makeatother\aton
697 % And we must make sure that the user can use all of our nice commands.
698 % Once the document starts, we allow @-commands.
701 \AtBeginDocument{\aton}
704 % \begin{macro}{\dospecials}
705 % \begin{macro}{\@sanitize}
707 % We must add the `@@' character to the various specials lists.
710 \expandafter\def\expandafter\dospecials\expandafter{\dospecials\do\@}
711 \expandafter\def\expandafter\@sanitize\expandafter{%
712 \@sanitize\@makeother\@}
718 % \subsection{Default \athyphen commands}
720 % We define some trivial examples to get the user going.
723 \expandafter\chardef\csname at.@\endcsname=`\@
724 \atdef*#1*{\ifmmode\mathbf{#1}\else\textbf{#1}\fi}
725 \atdef/#1/{\ifmmode\mathit{#1}\else\emph{#1}\fi}
727 \atdef I#1{#1\index{#1}}
731 % \hfill Mark Wooding, \today