# Welcome to the UK List of TeX Frequently Asked Questions on the Web

## Searching

The index of Frequently Asked Questions about TeX is searchable.

Please enter your keyword here:   then press here: ,    or:

## Optional arguments like \section


The requisite trick is to use a macro in the optional argument:

\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}

LaTeX itself has a trickier (but less readily understandable) method, using a macro \@dblarg; inside LaTeX, the example above would have been programmed:

\newcommand\thing{\@dblarg\@thing}
\newcommand\@thing[2][\@error]{%
optional arg: #1,  mandatory arg: #2%
}

In that code, \@thing is only ever called with an optional and a mandatory argument; if the default from the \newcommand is invoked, a bug in user code has bitten…

Go to previous question, or next question

Go to FAQ home.

URL for this question: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=oarglikesect

Comments, suggestions, or error reports? - see “how to improve the FAQ”.

This is FAQ version 3.28, released on 2014-06-10.