Custom Commands & Macros in LaTeX (\newcommand)
Define custom commands in LaTeX with \newcommand to save typing and keep notation consistent. Add arguments, defaults, and environments — full guide inside.
Define custom commands in LaTeX with \newcommand to save typing and — more importantly — keep notation consistent across a document. Instead of repeating \mathbb{R}^{n} everywhere, define \Rn once; change the definition and every instance updates. Macros are the single biggest productivity win in serious LaTeX writing. Here's how to use them.
1. A command with no arguments
\newcommand{\R}{\mathbb{R}} % the reals
\newcommand{\eg}{e.g.,\ } % consistent "e.g.,"
...
The function maps $\R^n \to \R$. % uses the macro
Define these in the preamble. Now \R always renders identically, and you can restyle every occurrence by editing one line.
2. Commands with arguments
\newcommand{\norm}[1]{\left\lVert #1 \right\rVert} % \norm{x}
\newcommand{\abs}[1]{\left| #1 \right|} % \abs{y}
[1] says one argument; #1 is where it's inserted. Up to nine arguments are allowed.
3. Optional arguments with defaults
\newcommand{\seq}[2][n]{#2_1, \dots, #2_{#1}}
% \seq{x} -> x_1, ..., x_n
% \seq[m]{x} -> x_1, ..., x_m
The value after the count ([n]) is the default for the first argument, supplied in [...].
4. new vs renew vs provide
| Command | Behavior |
|---|---|
| \newcommand | Define new; error if it exists |
| \renewcommand | Redefine existing; error if it doesn't |
| \providecommand | Define only if not already defined |
Use \newcommand for your macros, \renewcommand only to override a default deliberately.
5. Share macros across files
For a big project, keep definitions in macros.tex and pull them in:
\input{macros}
This keeps the main file clean and lets co-authors reuse one notation set — invaluable in collaborative writing. To define a new environment (not just a command), use \newenvironment. Consistent macros pair naturally with the math symbols reference.
→ Define and reuse macros with instant preview in LetX.
Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.
Frequently Asked Questions
What's the difference between \newcommand and \renewcommand?
\newcommand{\foo}{...} defines a brand-new command and errors if \foo already exists, which protects you from accidentally clobbering a built-in. \renewcommand{\foo}{...} redefines an existing command and errors if it doesn't yet exist. Use \newcommand for your own macros and \renewcommand only when you intend to override something like \baselinestretch or a class default. There's also \providecommand, which defines a command only if it isn't already defined.
How do I make a command that takes arguments?
Add the argument count in square brackets: \newcommand{\norm}[1]{\left\lVert #1 \right\rVert} defines \norm{x} that wraps its argument in norm bars, where #1 is the first argument. You can have up to nine arguments (#1 to #9). To give the first argument a default value, add it after the count: \newcommand{\ve}[2][n]{\mathbf{#2}_{#1}} makes the optional first argument default to n unless you pass one in brackets.
Where should I define my custom commands?
Define them in the preamble, after loading packages and before \begin{document}, so they're available throughout. For a large project or a shared notation set, put them in a separate file like macros.tex and pull it in with \input{macros} — this keeps the main file clean and lets co-authors reuse the same definitions. Defining commands inside the body works but limits their scope to where they appear.