summaryrefslogtreecommitdiff
path: root/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-twooptarg.html
blob: 0284486d9c6e601b71b7b82973158625155025c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<head>
<title>UK TeX FAQ -- question label twooptarg</title>
</head><body>
<h3>More than one optional argument</h3>
<p>If you've already read 
"<a href="FAQ-moren9.html">breaking the 9-argument limit</a>".
you can probably guess the solution to this problem: command relaying.
<p>LaTeX allows commands with a single optional argument thus:
<pre>
  \newcommand{\blah}[1][Default]{...}
</pre>
<p>You may legally call such a command either with its optional argument
present, as
<code>\</code><code>blah[nonDefault]</code> or as <code>\</code><code>blah</code>; in the latter case, the
code of <code>\</code><code>blah</code> will have an argument of <code>Default</code>.
<p>To define a command with two optional arguments, we use the relaying
technique, as follows:
<pre>
  \newcommand{\blah}[1][Default1]{%
    \def\ArgI{{#1}}%
    \BlahRelay
  }
  \newcommand\BlahRelay[1][Default2]{%
    % the first optional argument is now in
    %   \ArgI
    % the second is in #1
    ...%
  }
</pre>
Of course, <code>\</code><code>BlahRelay</code> may have as many mandatory arguments as are
allowed, after allowance for the one taken up with its own
optional argument - that is, 8.
<p>Variants of <code>\</code><code>newcommand</code> (and friends), with names like
<code>\</code><code>newcommandtwoopt</code>, are available in the <i>twoopt</i> package.
However, if you can, it's probably better to learn to write the commands
yourself, just to see why they're not even a good idea from the
programming point of view.
<p>A command with two optional arguments strains the limit of what's
sensible: obviously you can extend the technique to provide as many
optional arguments as your fevered imagination can summon.  However,
see the comments on the use of the <i>keyval</i> package, again in 
"<a href="FAQ-moren9.html">breaking the 9-argument limit</a>".
which offer an alternative way forward.
<p>If you must, however, consider the <i>optparams</i> package;
provides a <code>\</code><code>optparams</code> command that you use as an intermediate in
defining commands with up to nine optional arguments.  The
documentation shows examples of commands with four optional arguments
(and this from an author who has his own key-value package!).
<p>An alternative approach is offered by Scott Pakin's
<i>newcommand</i> program, which takes a command name and a
definition of a set of command arguments (in a fairly
readily-understood language), and emits (La)TeX macros which enable
the command to be defined.  The command requires that a
<i>Python</i> system be installed on your computer.
<dl>
<dt><tt><i>newcommand.py</i></tt><dd><a href="ftp://cam.ctan.org/tex-archive/support/newcommand.zip">support/newcommand</a> (<a href="ftp://cam.ctan.org/tex-archive/support/newcommand.tar.gz">gzipped tar</a>, <a href="http://www.tex.ac.uk/tex-archive/support/newcommand/">browse</a>)
<dt><tt><i>optparams.sty</i></tt><dd>Distributed as part of <a href="ftp://cam.ctan.org/tex-archive/macros/latex/contrib/sauerj.zip">macros/latex/contrib/sauerj</a> (<a href="ftp://cam.ctan.org/tex-archive/macros/latex/contrib/sauerj.tar.gz">gzipped tar</a>, <a href="http://www.tex.ac.uk/tex-archive/macros/latex/contrib/sauerj/">browse</a>)
<dt><tt><i>twoopt.sty</i></tt><dd>Distributed as part of <a href="ftp://cam.ctan.org/tex-archive/macros/latex/contrib/oberdiek.zip">macros/latex/contrib/oberdiek</a> (<a href="ftp://cam.ctan.org/tex-archive/macros/latex/contrib/oberdiek.tar.gz">gzipped tar</a>, <a href="http://www.tex.ac.uk/tex-archive/macros/latex/contrib/oberdiek/">browse</a>)
</dl>
<p><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=twooptarg">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=twooptarg</a>
</body>