3 min read

Categories

An abstract digital art painting of a macro instruction in computer science
Generated using DALL-E-2 with the prompt "An abstract digital art painting of a macro instruction in computer science"

Contents

Introduction

LaTeX is a powerful typesetting system widely used in academia, particularly in mathematics and sciences. When writing a document that involves repetitive notation, it can become tiresome to repeatedly write complex symbols or subscripts. In this blog post, we’ll explore how to define convenient shortcuts using commands in LaTeX, making your notation more concise and readable.

LaTeX Syntax for Defining Commands

LaTeX provides a powerful syntax for defining commands that allows you to create shortcuts and customize your document’s notation. One commonly used command for defining macros is \newcommand.

The syntax of \newcommand is as follows:

\newcommand{\name}[num]{definition}

Let’s break down each component of the syntax:

  • \newcommand: This is the command used to define a new command in LaTeX.

  • {\name}: This is the name you choose for your command. It should be unique and follow LaTeX’s rules for naming conventions. The name starts with a backslash \ and it can only contain letters (a-z, A-Z). Note that LaTeX is case-sensitive, so \name and \Name are different commands. The curly braces {} are optional.

  • [num]: This optional argument specifies the number of arguments the command takes. You can specify up to 9 arguments for your command so num can be any number from 1 to 9. If your command does not take any arguments, you can omit this part.

  • {definition}: This is the definition of your command, which specifies the action it performs. It can include any valid LaTeX code, such as symbols, text, or other commands. You can also refer to the arguments of your command using #1, #2, and so on, corresponding to the order of the arguments.

Using \newcommand in your LaTeX Document

To define a command, you would typically include the \newcommand syntax in the preamble of your LaTeX document, before the \begin{document} command. This ensures that your command is available throughout the document.

Once defined, you can use your command by invoking its name followed by any required arguments within the body of your document. LaTeX will replace the command with the specified definition whenever it encounters it.

For example if you define $\newcommand{\half}{\frac{1}{2}}, then every time you write \half in your document, it will be replaced by $\frac{1}{2}$.

You can also use \newcommand when incorporating LaTeX in markdown documents and in markdown cells in Jupyter notebooks. By placing your \newcommand definitions in the top of the document, they will be available throughout the document.

Now that we’ve covered the basics of \newcommand, let’s explore some examples of how to use it to simplify your notation.

Constants

Let’s consider a common scenario: you frequently use the symbol $\alpha_{t-1}$ (written as \alpha_{t-1}) in your blog post. To avoid rewriting it each time, it’s convenient to define a shorthand. For instance, we can introduce the command \atmo, representing “alpha t minus one”. By defining

\newcommand{\atmo}{\alpha_{t-1}}

you can now simply use \atmo throughout your document instead of writing the entire expression every time.

Functions with Single Variables

However, your blog post might involve other subscripts for the symbol $\alpha$, such as $\alpha_0, \alpha_t, \alpha_\tau$ (\alpha_0, \alpha_t, \alpha_\tau). It would be useful to define a more general command \at that accepts a subscript as an argument. This would be a command with a single argument

\newcommand{\at}[1]{\alpha_{#1}}

You can now call \at{0} to produce $\alpha_0$, \at{t} for $\alpha_t$, \at{\tau} for $\alpha_\tau$, and so on.

Furthermore, commands can be nested within other commands. So the command \atmo which we defined earlier can be rewritten as

\newcommand{\atmo}{\at{t-1}}

Functions with Many Variables

Now, let’s consider a scenario where your blog post involves multiple symbols, such as $\alpha$ and $\beta$. To handle this, we can define a more versatile command \ft that takes two arguments: a symbol and a subscript. For instance, \newcommand{\ft}[2]{#1_{#2}} allows you to call \ft{\alpha}{t} to produce $\alpha_{t}$ or \ft{\beta}{t} for $\beta_t$.

Commands with multiple arguments can also be called within other commands. Consequently, you can define \at and \bt in terms of \ft. For example, by fixing the first argument of \ft to be $\alpha$ and $\beta$, respectively, you can now define

\newcommand{\at}[1]{\ft{\alpha}{#1}}
\newcommand{\bt}[1]{\ft{\beta}{#1}}

enabling you to write \at{t-1} for $\alpha_{t-1}$ and \bt{t-1} for $\beta_{t-1}$.

Conclusion

By leveraging the power of LaTeX commands, you can significantly simplify and streamline the notation used in your blog post. This can improve readability and clarity of your mathematical expressions, reduce repetitive typing, and make your LaTeX code more maintainable. Additionally, it allows you to create consistent and standardised notation across your document.