About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account


8.1 Defining Commands with an Optional Argument

As mentioned earlier, the \newcommand command has a second optional argument <default>. This allows you to define a command with an optional argument[More than one optional argument]. For example, suppose we want a command called, say, \price. Suppose we want the following code:

\price{100}

to produce the following output:

Image showing typeset output (click here for a more detailed description).

and let's suppose we want an optional argument so that we can change the VAT. That is, we would want the following code:

\price[20]{30}

to produce the following output:

Image showing typeset output (click here for a more detailed description).

Therefore we want to define a command such that if the optional argument is absent we will have 17.5, and if it is present the optional argument will be substituted instead. This command can be defined as follows:

\newcommand{\price}[2][17.5]{\pounds #2 excl VAT @ #1\%}

Here, #1 represents the optional argument (by default 17.5) and #2 represents the mandatory argument (the second argument if the optional argument is present, or the only argument if the optional argument is absent.)

As before, since the argument is unlikely to contain a paragraph break, we should indicate that it is a short command using the starred form:

\newcommand*{\price}[2][17.5]{\pounds #2 excl VAT @ #1\%}

Exercise 20: Defining Commands with an Optional Argument

In this exercise, you will need to define a slightly modified version of the above example. Try defining a command called, say, \cost. It should take one optional argument and one mandatory argument. Without the optional argument, it behaves in the same way as the \price example above, so that, say,

\cost{50}

will produce

but with the optional argument, you can change the excl VAT @ 17.5\% bit. So that, say,

\cost[inc VAT]{50}

will produce

You can download or view the solution.

For the more adventurous:

If you did Exercise 19 and you modified \keyword so that it indexed the keyword, you may have noticed that

\keyword{command}

and

\keyword{commands}

produced separate entries in the index. It would be better to have an optional argument to override the indexing mechanism. For example,

\keyword{command}

should print and index the word “command”, whereas

\keyword[command]{commands}

should print “commands” and index “command”. In other words, we need an optional argument that defaults to the mandatory argument if it is not present. This is how to achieve that type of effect:8.1

\newcommand*{\keyword}[2][\keywordentry]{%
  \def\keywordentry{#2}%
  \textsf{#2}%
  \index{#1}%
}

In this example, the default value for the optional argument is set to the command \keywordentry. At the start of \keyword this is defined to be the mandatory argument (as specified by #2) using TeX's \def command:8.2

\def\keywordentry{#2}
Then typeset the keyword (given in the mandatory argument #2) in a sans-serif font: Now index the term using the optional argument (#1): If an optional argument is specified, #1 will be the given argument, but if the optional argument is missing, #1 will be \keywordentry, which has earlier been set to the mandatory argument #2.



Footnotes

... effect:8.1
Recall from §2. Some Definitions the percent symbol discards the space resulting from the end of line character.
... command:8.2
\def is too complicated for an introductory LaTeX guide but, if you're interested, read The TeXbook [6].

This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).

© 2012 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs