| 1 |
|
| 2 |
%%# |
| 3 |
%%# Author: |
| 4 |
%%# |
| 5 |
%%# M. V\"ath [email protected] |
| 6 |
%%# |
| 7 |
%%# The author thanks |
| 8 |
%%# David Kastrup <[email protected]> |
| 9 |
%%# |
| 10 |
|
| 11 |
%%# The package may be distributed and/or modified under the conditions of |
| 12 |
%%# the LaTeX Project Public License (version 1.3c or later). |
| 13 |
|
| 14 |
%%############################################################################# |
| 15 |
%%# |
| 16 |
%%# The TeX programmer's toolbox; enhanced version. |
| 17 |
%%# This (La)TeX package provides some macros which are convenient for |
| 18 |
%%# writing indices, glossaries, or other macros. |
| 19 |
%%# It contains macros which support |
| 20 |
%%# |
| 21 |
%%# - implicit macros - a useful supplement to \index or varindex for |
| 22 |
%%# writing glossaries or indices |
| 23 |
%%# - fancy optional arguments |
| 24 |
%%# - loops over tokenlists and itemlists |
| 25 |
%%# - searching, splitting, and replacing |
| 26 |
%%# - controlled expansion |
| 27 |
%%# - redefinition of macros |
| 28 |
%%# - concatenated macro names |
| 29 |
%%# |
| 30 |
%%############################################################################# |
| 31 |
|
| 32 |
%%# |
| 33 |
%%# This package was tested with plain TeX, LaTeX 2.09, and LaTeX 2e, |
| 34 |
%%# and it should actually run with all TeX formats. |
| 35 |
%%# |
| 36 |
|
| 37 |
|
| 38 |
%%# To use toolbox, you have to put `toolbox.sty' in a path where TeX looks |
| 39 |
%%# for its input files. The TeX documents using toolbox need the |
| 40 |
%%# following modifications in their header: |
| 41 |
%%# |
| 42 |
%%# a) If you use LaTeX2.09, use toolbox as a style option, e.g. |
| 43 |
%%# \documentstyle[toolbox]{article} |
| 44 |
%%# or |
| 45 |
%%# \documentstyle[toolbox,12pt]{article} |
| 46 |
%%# b) If you use LaTeX2e, put in the preamble the command |
| 47 |
%%# \usepackage{toolbox} |
| 48 |
%%# c) If you use some other (non-LaTeX) format, you will probably have to |
| 49 |
%%# insert a line like |
| 50 |
%%# \catcode`\@=11\input toolbox.sty\catcode`\@=12\relax |
| 51 |
%%# |
| 52 |
%%# The only LaTeX-specific commands used in `toolbox.sty' are: |
| 53 |
%%# |
| 54 |
%%# \newcommand (only in the form \newcommand{\command}{} to ensure that |
| 55 |
%%# \command was not defined before) |
| 56 |
%%# \ProvidesPackage |
| 57 |
%%# \typeout |
| 58 |
%%# |
| 59 |
%%# The above commands are used only if they are defined. |
| 60 |
%%# |
| 61 |
|
| 62 |
%%# |
| 63 |
%%# Description of the provided macros: |
| 64 |
%%# |
| 65 |
|
| 66 |
%%# General remark: Many macros could appear in several sections. |
| 67 |
%%# For example, \toolboxMakeDef and \toolboxSourround might be considered |
| 68 |
%%# as macros which support redefinitions of macros. However, we put them |
| 69 |
%%# in different sections which perhaps explain better their nature. |
| 70 |
|
| 71 |
%%# |
| 72 |
%%# Implicit macro definitions |
| 73 |
%%# -------------------------- |
| 74 |
|
| 75 |
%%# What we mean by implicit macro definitions is probably best explained by |
| 76 |
%%# the following examples which show the intended usage: |
| 77 |
%%# |
| 78 |
%%# (In the following examples, we always refer to the \index command. Note |
| 79 |
%%# that it may be more convenient to use for indices the varindex package in |
| 80 |
%%# addition - the documentation of varindex (release 2.3 or newer) gives |
| 81 |
%%# additional hints and examples how these two (essentially independent) |
| 82 |
%%# approaches can be combined in practice). |
| 83 |
%%# |
| 84 |
%%# Assume that you want to write an index for a book which has rather |
| 85 |
%%# long and complicated \index entries. The first idea that one might have in |
| 86 |
%%# this connection is to put the various \index commands at the beginning of |
| 87 |
%%# the document into several macros (one for each \index entry), and to use |
| 88 |
%%# just these macros in the main text. For example, one might want to write |
| 89 |
%%# near the beginning of the document commands like |
| 90 |
%%# \newcommand{\Start}{\index{finish or end}} |
| 91 |
%%# \newcommand{\End}{\index{finish or end}} |
| 92 |
%%# and then to use in the main text \Start and \End whenever a reference |
| 93 |
%%# in the corresponding index to the current place is desired. |
| 94 |
%%# However, this has two major disadvantages: |
| 95 |
%%# 1. Unless you are very disciplinary with your macro names, it is easy to |
| 96 |
%%# forget that \End writes an index entry. So the \End in the main text |
| 97 |
%%# might be very confusing. |
| 98 |
%%# 2. You cannot choose short and intuitive macro names for common phrases, |
| 99 |
%%# because they are usually already reserved by TeX, LaTeX, or some |
| 100 |
%%# packages. |
| 101 |
%%# |
| 102 |
%%# To avoid these problems, one may be very disciplinary and call the |
| 103 |
%%# involved macros systematically e.g. \GlossaryStart \GlossaryEnd etc. |
| 104 |
%%# However, this produces terrible long and unreadable macro names in the |
| 105 |
%%# main text. |
| 106 |
%%# |
| 107 |
%%# The implicit macro definitions of "toolbox" provide a more convenient |
| 108 |
%%# solution. The idea is that you do not use the corresponding macros directly |
| 109 |
%%# but only implicitly by a call of other macros where your "macro name" is |
| 110 |
%%# just an argument. |
| 111 |
%%# Moreover, "toolbox" assists you in writing the corresponding definitions. |
| 112 |
%%# For example, if you know that you want a set of macros which all expand |
| 113 |
%%# into something of the form \index{...}, you can give a ``mask'' which |
| 114 |
%%# contains this form, and you only have to fill in the changing content |
| 115 |
%%# (similarly as for usual TeX macros with arguments, but the level of |
| 116 |
%%# abstraction is one step higher). |
| 117 |
%%# For the above task, you might use the command: |
| 118 |
%%# |
| 119 |
%%# \toolboxMakeDef{Glossary}{\index{#1}} |
| 120 |
%%# |
| 121 |
%%# The argument `Glossary' serves to distinguish independent definitions |
| 122 |
%%# (this will become clear later). Its effect visible now is that it |
| 123 |
%%# determines the name of the following macros which you can use after |
| 124 |
%%# the above call: |
| 125 |
%%# |
| 126 |
%%# \NewGlossary{start-1}{start} |
| 127 |
%%# \NewGlossary{start-2}{start or beginning} |
| 128 |
%%# \NewGlossary{end}{finish or end} |
| 129 |
%%# |
| 130 |
%%# These command are now similar to the \newcommand definitions explained |
| 131 |
%%# above. However, there is no name collision with the TeX-internal command |
| 132 |
%%# \end. Of course, this means that you cannot just write \end in the main |
| 133 |
%%# text to get the desired index entry. Instead, you have to write the more |
| 134 |
%%# intuitive commands |
| 135 |
%%# |
| 136 |
%%# \Glossary{start-1} |
| 137 |
%%# \Glossary{start-2} |
| 138 |
%%# \Glossary{end} |
| 139 |
%%# |
| 140 |
%%# (again, the name \Glossary stems from our first call of \toolboxMakeDef). |
| 141 |
%%# Note that e.g. \Glossary{start-1} expands not only to `start' but |
| 142 |
%%# actually to \index{start} (because of our first call of \toolboxMakeDef). |
| 143 |
%%# Note also that you can use symbols like - or numbers which are usually |
| 144 |
%%# not allowed in TeX macro names. |
| 145 |
%%# |
| 146 |
%%# Of course, similarly as for \newcommand, you can also do other things with |
| 147 |
%%# the macros. For example, |
| 148 |
%%# |
| 149 |
%%# \LetGlossary\tempname{end} |
| 150 |
%%# \NewGlossary*{finish}\tempname |
| 151 |
%%# |
| 152 |
%%# will first define \tempname to expand to the same text as \Glossary{end}, |
| 153 |
%%# and then defines a new entry \Glossary{finish} to expand to the same text |
| 154 |
%%# as \tempname. Hence, the above new lines make the calls \Glossary{end} and |
| 155 |
%%# \Glossary{finish} equivalent. |
| 156 |
%%# |
| 157 |
%%# At the end of your list of \NewGlossary commands, you might want to put |
| 158 |
%%# |
| 159 |
%%# \toolboxFreeDef*{Glossary} |
| 160 |
%%# |
| 161 |
%%# The purpose of this command is that \NewGlossary cannot be used anymore |
| 162 |
%%# (unless, of course, you define it again). So you cannot unintentionally |
| 163 |
%%# add new entries to your glossary list (but you still can use \Glossary{...} |
| 164 |
%%# to reference to the already produced entries). |
| 165 |
%%# Moreover, the above command frees some memory which was needed for |
| 166 |
%%# \NewGlossary to work. |
| 167 |
%%# |
| 168 |
%%# If you additionally want to free the memory used by \Glossary, you can use |
| 169 |
%%# |
| 170 |
%%# \toolboxFreeDef{Glossary} |
| 171 |
%%# |
| 172 |
%%# (without the `*'). This may be necessary, if you want to call again e.g. |
| 173 |
%%# |
| 174 |
%%# \toolboxMakeDef{Glossary}{\emph{#1}\index{#1}} |
| 175 |
%%# |
| 176 |
%%# (if you have not freed the memory for \Glossary before this repeated call, |
| 177 |
%%# TeX will complain that \Glossary is already defined). |
| 178 |
%%# |
| 179 |
%%# Of course, it is possible to call \toolboxMakeDef with several different |
| 180 |
%%# names, for example, for \Glossary, \SymbolList etc. Another application |
| 181 |
%%# might be to use a different command to mark e.g.\ the main occurrence of |
| 182 |
%%# some index entry or to output additionally the entry into the running text. |
| 183 |
%%# We do this in the following example which simultaneously |
| 184 |
%%# demonstrates that the names can also be constructed in another way: |
| 185 |
%%# |
| 186 |
%%# \toolboxMakeDef[Ind]{}{\index{#1}} |
| 187 |
%%# \toolboxMakeDef[Ind]{Main}{\index{#1|textbf}} |
| 188 |
%%# \toolboxMakeDef{OutInd}{#1} |
| 189 |
%%# |
| 190 |
%%# \IndNew{A}{A is a letter} |
| 191 |
%%# \IndNewMain{A}{A is a letter} |
| 192 |
%%# \NewOutInd{A}{\textbf{The letter A}\Ind{A}} |
| 193 |
%%# |
| 194 |
%%# After the above commands, you can use \Ind{A}, \IndMain{A}, and |
| 195 |
%%# \OutInd{A} to produce the corresponding \index entry, the "main" \index |
| 196 |
%%# entry (with a boldface page number), and the text \textbf{The letter A} |
| 197 |
%%# with an additional entry into the index, respectively. |
| 198 |
%%# Of course, it might usually be more convenient to define the \Ind and |
| 199 |
%%# \IndMain" entries simultaneously, e.g. as follows |
| 200 |
%%# |
| 201 |
%%# \toolboxMakeDef[Ind]{}{#1} |
| 202 |
%%# \toolboxMakeDef[Ind]{Main}{#1} |
| 203 |
%%# \newcommand{\NewStandardInd}[2]{% |
| 204 |
%%# \IndNew{#1}{\index{#2}}% |
| 205 |
%%# \IndNewMain{#1}{\index{#2|textbf}}} |
| 206 |
%%# |
| 207 |
%%# \NewStandardInd{A}{A is a letter} |
| 208 |
%%# |
| 209 |
%%# This approach has the additional advantage that you can define exceptional |
| 210 |
%%# cases "by hand" (e.g. if you want that for certain "main" index |
| 211 |
%%# entries the page number is printed with "\textsl" instead of "\textbf"). |
| 212 |
|
| 213 |
%%# Since the motivation for implicit definitions now is hopefully clear, |
| 214 |
%%# let us now describe in detail which commands are provided by "toolbox" |
| 215 |
%%# for this purpose. |
| 216 |
%%# As explained in the example, the main generic macro provided to this |
| 217 |
%%# purpose is \toolboxMakeDef. Its call syntax is as follows: |
| 218 |
%%# |
| 219 |
%%# \toolboxMakeDef[Prefix]{Name}{ReplacementMask} |
| 220 |
%%# |
| 221 |
%%# (the argument [Prefix] is optional and by default empty). |
| 222 |
%%# The above command generates new macros |
| 223 |
%%# |
| 224 |
%%# \PrefixNewName |
| 225 |
%%# \PrefixRenewName |
| 226 |
%%# \PrefixProvideName |
| 227 |
%%# \PrefixDefName |
| 228 |
%%# \PrefixLetName |
| 229 |
%%# \PrefixName |
| 230 |
%%# |
| 231 |
%%# which in turn can be called as follows |
| 232 |
%%# |
| 233 |
%%# \PrefixNewName{something}{RememberText} |
| 234 |
%%# \PrefixRenewName{something}{RememberText} |
| 235 |
%%# \PrefixProvideName{something}{RememberText} |
| 236 |
%%# \PrefixDefName{something}{RememberText} |
| 237 |
%%# \PrefixNewName*{something}{\SomeMacro} |
| 238 |
%%# \PrefixRenewName*{something}{\SomeMacro} |
| 239 |
%%# \PrefixProvideName*{something}{\SomeMacro} |
| 240 |
%%# \PrefixDefName*{something}{\SomeMacro} |
| 241 |
%%# \PrefixLetName{\SomeMacro}{something} |
| 242 |
%%# \PrefixName{something} |
| 243 |
%%# \PrefixName*{something} |
| 244 |
%%# |
| 245 |
%%# These calls are in a sense similar to the respective commands |
| 246 |
%%# |
| 247 |
%%# \newcommand{\something}{RememberText} |
| 248 |
%%# \renewcommand{\something}{RememberText} |
| 249 |
%%# \providecommand{\something}{RememberText} |
| 250 |
%%# \def\something{RememberText} |
| 251 |
%%# \newcommand{\something}{}\let\something\SomeMacro |
| 252 |
%%# \renewcommand{\something}{}\let\something\SomeMacro |
| 253 |
%%# \@ifundefined\something{\let\something\SomeMacro}{} |
| 254 |
%%# \let\something\SomeMacro |
| 255 |
%%# \let\SomeMacro{\something} |
| 256 |
%%# \something |
| 257 |
%%# \something (but without error if \something is undefined). |
| 258 |
%%# |
| 259 |
%%# with the differences already pointed out before: |
| 260 |
%%# 1. The macro name actually used is not \something. Instead, it is a name |
| 261 |
%%# which does not conflict with any existing macro (except one |
| 262 |
%%# generated previously by another \PrefixNewName, but in this case a |
| 263 |
%%# descriptive error is reported). For this reason, it is not possible to |
| 264 |
%%# use this macro directly but only indirectly by the call |
| 265 |
%%# \PrefixName{something} (or with \PrefixLetName). |
| 266 |
%%# 2. The replacement text is not `RememberText' but determined by |
| 267 |
%%# ReplacementMask where every occurrence of #1 in ReplacementMask is |
| 268 |
%%# replaced by RememberText (recall the examples). If you want to have the |
| 269 |
%%# plain RememberText, use {#1} as ReplacementMask. |
| 270 |
%%# |
| 271 |
%%# Since toolbox 4.2 there is another slight difference: The symbol '#' |
| 272 |
%%# is treated as usual and not as in a macro definition. |
| 273 |
%%# |
| 274 |
%%# \toolboxMakeDef gives an error message if the commands |
| 275 |
%%# \Prefix... are already defined. If you intentionally want to |
| 276 |
%%# change a previous definition, you have to call the command |
| 277 |
%%# |
| 278 |
%%# \toolboxFreeDef[Prefix]{Name} |
| 279 |
%%# |
| 280 |
%%# before. The latter not only lets all of the macros \Prefix... be |
| 281 |
%%# \undefined, but also frees all other memory internally used by the |
| 282 |
%%# corresponding call of \toolboxMakeDef |
| 283 |
%%# (note, however, that the above command does not free the |
| 284 |
%%# memory allocated before by calls of \PrefixNew... - to free the latter, |
| 285 |
%%# you have to call subsequently e.g. |
| 286 |
%%# |
| 287 |
%%# \PrefixNewName*{...}{\undefined} |
| 288 |
%%# |
| 289 |
%%% before). There is also the command |
| 290 |
%%# |
| 291 |
%%# \toolboxFreeDef*[Prefix]{Name} |
| 292 |
%%# |
| 293 |
%%# which acts similarly as \toolboxFreeDef but which does not undefine the |
| 294 |
%%# two macros \PrefixName and \PrefixLetName. |
| 295 |
%%# |
| 296 |
|
| 297 |
%%# |
| 298 |
%%# Fancy optional argument parsing |
| 299 |
%%# ------------------------------- |
| 300 |
|
| 301 |
%%# This section contains macros which are convenient if you e.g. write a |
| 302 |
%%# package that contains macros which contain a lot of optional arguments |
| 303 |
%%# and flags (like "*"). |
| 304 |
%%# Typically, to read such an optional argument or flag, you save |
| 305 |
%%# the next token with \futurelet and then call a macro which decides what to |
| 306 |
%%# do with the token read. Thus, a typical use of \futurelet looks like |
| 307 |
%%# |
| 308 |
%%# \def\MacroWithOptionalFlag{\futurelet\tokread\myscan} |
| 309 |
%%# |
| 310 |
%%# which will define \tokread to be the token *following* the macro |
| 311 |
%%# \MacroWithOptionalFlag in the token stream and then execute \myscan. |
| 312 |
%%# In this context, it is not very convenient that you are *forced* |
| 313 |
%%# to define a macro \myscan: It could be more convenient if you could just |
| 314 |
%%# write the *content* of \tokread (in braces) into the above definition. |
| 315 |
%%# You can indeed do this if you replace \futurelet by \toolboxFuturelet: |
| 316 |
%%# |
| 317 |
%%# \toolboxFuturelet\token{argument} |
| 318 |
%%# |
| 319 |
%%# The call \toolboxFuturelet\token{\command} has precisely the same effect as |
| 320 |
%%# \futurelet\token\command. The advantage of \toolboxFuturelet is that |
| 321 |
%%# instead of a single \command one may use also a sequence of commands. |
| 322 |
|
| 323 |
%%# Let us consider \MacroWithOptionalFlag as above. Assume that the user |
| 324 |
%%# has called this macro in the form "\MacroWithOptionalFlag*" where the |
| 325 |
%%# "*" is a flag which should cause your macro to do something slightly |
| 326 |
%%# different. On some place in your macro definition you will have recognized |
| 327 |
%%# (e.g. with \futurelet or \toolboxFuturelet) that a "*" is following |
| 328 |
%%# in the calling sequence. So you now want to execute your action |
| 329 |
%%# (whatever \MacroWithOptionalFlag is supposed to do). |
| 330 |
%%# However, if you do not take special care, after this action, TeX will |
| 331 |
%%# print a "*", because this is the next token on the token stream: \futurelet |
| 332 |
%%# does not delete any tokens. So you have to "gobble" this token away. |
| 333 |
%%# A rude way to do this is by using the macro \gobblenext as the last token |
| 334 |
%%# in you macro which can be defined by |
| 335 |
%%# \begin{verbatim} |
| 336 |
%%# \def\gobblenext#1{}\end{verbatim} |
| 337 |
%%# However, this has two major drawbacks: |
| 338 |
%%# |
| 339 |
%%# 1. This works for "*", but not for "{" or space tokens. |
| 340 |
%%# For space tokens the situation is even worse, since TeX eats spaces |
| 341 |
%%# around arguments, so sometimes space tokens might unexpectedly |
| 342 |
%%# disappear. |
| 343 |
%%# 2. It is not possible in this way to read another argument following |
| 344 |
%%# the "*": Recall that \gobblenext must be the *last* token in your |
| 345 |
%%# macro expansion, i.e. you have "lost control" after this call. |
| 346 |
%%# |
| 347 |
%%# The solution to these problems is instead of calling \gobblenext to use |
| 348 |
%%# \toolboxGobbleNext as the last command in your call: |
| 349 |
%%# You can pass it an argument which describes the action that |
| 350 |
%%# you want to do *after* gobbling the next token ("*" in the above example) |
| 351 |
%%# from the token stream. Thus |
| 352 |
%%# |
| 353 |
%%# \toolboxGobbleNext{cmd} |
| 354 |
%%# |
| 355 |
%%# erases the token following that command from the token stream and |
| 356 |
%%# then executes cmd. This is similar to |
| 357 |
%%# \def\toolboxGobbleNext#1#2{#1} |
| 358 |
%%# with the difference that #2 is considered as a token and that no spaces |
| 359 |
%%# are eaten. |
| 360 |
%%# The effect is that e.g. the call |
| 361 |
%%# |
| 362 |
%%# Example: |
| 363 |
%%# \toolboxGobbleNext{\foo}{{arg} |
| 364 |
%%# is the same as \foo{arg} (the brace "{" is eaten in this example). |
| 365 |
|
| 366 |
%%# As described earlier, the commands "\futurelet" or "\toolboxFuturelet" can |
| 367 |
%%# be used to check for optional flags. Frequently you will only want |
| 368 |
%%# to test for one particular flag and decide the next action on this flag. |
| 369 |
%%# Of course, you can test the token found with \ifx...\f", but this has the |
| 370 |
%%# disadvantage that some tokens (e.g. \fi) follow your action, which might |
| 371 |
%%# be bad (recall that e.g. \toolboxGobbleNext must be the last command of |
| 372 |
%%# your action, i.e. it would in the above examples not gobble the "*" but |
| 373 |
%%# the "\fi" which is probably not what you want). |
| 374 |
%%# The simplest solution is to use the command \toolboxIfNextToken which |
| 375 |
%%# already has the test included. For example, to test for an optional "[", |
| 376 |
%%# you can simply write |
| 377 |
%%# |
| 378 |
%%# \def\MacroWithOptionalBrace{\toolboxIfNextToken[{\yes}{\no}} |
| 379 |
%%# |
| 380 |
%%# and then the call "\MacroWithOptionalBrace[...]" will expand to "\yes[...]" |
| 381 |
%%# while "\MacroWithOptionalBrace x" will expand to "\no x" |
| 382 |
%%# (note that the brace is not gobbled - if you want the latter, |
| 383 |
%%# use "\toolboxIfNextGobbling" described below). |
| 384 |
|
| 385 |
%%# More precisely, the calling syntax of "\toolboxIfNextToken" is |
| 386 |
%%# |
| 387 |
%%# \toolboxIfNextToken{token}{IfPart}{ElsePart} |
| 388 |
%%# |
| 389 |
%%# The semantic is the following: |
| 390 |
%%# If the token following this command is `token', then IfPart is executed, |
| 391 |
%%# otherwise ElsePart. It is explicitly admissible that `token' is a space. |
| 392 |
%%# To support further tests, \toolboxToken is \let to the token which follows |
| 393 |
%%# the command. \toolboxToken is only a temporary token, i.e. |
| 394 |
%%# it may also be modified by other commands of this package; in particular, |
| 395 |
%%# you may also freely to modify \toolboxToken. |
| 396 |
%%# The token \toolboxSpaceToken which is described later may be handy |
| 397 |
%%# in connection with this command. |
| 398 |
%%# |
| 399 |
%%# In contrast to similar LaTeX2e macros much care has been taken |
| 400 |
%%# that spaces are not eaten. This solves the following problem: |
| 401 |
%%# |
| 402 |
%%# Assume that you want to write a macro which should have the calling syntax |
| 403 |
%%# "\mymacro{arg1}" or "\mymacro{arg1}[arg2]". You will probably implement |
| 404 |
%%# \mymacro to read the first argument and then to look whether the next |
| 405 |
%%# token is a "[". If you use the LaTeX2e macro to test for "[", |
| 406 |
%%# then all spaces until the next non-space token would be gobbled which means |
| 407 |
%%# that if you would use the LaTeX2e macros for the test, then the call |
| 408 |
%%# "\mymacro{arg1} Text" would behave like "\mymacro{arg1}Text", |
| 409 |
%%# i.e. the space is `mysteriously' lost. With the "toolbox" macros this |
| 410 |
%%# does not happen. The `disadvantage' is that "\mymacro{arg1} [arg2]" |
| 411 |
%%# is not the same as "\mymacro{arg1}[arg2]" (which is reasonable IMHO). |
| 412 |
%%# |
| 413 |
%%# Example: |
| 414 |
%%# |
| 415 |
%%# \def\mycmd#1{\toolboxIfNextToken[{\ParseOpt{#1}}{\NoOpt{#1}}} |
| 416 |
%%# \def\ParseOpt#1[#2]{\OptAtEnd{#1}{#2}} |
| 417 |
%%# |
| 418 |
%%# After the above definition, \mycmd{arg} executes \NoOpt{arg} while |
| 419 |
%%# \mycmd{arg}[optional] executes \OptAtEnd{arg}{optional} |
| 420 |
%%# We point out once more that in the first call a space following |
| 421 |
%%# \mycmd{arg} does not vanish (as would be the case if the LaTeX 2e macros |
| 422 |
%%# would have been used). |
| 423 |
|
| 424 |
%%# If \toolboxIfNextToken has found the required token, it does *not* |
| 425 |
%%# gobble that token from the token stream. Of course, you can do this by |
| 426 |
%%# yourself using the earlier described macro \toolboxGobbleNext. However, |
| 427 |
%%# it is simpler to use |
| 428 |
%%# |
| 429 |
%%# \toolboxIfNextGobbling{token}{IfPart}{ElsePart} |
| 430 |
%%# |
| 431 |
%%# This command is analogous to \toolboxIfNextToken with the difference that |
| 432 |
%%# in the case that the next token is the desired token, it is gobbled before |
| 433 |
%%# IfPart is executed. |
| 434 |
%%# |
| 435 |
%%# Example: |
| 436 |
%%# |
| 437 |
%%# \def\myloop{\toolboxIfNextGobbling*\toolboxTokenLoop\toolboxLoop} |
| 438 |
%%# |
| 439 |
%%# This makes \myloop*... behave like \toolboxTokenLoop, and |
| 440 |
%%# \myloop... (without *) behave like \toolboxLoop. |
| 441 |
|
| 442 |
%%# The following macro is one which you may want to use in connection with |
| 443 |
%%# LaTeX 2e optional arguments: |
| 444 |
%%# |
| 445 |
%%# \toolboxIfEmpty{arg}{IfPart}{ElsePart} |
| 446 |
%%# |
| 447 |
%%# The argument is not expand; it is only used to decide whether the |
| 448 |
%%# {IfPart} or the {ElsePart} will be expanded. |
| 449 |
%%# For further tests there are more involved macros: |
| 450 |
%%# |
| 451 |
%%# \toolboxIfx{arg}\macro{IfPart}{ElsePart} |
| 452 |
%%# |
| 453 |
%%# This tests via \ifx whether \def\Macro{arg} would give the definition |
| 454 |
%%# of \macro. |
| 455 |
%%# |
| 456 |
%%# \toolboxIfX{arga}{argb}{IfPart}{ElsePart} |
| 457 |
%%# |
| 458 |
%%# This tests whether "arga" and "argb" are the same token sequences. |
| 459 |
%%# If you want to avoid the \else and \fi commands to avoid certain side |
| 460 |
%%# effects, you can use instead: |
| 461 |
%%# |
| 462 |
%%# \toolboxIfElse{ifcmd}{IfPart}{ElsePart} |
| 463 |
%%# |
| 464 |
%%# This is rather analogous to "ifcmd" IfPart \else ElsePart \fi |
| 465 |
%%# but has everything in this line already eliminated from the tokenlist |
| 466 |
%%# when IfPart resp. ElsePart are expanded. |
| 467 |
|
| 468 |
%%# |
| 469 |
%%# Loops over tokenlists and itemlists |
| 470 |
%%# ----------------------------------- |
| 471 |
|
| 472 |
%%# \toolboxLoop{items}{action} |
| 473 |
%%# |
| 474 |
%%# This calls iteratively "action{#1}", where #1 runs over each item in the |
| 475 |
%%# argument items. Here, an item is either a token or a group braced by {...}. |
| 476 |
%%# In the latter case, the braces are lost. Spaces in items are ignored |
| 477 |
%%# (unless they are braced). It is admissible that \action is not a single |
| 478 |
%%# macro but instead a sequence of tokens. |
| 479 |
%%# Examples follow below. |
| 480 |
%%# |
| 481 |
%%# The counterintuitive order of arguments is explained by the fact that the |
| 482 |
%%# typical usage is |
| 483 |
%%# \expandafter\toolboxLoop\expandafter{\MacroExpandingToItems}{action} |
| 484 |
%%# which for swapped order of arguments could hardly be written. |
| 485 |
|
| 486 |
%%# \toolboxLoop is not reentrant i.e. "action" may not expand |
| 487 |
%%# to something which contains a call to \toolboxLoop. To enable such calls |
| 488 |
%%# anyway, the command |
| 489 |
%%# |
| 490 |
%%# \toolboxLoopName{name}{items}{\action} |
| 491 |
%%# |
| 492 |
%%# is provided which is analogous to \toolboxLoop. This is also not reentrant, |
| 493 |
%%# but in contrast to \toolboxLoop, calls with different `name' arguments can |
| 494 |
%%# be used independently of each other, i.e. in the `action' part of a |
| 495 |
%%# \toolboxLoop (or \toolboxLoopName) can be a call to \toolboxLoopName with |
| 496 |
%%# a *different* `name'. |
| 497 |
%%# In particular, using a counter in `name' one could easily implement |
| 498 |
%%# even recursive calls. In this connection, it should be noted that |
| 499 |
%%# `name' is expanded via \csname ... \endcsname, and so you may use |
| 500 |
%%# constructs like \the\namecounter there. |
| 501 |
|
| 502 |
%%# \toolboxTokenLoop{tokens}{\action} |
| 503 |
%%# |
| 504 |
%%# This is similar to \toolboxLoop: The command \action\toolboxToken |
| 505 |
%%# is executed iteratively where \toolboxToken runs over each token in tokens. |
| 506 |
%%# The important difference is that \toolboxToken is a token (instead of an |
| 507 |
%%# item). In particular, \toolboxToken runs through every single token |
| 508 |
%%# including spaces and braces. |
| 509 |
%%# The token \toolboxSpaceToken which is described later may be handy |
| 510 |
%%# in connection with this command. |
| 511 |
%%# |
| 512 |
%%# Example: |
| 513 |
%%# |
| 514 |
%%# \toolboxTokenLoop{Some text}{\kern0.1em} |
| 515 |
%%# |
| 516 |
%%# is the similar to "\kern0.1em S\kern0.1em o\kern0.1em m...", i.e. you |
| 517 |
%%# get wider spacing between the letters of "Some text" (I do not claim that |
| 518 |
%%# this is typographically a good idea). |
| 519 |
%%# |
| 520 |
%%# Note that you do not have to take special care about the space. |
| 521 |
%%# With \toolboxLoop, you would have to mask the space e.g. with |
| 522 |
%%# |
| 523 |
%%# \toolboxLoop{Some{ }text}{\kern0.1em} or |
| 524 |
%%# \toolboxLoop{Some\toolboxSpace text}{\kern0.1em} |
| 525 |
%%# |
| 526 |
%%# In contrast, \toolboxTokenLoop would behave differently here: |
| 527 |
%%# |
| 528 |
%%# \toolboxTokenLoop{Some{ }text}{\kern0.1em} |
| 529 |
%%# |
| 530 |
%%# would produce \kern0.1em S...\kern0.1em{\kern0.1em <space>\kern0.1em}... |
| 531 |
%%# because the braces are simply considered as tokens. |
| 532 |
|
| 533 |
%%# \toolboxTokenLoop is not reentrant. Analogously to \toolboxLoopName, |
| 534 |
%%# independent versions can be generated by |
| 535 |
%%# |
| 536 |
%%# \toolboxTokenName{name}{tokens}{\action} |
| 537 |
|
| 538 |
%%# |
| 539 |
%%# Controlled expansion |
| 540 |
%%# -------------------- |
| 541 |
|
| 542 |
%%# There are some occasions when you want more control over the expansion. |
| 543 |
%%# E.g. you might want to concatenate the contents of two macros to a |
| 544 |
%%# further macro or you want to expand a macro by one level but no full |
| 545 |
%%# expansion. Usually you can get this effects with \expandafter, but if |
| 546 |
%%# you expand several concatenated tokens in this way you either have to |
| 547 |
%%# write a lot of \expandafter's or you have to define subsidiary macros |
| 548 |
%%# that help you to \expandafter certain parts of macros. The macros in |
| 549 |
%%# this section allow you to do this in the most generic way that I could |
| 550 |
%%# implement. |
| 551 |
|
| 552 |
%%# \toolboxDef\macrotodefine{argumentlist} |
| 553 |
%%# |
| 554 |
%%# This call is similar to |
| 555 |
%%# \def\macrotodefine{argumentlist} |
| 556 |
%%# with two important differences: |
| 557 |
%%# |
| 558 |
%%# For \toolboxDef, the argumentlist is expanded precisely by one level. |
| 559 |
%%# argumentlist may not contain macros with parameters, and spaces on the |
| 560 |
%%# highest level are ignored. If you want to force a space on a particular |
| 561 |
%%# place, use the macro \toolboxSpace at this place |
| 562 |
%%# (which is described later). Contrary to the usual \def, the symbol # is |
| 563 |
%%# treated as a usual symbol. |
| 564 |
%%# |
| 565 |
%%# Example of usage: |
| 566 |
%%# \toolboxDef\chain{\chain\toolboxSpace\after} |
| 567 |
%%# This modifies the macro \chain such that a space and the content of the |
| 568 |
%%# macro \after is appended at the end. |
| 569 |
|
| 570 |
%%# \toolboxAppend\macrotodefine{arglist} |
| 571 |
%%# |
| 572 |
%%# This is equivalent to \toolboxDef\macrotodefine{\macrotodefine arglist} |
| 573 |
%%# |
| 574 |
|
| 575 |
%%# \toolboxSurround{ContentBefore}{ContentAfter}\macro |
| 576 |
%%# |
| 577 |
%%# This redefines \macro such that ContentBefore is put at the beginning |
| 578 |
%%# and ContentAfter after the definition of \macro. So this is equivalent to |
| 579 |
%%# \def\macro{ContentBefore * ContentAfter} |
| 580 |
%%# where * is the old content of \macro. It is required that \macro is a |
| 581 |
%%# usual macro without any arguments. If you want to patch more complicated |
| 582 |
%%# macros, use the patch.doc package instead. |
| 583 |
%%# The order of the arguments may appear strange, but it is convenient if |
| 584 |
%%# ContentBefore or ContentAfter are macros which should be expanded with |
| 585 |
%%# \expandafter. |
| 586 |
|
| 587 |
%%# There is some subsidiary macro used in the implementation of the above |
| 588 |
%%# macros which might be useful also in some other situations: |
| 589 |
%%# |
| 590 |
%%# \toolboxTokDef{argumentlist}\macrotodefine |
| 591 |
%%# |
| 592 |
%%# This call is similar to |
| 593 |
%%# \def\macrotodefine{argumentlist} |
| 594 |
%%# with the difference that the symbol # is stored as such. |
| 595 |
%%# The order of the arguments has been swapped in order to simplify the |
| 596 |
%%# application of \expandafter to the argumentlist. |
| 597 |
|
| 598 |
%%# |
| 599 |
%%# Searching, splitting, and replacing |
| 600 |
%%# ---------------------------------- |
| 601 |
|
| 602 |
%%# \toolboxSplitAt{argument}{search}{\beforestring}{\afterstring} |
| 603 |
%%# |
| 604 |
%%# Here, \beforestring and \afterstring are arbitrary macro names, |
| 605 |
%%# and search and argument are any sequences of tokens (which are in |
| 606 |
%%# the following considered as `strings'). |
| 607 |
%%# |
| 608 |
%%# This call scans the string `argument' for the first occurrence of `search'. |
| 609 |
%%# The macros \beforestring and \afterstring are defined correspondingly |
| 610 |
%%# such that \beforestring expands to the part before the first occurrence, |
| 611 |
%%# and \afterstring to the part following the first occurrence. |
| 612 |
%%# If `search' does not occur in `argument', \beforestring is defined to |
| 613 |
%%# `argument', and \afterstring is \let \undefined. |
| 614 |
%%# |
| 615 |
%%# If \beforestring or \afterstring had already been defined before the |
| 616 |
%%# call, the previous definition is tacitly overridden. |
| 617 |
%%# It is explicitly allowed that \beforestring and \afterstring are the |
| 618 |
%%# same names. In this case, the result has the meaning of \afterstring. |
| 619 |
%%# |
| 620 |
%%# It is guaranteed that braces {...} are *not* lost in `argument'. |
| 621 |
%%# However, `search' may not contain any braces, and `argument' may contain |
| 622 |
%%# only matching pairs of braces. Moreover, occurrences of `search' within a |
| 623 |
%%# pair of braces in `argument' are not recognized. |
| 624 |
%%# |
| 625 |
%%# (The order of the arguments has been chosen in order to simplify the |
| 626 |
%%# use of \expandafter). |
| 627 |
%%# |
| 628 |
%%# There are some restrictions for the strings in search. For example, |
| 629 |
%%# the symbol '#' is not allowed. |
| 630 |
%%# |
| 631 |
%%# In the above call, the arguments may not run over several paragraphs. |
| 632 |
%%# If you want the latter, you have to use the alternative call |
| 633 |
%%# |
| 634 |
%%# \toolboxSplitAt*{argument}{search}{\beforestring}{\afterstring} |
| 635 |
%%# |
| 636 |
%%# Example of usage: |
| 637 |
%%# |
| 638 |
%%# \def\examplemacro#1{\toolboxSplitAt{#1}{@}\testme\testme |
| 639 |
%%# \ifx\testme\undefined |
| 640 |
%%# ... (do this when #1 contains no `@' token) |
| 641 |
%%# \fi} |
| 642 |
%%# |
| 643 |
|
| 644 |
%%# \toolboxMakeSplit{search}{command} |
| 645 |
%%# |
| 646 |
%%# If \toolboxSplitAt should be used several times with the same search |
| 647 |
%%# string, it is much more effective to use the above call: This call |
| 648 |
%%# defines a new macro \command (the name is determined by the second argument |
| 649 |
%%# of \toolboxMakeSplit) which can be called in the form |
| 650 |
%%# |
| 651 |
%%# \command{argument}{\beforestring}{\afterstring} |
| 652 |
%%# |
| 653 |
%%# and which has the analogous meaning as \toolboxSplitAt (the argument |
| 654 |
%%# {search} is implicitly fixed and taken from the call of |
| 655 |
%%# \toolboxMakeSplit). |
| 656 |
%%# It is explicitly admissible that the above macro \toolboxMakeSplit is |
| 657 |
%%# used with an already existing command name. In this case, the previous |
| 658 |
%%# definition of \command is tacitly overridden. |
| 659 |
%%# |
| 660 |
%%# The command created by \toolboxMakeSplit does not accept arguments which |
| 661 |
%%# run over several paragraphs. If you want the latter, you have to create |
| 662 |
%%# this command by the alternative call |
| 663 |
%%# |
| 664 |
%%# \toolboxMakeSplit*{search}{command} |
| 665 |
|
| 666 |
%%# |
| 667 |
%%# The command |
| 668 |
%%# \toolboxFreeSplit{command} |
| 669 |
%%# frees the memory used by a previous \toolboxMakeSplit (and lets \command |
| 670 |
%%# again be undefined). |
| 671 |
|
| 672 |
%%# The command |
| 673 |
%%# \toolboxReplace{search}{replace}\macro |
| 674 |
%%# replaces in \macro all occurences of {search} by {replace}. |
| 675 |
%%# The same matches are found as in \toolboxSplitAt. |
| 676 |
%%# If you need to search for the same text several times, it is faster |
| 677 |
%%# to use the command |
| 678 |
%%# \toolboxReplaceSplit{replace}\SplitCmd\macro |
| 679 |
%%# where \SplitCmd is a command previously generated with \toolboxMakeSplit* |
| 680 |
%%# according to your search string. (You could also use \toolboxMakeSplit |
| 681 |
%%# to generate \SplitCmd, but then \macro should not contain any \par's). |
| 682 |
|
| 683 |
%%# |
| 684 |
%%# Redefinition of macros |
| 685 |
%%# ---------------------- |
| 686 |
|
| 687 |
%%# \toolboxMakeHarmless{\macro} |
| 688 |
%%# |
| 689 |
%%# The above call redefines \macro such that it expands to an ASCII text |
| 690 |
%%# containing the previous definition of \macro (i.e. the catcodes of \macro |
| 691 |
%%# are changed). |
| 692 |
|
| 693 |
%%# \toolboxDropBrace\variable |
| 694 |
%%# |
| 695 |
%%# drops possible outer braces in \variable. More precisely, |
| 696 |
%%# if \variable expands to {content}, then \variable is redefined to |
| 697 |
%%# content (without braces). Otherwise, nothing happens. |
| 698 |
|
| 699 |
%%# |
| 700 |
%%# \toolboxIf\comparison{DefinitionCommand}{\macro}... |
| 701 |
%%# |
| 702 |
%%# The above command allows conditional definitions. |
| 703 |
%%# Here, {DefinitionCommand} is either \def, {\long\def}, \let, or some |
| 704 |
%%# similar command like the LaTeX \newcommand. If the test |
| 705 |
%%# \ifx\comparison\macro evaluates positive, then \macro is defined |
| 706 |
%%# correspondingly. Otherwise, \macro is not changed. |
| 707 |
%%# |
| 708 |
%%# Examples: |
| 709 |
%%# |
| 710 |
%%# \toolboxIf\undefined\def\macro{....} |
| 711 |
%%# \toolboxIf\undefined\let\macro... |
| 712 |
%%# \toolboxIf\undefined{\long\def}\macro{....} |
| 713 |
%%# \toolboxIf\undefined\newcommand{\macro}{....} |
| 714 |
%%# |
| 715 |
%%# are similar to \def\macro{...} resp. \let\macro... resp. |
| 716 |
%%# \long\def\macro{...} resp. \newcommand{\macro}{...} with the difference |
| 717 |
%%# that \macro is not changed if it was already defined. |
| 718 |
%%# In this sense, \toolboxIf is a more flexible variant of \providecommand. |
| 719 |
%%# |
| 720 |
|
| 721 |
%%# \toolboxNewiftrue{name} or \toolboxNewiffalse{name} |
| 722 |
%%# |
| 723 |
%%# If the command \ifname was already introduced with \newif, then |
| 724 |
%%# nothing happens. Otherwise \ifname is introduced similarly to |
| 725 |
%%# \newif\ifname and set to `true' respectively `false'. |
| 726 |
%%# In contrast to the corresponding command in TeX or LaTeX 2.09, this macro |
| 727 |
%%# is not \outer! |
| 728 |
|
| 729 |
%%# \toolboxNewifTrue{name} or \toolboxNewifFalse{name} |
| 730 |
%%# |
| 731 |
%%# are similar to \toolboxNewiftrue{name} and \toolboxNewiffalse{name}, |
| 732 |
%%# respectively, with the difference that \ifname is set unconditionally |
| 733 |
%%# to `true' respectively `false'. |
| 734 |
|
| 735 |
%%# |
| 736 |
%%# Concatenated macro names |
| 737 |
%%# ------------------------ |
| 738 |
|
| 739 |
%%# \toolboxLet\variable{macroname} |
| 740 |
%%# |
| 741 |
%%# The above command is analogous to \let\variable\macroname with the |
| 742 |
%%# difference that macroname can also contain other tokens like numbers |
| 743 |
%%# (it is obtained via \csname). Some converse to this command is |
| 744 |
%%# |
| 745 |
%%# \toolboxWithNr {number}\cmd{macro} |
| 746 |
%%# |
| 747 |
%%# which translates into \cmd\macronumber (here, `macro' and `number' |
| 748 |
%%# are just concatenated and evaluated via \csname). |
| 749 |
%%# Examples: |
| 750 |
%%# \toolboxWithNr 1\let{name}\toolboxEmpty |
| 751 |
%%# This is the same as \let\name1\toolboxEmpty |
| 752 |
%%# (but such that \name1 is considered as a name, not as \name 1) |
| 753 |
%%# \toolboxWithNr {10}\def{name}{Foo} |
| 754 |
%%# This corresponds analogously to \def\name10{Foo}. |
| 755 |
%%# \toolboxLet \mymacro{name\the\mycount} |
| 756 |
%%# This is similar to \let\mymacro\namexx where xx is the content |
| 757 |
%%# of the counter \mycount. |
| 758 |
|
| 759 |
%%# |
| 760 |
%%# Various |
| 761 |
%%# ------- |
| 762 |
|
| 763 |
%%# The following macros have equivalents in most formats (like LaTeX2e). |
| 764 |
%%# However, we do not want to rely too much on these formats, so we provide |
| 765 |
%%# our own definitions. |
| 766 |
%%# The macro |
| 767 |
%%# \toolboxEmpty |
| 768 |
%%# expands to nothing (usually, this is the same as \empty). |
| 769 |
%%# Similarly, the macro |
| 770 |
%%# \toolboxSpace |
| 771 |
%%# expands to a space symbol (usually, this is the same as \space). |
| 772 |
%%# The token |
| 773 |
%%# \toolboxSpaceToken |
| 774 |
%%# is \let a space token (usually, this is the same as \@sptoken). |
| 775 |
%%# This token is convenient in tests of tokens |
| 776 |
%%# (because it is hard to get a space there which is not eaten by the TeX |
| 777 |
%%# parser, although sometimes also constructions like |
| 778 |
%%# \expandafter\ifx\toolboxSpace\token can be used). |
| 779 |
%%# Also the macros |
| 780 |
%%# \toolboxFirstOfTwo |
| 781 |
%%# \toolboxSecondOfTwo |
| 782 |
%%# are provided which read two arguments and return only the first |
| 783 |
%%# respectively second argument (usually, this is the same as |
| 784 |
%%# \@firstoftwo respectively \@secondoftwo). Similarly, |
| 785 |
%%# \toolboxGobbleArg{argument} |
| 786 |
%%# just reads its argument and expands to nothing. |
| 787 |
|
| 788 |
\endinput |
| 789 |
%% |
| 790 |
%% End of file `toolbox.txt'. |