Reusable LaTeX Macros with \newcommand and Optional Arguments
Use \newcommand to define repeatable LaTeX macros with optional arguments, defaults, and robust behavior in section headings.
28 Aug 2025, 13:48 UTC

The problem: repeating the same styled phrase
You are writing a LaTeX article and notice the same formatted phrase appearing everywhere: a product name in bold italics, or a version string such as "Release 2.0 (beta)". Copying it by hand is tedious, and a typo in one instance means hunting through the document to fix them all.
The useful takeaway is to define it once with \newcommand in your preamble, then call it everywhere. LaTeX will refuse to silently overwrite an existing macro, you can give the macro an optional argument with a default value, and you can make it robust enough to survive inside a section heading.
Why \newcommand instead of \def
Plain TeX offers \def, but LaTeX's \newcommand is safer for document macros. If you accidentally define the same name twice, \newcommand stops with an "already defined" error instead of quietly replacing the first definition. That prevents subtle bugs where one part of your document expects the old macro and another gets the new one.
Use \renewcommand when you intentionally want to replace an existing macro, and \providecommand when you want to define it only if it does not already exist.
Optional arguments and defaults
\newcommand accepts up to nine mandatory arguments and one optional argument. The optional argument, if present, is declared first and carries a default value.
\newcommand{\release}[2][v1.0]{Release \textbf{#2} (#1)}
Call it as:
\release{2.1} % prints "Release 2.1 (v1.0)"
\release[beta]{2.1} % prints "Release 2.1 (beta)"
The optional argument must appear in square brackets immediately after the macro name. If you put text between the macro name and the bracket, LaTeX treats the bracket as ordinary text.
Fragile macros and moving arguments
Some places in LaTeX, such as \section headings, captions, and \write operations, are "moving arguments." The text is typeset once and also written to auxiliary files such as the table of contents. A macro that works in normal text can break there because its tokens expand at the wrong time. Such macros are called fragile.
If your macro appears in a heading, switch from \newcommand to \DeclareRobustCommand:
\DeclareRobustCommand{\product}[1]{\textbf{\textit{#1}}}
Then \section{Introducing \product{Widget}} compiles safely. For simple formatting macros, \newcommand often works, but robust declaration is the safer default for anything reused in headings.
Worked example
Save this as example.tex and compile with pdflatex example.tex (or LuaLaTeX/XeLaTeX). No elevated permissions are needed; write access to the working directory is sufficient.
\documentclass{article}
\DeclareRobustCommand{\product}[1]{\textbf{\textit{#1}}}
\newcommand{\release}[2][v1.0]{Release \textbf{#2} (#1)}
\begin{document}
\section{Introducing \product{Widget}}
We launched \product{Widget} as \release{2.0}. The next cycle is \release[beta]{2.1}.
\end{document}
Expected checks: the PDF should show bold-italic "Widget" in the heading and body, and the release lines should show the default and overridden optional argument. If you add a second \newcommand{\product} line, the log should report "Command \product already defined" and compilation should stop.
Risks: unbalanced braces or special characters inside an optional argument can confuse the parser. Group them carefully. Also, \newcommand is a LaTeX2e feature; plain TeX documents do not have it unless a format provides it.
Limitation and verification
You can define at most one optional argument per macro, and it must be the first argument. If you need more complex parsing, consider \NewDocumentCommand from the xparse package.
A practical way to check the result is to compile a minimal document, then temporarily add a duplicate \newcommand line and confirm the error appears in the log before removing it again. To verify robustness, place the macro inside \section and confirm no missing-character or alignment errors appear.
Closing
Start with \newcommand for repeated phrases. Add defaults for optional arguments. Use \DeclareRobustCommand when the macro travels into headings. These habits keep your source DRY and your builds predictable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.