Debian Codegraph @

texmf-dist/doc/latex/exp-testopt/README

raw
1 [en]
2
3 LaTeX-package 'exp-testopt' - expandable variant of \@testopt
4
5 Author: Paul Ebermann <[email protected]>
6 License: LPPL 1.3b or later, maintained
7 (see http://www.latex-project.org/lppl/).
8 Version: 0.3
9
10 All Documentation (other than this file) is in Esperanto.
11
12 This package is meant for use by other packages.
13
14 This package provides an expandable variant of the LaTeX
15 kernel command \@testopt, named \@expandable@testopt, and
16 a more general \@expandable@ifopt, both intended for package
17 writers. Also we have a variant of \newcommand which
18 uses these macros to check for optional arguments.
19
20
21 This Package needs no other packages for running.
22
23 The typesetting of the documentation needs additionally
24 class scrartcl (KOMA-script), gmdoc-enhance (from me)
25 and gmdoc (from Grzegorz `Natror' Murzynowski),
26 gmdoc's dependencies, and some required LaTeX packages.
27
28 The package comes as .dtx + .ins.
29 Run "tex exp-testopt.ins" to create the style file (and maybe put
30 it to texmf/tex/latex/paul/, if your docstrip is configured
31 accordingly), run (after that and maybe updating your TeX hash)
32 "latex exp-testopt.dtx" to create the documentation.
33 (Docstrip also creates an exp-testopt.test in the same directory,
34 which is to be used by the documentation.)
35
36 Usage
37 -------
38
39 There are two main commands for package writers:
40
41 \@expandable@ifopt{<YES>}{<NO>}
42 \@expandable@testopt{<COMMAND>}{<DEFAULT>}
43
44 Both of them try to check, wether following them is a optional
45 argument (this is, some stuff enclosed in []). In this case,
46 they expand to:
47 <YES>
48 <COMMAND>
49 In the other case, they expand to:
50 <NO>
51 <COMMAND>[<DEFAULT>]
52
53 So, \@expandable@ifopt is a general branch command, while
54 \@expandable@testopt is meant as a replacement for \@testopt,
55 where <COMMAND> is "any sequence of commands that 'expects' to
56 be followed by [." (Quoted from ltdefns.dtx.)
57
58 This replacement has the advantage of being expandable
59 (if <COMMAND> is so), but also has quite some limitations.
60
61
62 A third command is:
63
64 \expnewcommand*{<COMMAND>}[<argnum>][<DEFAULT>]{<code>}
65
66 This works like \newcommand, but when a command with an
67 optional argument is defined, it uses our expandable check
68 for the existence of the optional argument. (Without the default
69 argument it should work exactly the same.)
70
71 Examples
72 ----------
73
74
75 The main usage is in definitions of commands, which are to
76 accept optional arguments.
77
78 \expnewcommand*{\example}[1][Default]{%
79 \fbox{#1}%
80 }
81
82 Thus \example outputs an box with "Default" in it, while
83 \example[Test] hat "Test" in it.
84
85
86 From another package (to be published soon):
87
88 \expnewcommand*\monthName[2][\languagename]{%
89 \@format@month@name{#1}{\value{#2}}%
90 }
91
92 This outputs the value of a given LaTeX counter as
93 a name of the like-numbered month, in the given
94 language, the default language being the current
95 babel language. (And here the expansibility is important,
96 since we don't want the value of the counter when
97 this is printed, but now, also when this ends up in
98 the table of contents.)
99
100 When you want more control, you may use \@expandable@ifopt{<YES>}{<NO>}:
101
102 \def\exampleOne{%
103 \@expandable@ifopt{%
104 \exampleOneOpt}{%
105 \exampleOneNoOpt}%
106 }
107
108 \def\exampleOneOpt[#1]{#2}{
109 % do something with #1 and #2
110 }
111 \def\exampleOneNoOpt#1{%
112 % do some other thing with #1
113 }
114
115 With \@expandable@testopt{<COMMAND>}{<DEFAULT>} you may also
116 create commands which accept more than one optional argument.
117
118
119 Benefits
120 ----------
121
122 The advantages of \expnewcommand and \@expandable@testopt to
123 \newcommand and \@testopt are in the expandibility of the created
124 commands - you can use them in moving arguments (like inside of
125 \protected@edef, \protected@write and so on), and the accessed variables
126 then have their current meanings (or the meaning at shipout time, for
127 write), not the meanings when the thing is finally typeset.
128
129 \newcommand, on the other hand, creates protected commands - you can
130 use them also in moving arguments, but they are evaluated only at
131 typesetting time.
132
133 What to use when depends heavily on the use case.
134
135
136 Limitations
137 -------------
138
139 Since we want to be expandable, we really can't check the next
140 token, but only the next "macro argument". And comparing also
141 is not so easy when you can't assign something to a macro.
142
143 So, we use as a workaround making a control sequence from the
144 next argument (trying to neutralize a bit things like control
145 sequences), and comparing them to a known control sequence.
146
147 This has the following consequences:
148
149 - we cant distinguish between [ and {[} - the second one
150 is also regarded as a "start of optional argument".
151
152 - the next argument (when it is not simple '[') has to be of a
153 "friendly" kind - it should not contain braces, and any
154 "dangerous" token (anything not of category 10, 11, 12)
155 should come as the first token (only then it is neutralized).
156 (Often the next argument is only one token, then this is no
157 problem. Or it will be simple text, also no problem.)
158
159 Otherwise, there will be strange errormessages.
160
161 - multiple space tokens before the [ may irritate our
162 macros.
163
164
165 So, before using this macros, make sure the limitations don't
166 affect you.
167
168
169
170 Have fun!
171
172 -------------
173 [de]
174
175 LaTeX-Paket 'exp-testopt' - Expandierbare Variante von \@testopt.
176
177 Autor: Paul Ebermann ([email protected]).
178 Lizenz: LPPL 1.3b oder später, mit Maintenance-Status
179 "author-maintained". Siehe http://www.latex-project.org/lppl/.
180 Version 0.3
181
182 Geschrieben für eigenen Gebrauch
183 (d.h. für Nutzung in anderen LaTeX-Paketen von Paul Ebermann), aber
184 vielleicht ist es auch für andere von Nutzen.
185
186 Die Dokumentation (außer dieser Datei) ist nur auf Esperanto.
187
188
189 Dieses Paket ist für Verwendung in anderen LaTeX-Paketen gedacht.
190 Es enthält eine expandierbare Variante des LaTeX-Kernel-Kommandos
191 \@testopt unter dem Namen \@expandable@testopt, eine
192 Verallgemeinerung davon als \@expandable@ifopt sowie eine Variante
193 von \newcommand, welche diese Makros nutzt, um die Existenz
194 optionaler Argumente festzustellen.
195
196
197 Das Paket kommt als .dtx + .ins.
198 Mit "latex exp-testopt.ins" wird die .sty-Datei erstellt (und
199 eventuell gleich nach texmf/tex/latex/paul/ installiert,
200 wenn docstrip entsprechend eingerichtet ist), mit
201 "latex exp-testopt.dtx" kann (danach und eventuell nach einer
202 Aktualisierung der TeX-Dateidatenbank) die Dokumentation neu
203 erstellt werden.
204 Docstrip erstellt auch eine Datei exp-testopt.test, welche nur
205 von der Dokumentation verwendet wird (vom Paket nicht verwendet).
206
207
208 Anwendung
209 ----------
210
211 Die beiden Haupt-Kommandos für Package-Autoren:
212
213 \@expandable@ifopt{<JA>}{<NEIN>}
214 \@expandable@testopt{<BEFEHL>}{<DEFAULT>}
215
216 Beide versuchen herauszufinden, ob nach ihnen ein optionales
217 Argument folgt (das ist irgend etwas, was in [...] eingeschlossen
218 ist). In diesem Fall expandieren diese Makros zu:
219
220 <JA>
221 <BEFEHL>
222
223 Im anderen Fall expandieren sie zu:
224
225 <NEIN>
226 <BEFEHL>[<DEFAULT>]
227
228 \@expandable@ifopt ist also ein genereller Verzweigungsbefehl,
229 während \@expandable@testopt (welches dem Paket den Namen gab)
230 ein Ersatz für \@testopt aus dem LaTeX-Kernes ist: hier sollte
231 <BEFEHL> eine beliebige Sequenz von Kommandos sein, welche es
232 'erwartet', von [ gefolgt zu werden. (Frei übersetzt aus
233 ltdefns.dtx.) Damit kann man Makros mit optionalen Argumenten
234 und Defaultwerten für diese bauen.
235
236 Dieser Ersatz hat den Vorteil, expandierbar zu sein (falls
237 <BEFEHL> dies ist), hat aber auch einige Beschränkungen (siehe
238 unten).
239
240 Ein drittes Kommando ist:
241
242 \expnewcommand*{<MAKRONAME>}[<argnum>][<DEFAULT>]{<Code>}
243
244 Es funktioniert analog zu \newcommand, mit dem Unterschied,
245 dass (falls <DEFAULT> gegeben ist) die Überprüfung auf
246 optionale Argumente mit unserem \@expandable@testopt anstatt
247 mit \@testopt (bzw. seiner protected-Variante) geschieht.
248 (Ohne diesen Default-Wert sollten beide Definitionen exakt
249 identisch sein.)
250
251 Beispiele
252 -----------
253
254 Die Hauptverwendung wird die Definition von Kommandos mit
255 optionalen Argumenten sein:
256
257 \expnewcommand*{\example}[1][Default]{%
258 \fbox{#1}%
259 }
260
261 Hier gibt \example einen Kasten mit "Default" drin aus, wobei
262 \example[Test] ein "Test" im Kasten hat.
263
264 Aus einem meiner anderen Packages (wird bald veröffentlicht)
265 sinngemäß:
266
267 \expnewcommand*\monthName[2][\languagename]{%
268 \@format@month@name{#1}{\value{#2}}%
269 }
270
271 Das gibt den Wert eines LaTeX-Zählers in der Form
272 eines Monatsnamens in der gewählten Sprache aus, wobei
273 der Default die aktuelle Babel-Sprache ist.
274 (Hierbei braucht man die Expansibilität wirklich, denn
275 wir wollen ja den aktuellen Wert des Zählers (und der
276 Sprache) haben, auch wenn das ganze nachher im
277 Inhaltsverzeichnis landet, wo eventuell andere Werte
278 aktuell sind.)
279
280 Mehr Kontrolle bekommt man mit \@expandable@ifopt:
281
282
283 \def\exampleOne{%
284 \@expandable@ifopt{%
285 \exampleOneOpt}{%
286 \exampleOneNoOpt}%
287 }
288
289 \def\exampleOneOpt[#1]#2{
290 % mache etwas mit #1 und #2
291 }
292 \def\exampleOneNoOpt#1{%
293 % mache etwas ganz anderes mit #1
294 }
295
296 Mit \@expandable@testopt kann man auch Kommandos basteln,
297 welche mehr als ein optionales Argument enthalten.
298
299
300
301 Vorteile
302 ---------
303
304 Die Vorteile von \expnewcommand sowie \@expandable@testopt
305 im Vergleich zu \newcommand und \@testopt aus dem Kernel liegen
306 in der Expandierbarkeit der erstellten Kommandos - man kann sie
307 innerhalb von bewegten Argumenten nutzen (wie \protexted@edef,
308 \@protected@write etc.) und die darin verwendeten Variablen
309 haben dann ihre aktuellen Werte, nicht die Werte, wenn der
310 Text endlich gesetzt wird.
311
312 \newcommand (mit den optionalen Argumenten) erstellt "geschützte"
313 Kommandos - man kann sie in bewegten Argumenten verwenden, aber
314 sie werden erst dann ausgewertet, wenn sie gesetzt werden.
315
316
317 Welche der beiden Möglichkeiten besser ist, hängt stark von
318 der jeweiligen Situation ab.
319
320
321 Begrenzungen
322 --------------
323
324 Weil wir expandierbare Makros haben wollen, können wir nicht
325 wirklich (wie es \@testopt macht) das nächste Token überprüfen,
326 sondern nur das "nächste Makro-Argument". Der Vergleich ist
327 auch nicht einfach, wenn man keine Zuweisungen/Definitionen
328 machen darf.
329
330 Als Workaround erstellen wir eine Kontrollsequenz aus dem
331 nächsten Argument (und versuchen dabei, es etwas unschädlich
332 zu machen), und vergleichen diese mit einer bekannten
333 Kontrollsequenz.
334
335 Das hat die folgenden Konsequenzen:
336
337 - Wir können nicht zwischen [ und {[} unterscheiden - auch
338 das zweite wird als "Anfang eines optionalen Argumentes"
339 interpretiert.
340
341 - Das nächste Argument (wenn es denn nicht ein einfaches '['
342 ist) muss sich friedlich verhalten - es sollte keine
343 {} enthalten, und andere gefährliche Token (alles, was nicht
344 Kategorie 10, 11, 12 hat) sollten nur an erster Stelle
345 auftauchen (da können sie unschädlich gemacht werden).
346
347 (Wenn das nächste Argument nur aus einem Token besteht,
348 ist das kein Problem, auch nicht, wenn es einfach nur
349 Text ist.)
350
351 Andernfalls kann es zu merkwürdigen Fehlermeldungen kommen.
352
353 - Falls vor dem '[' mehrere Leerzeichen-Token kommen, kann
354 das unsere Makros verwirren.
355
356
357 Also, bitte vor dem Verwenden dieser Makros aufpassen, dass
358 die Beschränkungen nicht stören.
359
360
361 Viel Spaß!
362
363 ----------
364 [eo]
365
366 LaTeX-pakaĵo 'exp-testopt' - plibonigoj por gmdoc.
367 (Se vi ne scias, kio estas gmdoc, legu sube.)
368
369 Aŭtoro: Paŭlo Ebermann ([email protected]).
370 Licenzo: LPPL 1.3, 'maintained'
371 (-> http://www.latex-project.org/lppl/).
372 Versio 0.3
373
374 Kreitaj por propra uzo, sed eble iom de ĝi ankaŭ
375 uzeblas por aliaj.
376
377 La dokumentaro (escepte tiu ĉi dosiero) estas nur en la germana
378 lingvo.
379
380 La pakaĵo ne bezonas ajnajn aliajn pakaĵojn.
381 Por la dokumentado bezonatas gmdoc, gmdoc-enhance
382 kaj scrartcl (KOMA-script).
383
384
385 La pakaĵo venas en .dtx + .ins.
386 Voku "latex exp-testopt.ins" por krei la .sty-dosieron (kaj
387 eble meti ĝin al texmf/tex/latex/paul/, se via docstrip estis
388 konfigurita laŭe), voku "latex exp-testopt.dtx" por rekrei
389 la dokumentaron.
390
391 La dokumentaĵo estas en exp-testopt.pdf.
392