diff options
Diffstat (limited to 'Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html')
-rw-r--r-- | Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html b/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html index f9b20074004..e5de72fef15 100644 --- a/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html +++ b/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-oarglikesect.html @@ -2,15 +2,39 @@ <title>UK TeX FAQ -- question label oarglikesect</title> </head><body> <h3>Optional arguments like <code>\</code><code>section</code></h3> -<p>Optional arguments, in macros defined using <code>\</code><code>newcommand</code>, don't +<p>Optional arguments, in macros defined using <code>\</code><code>newcommand</code>, don’t quite work like the optional argument to <code>\</code><code>section</code>. The default -value of <code>\</code><code>section</code>'s optional argument is the value of the -mandatory argument, but <code>\</code><code>newcommand</code> requires that you 'know' the +value of <code>\</code><code>section</code>’s optional argument is the value of the +mandatory argument, but <code>\</code><code>newcommand</code> requires that you ‘know’ the value of the default beforehand. <p>The requisite trick is to use a macro in the optional argument: +<blockquote> <pre> -\newcommand\thing[2][\DefaultOpt]{\def\DefaultOpt{#2} ...} -</pre> +\documentclass{article} +\newcommand\thing[2][\DefaultOpt]{% + \def\DefaultOpt{#2}% + optional arg: #1, mandatory arg: #2% +} +\begin{document} +\thing{manda}% #1=#2 +\thing[opti]{manda}% #1="opti" +\end{document} +</pre> +</blockquote><p> +LaTeX itself has a trickier (but less readily understandable) +method, using a macro <code>\</code><code>@dblarg</code>; inside LaTeX, the example +above would have been programmed: +<blockquote> +<pre> +\newcommand\thing{\@dblarg\@thing} +\newcommand\@thing[2][\@error]{% + optional arg: #1, mandatory arg: #2% +} +</pre> +</blockquote><p> +In that code, <code>\</code><code>@thing</code> is only ever called with an optional and a +mandatory argument; if the default from the <code>\</code><code>newcommand</code> is +invoked, a bug in user code has bitten... <p><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=oarglikesect">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=oarglikesect</a> </body> |