How to use ‘\newcommand’ in LaTeX?

The \newcommand command in LaTeX allows you to define your custom commands. Replacing long or frequently used text snippets with a shorter, easier-to-remember command can save you time and effort. This guide will walk you through the basics of \newcommand and its various features.

What is ‘\newcommand’?

\newcommand is a LaTeX macro that lets you create a new command with a specific definition. This definition can be:

  • Text: You can define your command to insert a specific text block automatically.
  • LaTeX code: You can define your command to execute a sequence of LaTeX commands. This can be useful for frequently used formatting patterns or complex expressions.
  • Parameters: Your command can take optional or mandatory arguments, allowing for dynamic content based on the provided input.

How to use \newcommand?

Basic Syntax

The basic syntax for \newcommand is as follows:

\newcommand{\commandname}[numargs][option]{definition}

\newcommand: This is the command that tells LaTeX you want to define a new command.

commandname: This is the name of your new command. It should start with a backslash (\), followed by a combination of lowercase and/or uppercase letters, or alternatively, a backslash followed by a single non-letter symbol. Choose a unique name for your command.

numargs: The number of arguments your command takes. It can take any integer values from 1 to 9. If not included, the command will be devoid of any arguments. (optional)

option: If this condition exists, the initial argument becomes optional, with a default value specified at this point. In the absence of this condition, all arguments are mandatory. (optional)

definition: This is the actual content of your command. It can be text, LaTeX code, or a combination of both. You can use the # symbol followed by the argument number (e.g., #1, #2, and so on) to refer to the arguments within the definition.

Where to write this command?

If you want your command to be available throughout the document, define it in the preamble (before \begin{document}).

Example

Let’s create a simple command that formats a person’s name in bold:

\newcommand{\person}[1]{\textbf{#1}}
  • \person: The name of the command.
  • [1]: The command takes one argument.
  • \textbf{#1}: The definition makes the argument bold.

Now you can use your \person command in the document:

\documentclass{article}

\newcommand{\person}[1]{\textbf{#1}}

\begin{document}
Hello, \person{John}! Welcome to electricalvoice.com
\end{document}

Output

Let’s look at another example.

\documentclass{article}

\newcommand{\eg}{\textbf{e.g.}} % Defines a new command for "e.g. or exempli gratia or for example in bold"
\newcommand{\ie}{\textbf{i.e.}} % Defines a new command for "i.e. or that is" in bold

\begin{document}
In many metropolitan areas, public transportation systems, \ie, buses, trains, and subways, play a crucial role in daily commuting. Residents often rely on these services to navigate the city efficiently. However, not all cities have comprehensive public transportation options. For instance, in smaller towns or rural areas, access to reliable public transportation may be limited, \eg, there might be only a few bus routes connecting key locations. This discrepancy in transportation infrastructure highlights the diverse challenges faced by urban and non-urban populations.
\end{document}

Output

Multiple Arguments in ‘\newcommand’

You can define commands with multiple arguments. For example,

\newcommand{\greet}[2]{Hello, #1 and #2!}

Now, look at the above command’s usage with two arguments in an example.

\documentclass{article}

\newcommand{\greet}[2]{Hello, #1 and #2!}

\begin{document}
\greet{Alice}{Bob} Welcome to electricalvoice.com
\end{document}

Output

Let’s look at another example.

\documentclass{article}

\newcommand{\earea}[2]{#1 \times #2} % Defines a command for product of two arguments

\begin{document}
The product of x and y is given by $\earea{x}{y}$
\end{document}

Output

Let’s look at an example in which it defines a command to format a fraction with specific spacing.

\documentclass{article}

\newcommand{\fractionformat}[2]{\frac{#1 \hspace{1mm}}{#2}}

\begin{document}
This is a $\fractionformat{1}{2}$ fraction.
\end{document}

This command takes two arguments (#1 and #2) and formats them as a fraction with a small space between the numerator and denominator.

Output

Tips

  • Choose clear and concise command names: Use easily recognizable names and reflect the command’s purpose.
  • Use arguments effectively: Define arguments for commands that require specific information or variation.
  • Document your commands: Add comments explaining your custom commands for future reference.
  • Use \newcommand strategically to target repetitive tasks and complex formatting.
  • You can use \renewcommand to redefine an existing command, including those defined by LaTeX. Be cautious, as it can break your document if used incorrectly.

That’s a basic guide to using \newcommand in LaTeX. By harnessing the potential of \newcommand, you transform LaTeX from a mere typesetting tool into a personal workspace customized to your needs. So, unleash your creativity, define shortcuts, and ease document creation.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.