Flexible TeX Macros: Using Optional Arguments with \newcommand
Learn how optional arguments in \newcommand let you write reusable TeX macros with variable arity while keeping code simple.
12 Feb 2026, 01:38 UTC

Problem: Reusable macros need variable arity
When you write a TeX macro that will be used in many places, you often face a dilemma. A fixed‑argument macro works for one signature, but if a different call needs an extra parameter you must either create a new macro or duplicate code. This quickly makes maintenance painful.
Solution: Optional‑argument syntax in \newcommand
The \newcommand primitive lets you declare a number of mandatory arguments followed by an optional argument block. The syntax is \newcommand{\cmd}[n]{…}, where n is the count of required arguments before any optional ones. This single definition can handle calls with 1, 2, or more arguments, preserving simplicity while adding flexibility.
How it works
- First, specify how many arguments are mandatory. Anything beyond that is optional and must be supplied positionally.
- When the macro is called, TeX matches arguments to positions; missing optional arguments are replaced by their default value (often the argument itself).
- The macro body receives the arguments in order, so you can use
#1,#2, … for the required parameters and#n+1for the first optional parameter.
Because the optional part is positional, the order of arguments matters. Careless re‑ordering can produce subtle bugs, and the mandatory count must be fixed at definition time.
Worked example
\newcommand{\greet}[2][World]{% mandatory: #1 (name), optional: #2 (greeting)
\textbf{#2} says \textit{#1}
Explanation of placeholders:
#1– the required name argument.#2– the optional greeting argument; if omitted, the default "World" is used.- The macro body uses
#2and#1to produce bold text followed by italicized name.
Typical calls and expected output:
\greet{Alice}→ World says Alice\greet[Hello]{Bob}→ Hello says Bob
Limitations and practical risks
Optional arguments are positional, so swapping the order of a required and an optional argument changes the result. For example, \greet[Hello]{Alice} works, but \greet{Alice}[Hello] treats Alice as the optional greeting and yields Alice says Hello, which is probably not intended. The mandatory argument count must be declared up front; changing it later requires redefining the macro, which can break existing documents.
Overusing optional arguments can make the macro interface hard to read. New users may not know which arguments are required, leading to errors.
Verification steps
- Write a minimal TeX file that defines the macro with optional arguments.
- Compile the file with
pdftex(or any engine that supports \newcommand). No special permissions are needed beyond write access to the .tex file. - Run the three example calls shown above and compare the printed output.
- Check that the optional argument defaults to the intended value when omitted and that re‑ordering arguments produces the expected (or unintended) effect.
Closing thoughts
Optional‑argument syntax in \newcommand is a practical engineering decision that balances flexibility with simplicity. It lets you write a single macro that serves multiple signatures, reducing duplication and keeping your TeX source tidy. However, remember the positional nature of optional arguments and the need to fix the mandatory count at definition time. By testing a small document and reviewing the output, you can confidently adopt this feature in your own reusable TeX macros.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.