Debian Codegraph @

texmf-dist/tex/generic/mathlig/mathlig.tex

raw
1 %
2 % mathlig.tex
3 %
4 % (c) 2001 Jules Bean <[email protected]>
5 % Permission is granted to freely use, copy, and distribute
6 % without restriction. Permission is also granted to distribute
7 % modified versions and incorporate into your own macro packages
8 % if a brief acknowledgement is given.
9 %
10 % Version 1.0 11 May 2001
11
12 % Define 'mathmode' ligatures, as in
13 %
14 %\mathlig{->}{\rightarrow}
15 %\mathlig{<-}{\leftarrow}
16 %\mathlig{<->}{\leftrightarrow}
17 %
18 % Works even if they have common prefixes (takes the longest match,
19 % then backtracks).
20 %
21 % Can be used to create ligatures even if the second
22 % and subsequent characters have weird catcodes, but I don't recommend
23 % it.
24
25 \count255\catcode`@
26 \catcode`@=11
27 \chardef\mathlig@atcode\count255
28
29 % Let or def an 'active' version of a token. Thanks to Donald
30 % Arseneau and groups.google.com
31 \def\actively#1#2{\begingroup\uccode`\~=`#2\relax\uppercase{\endgroup#1~}}
32 % Eat the next token, and then pass control to mathlig@next@cmd
33 \def\mathlig@gobble{\afterassignment\mathlig@next@cmd\let\mathlig@next= }
34 % Used to delimit delimited arguments, and ifx tests for emptyness.
35 \def\mathlig@delim{\mathlig@delim}
36 % Some macros to help dealing with 'computed names' (csnames).
37 % Just like def, but first parameter is evaluated as a csname
38 \def\mathlig@defcs#1{\expandafter\def\csname#1\endcsname}
39 % A version of \let <normal cs><computed cs>
40 \def\mathlig@let@cs#1#2{\expandafter\let\expandafter#1\csname#2\endcsname}
41 % Maintain a 'list' of tokens as a macro which expands to them
42 \def\mathlig@appendcs#1#2{\expandafter\edef\csname#1\endcsname{\csname#1\endcsname#2}}
43
44 % The main externally visible macro.
45 % Defines '#1' as a 'ligature' to expand to #2
46 % First uses \mathlig@checklig to make sure that all initial segments of
47 % #1 as set up as ligatures.
48 % Then sets the ligature #1 to expand to #2.
49 \def\mathlig#1#2{\mathlig@checklig#1\mathlig@end\mathlig@defcs{mathlig@back@#1}{#2}\ignorespaces}
50
51 % All the @check macros should be idempotent: they set up the definitions
52 % only if it hasn't already been done.
53
54 %Check #1#2 can be a ligature:
55 % Check #1 is math-active.
56 % If not, save the mathcode, set the macros #1
57 %Now check the suffix tables with \checkrest
58 \def\mathlig@checklig#1#2\mathlig@end{%
59 \expandafter\ifx\csname mathlig@forw@#1\endcsname\relax
60 \expandafter\mathchardef\csname mathlig@back@#1\endcsname=\mathcode`#1%
61 \mathcode`#1"8000\actively\def#1{\csname mathlig@look@#1\endcsname}%
62 \mathlig@dolig#1\mathlig@delim
63 \fi
64 \mathlig@checksuffix#1#2\mathlig@end
65 }
66
67 %Two-part macro.
68 %Check suffix tables. If #2 is empty, no suffix!
69 %Otherwise, we make sure #2 is a valid suffix for #1,
70 %then recurse for #3 on #1#2
71 \def\mathlig@checksuffix#1#2\mathlig@end{%
72 \ifx\mathlig@delim#2\mathlig@delim\relax\else\mathlig@checksuffix@{#1}#2\mathlig@end\fi
73 }
74 \def\mathlig@checksuffix@#1#2#3\mathlig@end{%
75 \expandafter\ifx\csname mathlig@forw@#1#2\endcsname\relax\mathlig@dosuffix{#1}{#2}\fi
76 \mathlig@checksuffix{#1#2}#3\mathlig@end
77 }
78
79 % The do macros should not be called more than once for a given ligature.
80
81 % Add #2 to the list of valid suffixes for #1
82 % Then make the ligature for #1#2 one which only backtracks, for now
83 \def\mathlig@dosuffix#1#2{%
84 \mathlig@appendcs{mathlig@toks@#1}{#2}%
85 \mathlig@dolig{#1}{#2}\mathlig@delim
86 }
87
88 % Setup #1#2 so that ligatures beginning #1#2 work.
89 % Such ligatures will look at what's coming next (in case
90 % of a longer ligature) and, failing that,
91 % backtrack to just #1
92
93 \def\mathlig@dolig#1#2\mathlig@delim{%
94 %The look macro just \futurelets what's coming up and
95 %then passes control to forw
96 \mathlig@defcs{mathlig@look@#1#2}{%
97 \mathlig@let@cs\mathlig@next{mathlig@forw@#1#2}\futurelet\mathlig@next@tok\mathlig@next}%
98 %The forw macro uses chck to try all possible suffixes, passing control
99 %either to one of those, or to the back macro
100 \mathlig@defcs{mathlig@forw@#1#2}{%
101 \mathlig@let@cs\mathlig@next{mathlig@back@#1#2}%
102 \mathlig@let@cs\checker{mathlig@chck@#1#2}%
103 \mathlig@let@cs\mathligtoks{mathlig@toks@#1#2}%
104 \expandafter\ifx\expandafter\mathlig@delim\mathligtoks\mathlig@delim\relax\else
105 \expandafter\checker\mathligtoks\mathlig@delim\fi
106 \mathlig@next
107 }%
108 %The toks macro just stores the suffixes
109 \mathlig@defcs{mathlig@toks@#1#2}{}%
110 %The chk macro goes through the suffixes one by one
111 %tail recursing until it runs out, or finds one.
112 \mathlig@defcs{mathlig@chck@#1#2}##1##2\mathlig@delim{%
113 %\message{Lig so far '#1#2', checking for '##1'}%
114 \ifx\mathlig@next@tok##1%
115 \mathlig@let@cs\mathlig@next@cmd{mathlig@look@#1#2##1}\let\mathlig@next\mathlig@gobble
116 \fi
117 \ifx\mathlig@delim##2\mathlig@delim\relax\else
118 \csname mathlig@chck@#1#2\endcsname##2\mathlig@delim
119 \fi
120 }%
121 %
122 % The back macro, defined only if this is a ligature of at least
123 % two characters, is a default fallback: go back to the previous char.
124 % (If this is a ligature of only one character, the mathcode will have
125 % been saved and set as the fallback elsewhere)
126 \ifx\mathlig@delim#2\mathlig@delim\else
127 \mathlig@defcs{mathlig@back@#1#2}{\csname mathlig@back@#1\endcsname #2}%
128 \fi
129 }%
130
131 \catcode`@\mathlig@atcode