LaTeX Macros

This page lists LaTeX commands (or "macros") that can save you time while typesetting documents.

Limits

Typesetting long calculations involving limits quickly become tedious because the same limit expression (such as "\(\lim_{i \to \infty}\)") appears in each in each step of the calculation—sometimes multiple times!—until the limit is fully evaluated. Any time an expression appears repeatedly, you should consider introducing a macro. In particular, when the input variable for a limit is written as \(i\), \(j\), or \(k\), then the variable almost always is integer index that goes to \(\infty\). Thus, we introduce macros to abbreviate the corresponding limit expressions. Similarly, \(h\) is commonly used as an distance that goes to zero, such as in the definition of the derivative, so we define a macro to insert "\(\lim_{h \to 0^+}\)."

Definition
\newcommand{\jlim}{\lim_{j \to \infty}}
\newcommand{\ilim}{\lim_{i \to \infty}} 
\newcommand{\klim}{\lim_{k \to \infty}}
\newcommand{\hlim}{\lim_{h \to 0^+}}

Examples

Code Output
\ilim 1/i = 0
$$\lim_{i \to \infty} 1/i = 0$$
\jlim 1/j = 0
$$\lim_{j \to \infty} 1/j = 0$$
\klim 1/k = 0
$$\lim_{k \to \infty} 1/k = 0$$
\hlim \frac{f(x + h) - f(x)}{h} = 0
$$\lim_{h \to 0^+} \frac{f(x + h) - f(x)}{h} = 0$$

Fractions

These macros make it easier to insert fractions that have 2, 3, 4, 5, 6, or 12 as the denominator. A "short" version of each command is included. The short versions separate the numerator and denominator with a slash instead of a horizontal line. For general short fractions, use `\fracshort`.

Definition
\newcommand{\fracshort}[2]{\left.#1 \:\middle/\: #2\right.}
\newcommand{\half}[1][1]{\frac{#1}{2}}
\newcommand{\third}[1][1]{\frac{#1}{3}}
\newcommand{\quarter}[1][1]{\frac{#1}{4}}
\newcommand{\fifth}[1][1]{\frac{#1}{5}}
\newcommand{\sixth}[1][1]{\frac{#1}{6}}
\newcommand{\twelfth}[1][1]{\frac{#1}{12}}
\newcommand{\halfshort}[1][1]{\fracshort{#1}{2}}
\newcommand{\thirdshort}[1][1]{\fracshort{#1}{3}}
\newcommand{\quartershort}[1][1]{\fracshort{#1}{4}}
\newcommand{\fifthshort}[1][1]{\fracshort{#1}{5}}
\newcommand{\sixthshort}[1][1]{\fracshort{#1}{6}}
\newcommand{\twelfthshort}[1][1]{\fracshort{#1}{12}}

Examples

Code Output
\fracshort{a}{b}
$$a/b $$
\fracshort{1}{\left(1 + e^{x^{-2}}\right)}
$$\left. 1 \middle/ \left(1 + e^{x^{-2}}\right) \right. $$
\half
$$\frac{1}{2} $$
\half[x]
$$\frac{x}{2} $$
\third \fourth \fifth \sixth \twelfth
$$\frac{1}{3}\frac{1}{4}\frac{1}{5}\frac{1}{6}\frac{1}{12} $$
\third[a] \fourth[b] \fifth[c] \sixth[d] \twelfth[e]
$$\frac{a}{3}\frac{b}{4}\frac{c}{5}\frac{d}{6}\frac{e}{12} $$

"Left-hand Side" and "Right-hand Side"

The command \lhs inserts "left-hand side" or "L.H.S." depending on the context. Similarly, \rhs inserts "right-hand side" or "R.H.S.". When used in text, the macros generate the spelled out text (i.e., "left-hand side"). In math mode, the acronym abbreviation is used.

Definition
\usepackage{xspace}
% Define \lhs for "left-hand side" in text or "L.H.S." in math. 
\newcommand{\lhs}{\relax\ifmmode\mathrm{L.H.S.}\else{}left-hand side\xspace\fi}
% Define \rhs for "right-hand side" in text or "R.H.S." in math. 
\newcommand{\rhs}{\relax\ifmmode\mathrm{R.H.S.}\else{}right-hand side\xspace\fi}

Examples

Code Output
To simplify the \lhs, we ...
$$\text{To simplify the left-hand side, we ...}$$
To simplify the \rhs, we ...
$$\text{To simplify the right-hand side, we ...}$$
To simplify the $\lhs$, we ...
$$\text{To simplify the L.H.S., we ...}$$
To simplify the $\rhs$, we ...
$$\text{To simplify the R.H.S., we ...}$$

Matrices (\mat)

Shortcut for inserting block matrices with an option for adjusting the vertical scale.

Definition
% Insert a matrix. Usage: \mat{a & b \\ c & d} or \mat[<vertical spacing>]{a & b \\ c & d}
\newcommand{\mat}[2][1]{\begingroup
  \renewcommand*{\arraystretch}{#1}
  \begin{bmatrix}#2\end{bmatrix}
\endgroup}

Examples

Code Output
\mat{1 \\ 2}
$$\begin{bmatrix} 1 \\ 2 \end{bmatrix} $$
\mat[3]{1 \\ 2}
$$\begin{bmatrix} \\ 1\\ \\ 2 \\ \ \end{bmatrix} $$

"Memory" Integral (\memint)

When evaluating an integral, often the integral symbol \(\int\) appears with the same limits repeatedly. A "memory" command \memint allows for the limits to be typed once when the integral first appears and omitted thereafter. In particular, there are two versions of \memint: a starred version \memint*[<lower limit>][<upper limit>] records the lower limit <lower limit> and the upper limit <lower limit> into memory. From then on, the unstarred version \memint will insert a integral with the recorded limits. In addition to saving time typing, \memint simplifies the LaTeX code, so it is easier to edit and find mistakes.

WARNING: Be careful while using this command because each time the starred version is called, it changes the definition for all of the unstarred versions until the next starred version. Thus, if you add \memlim* into the middle of text where you are already using \memlim with a different definition, you can unintentionally change the rendered equations. For this reason, I restrict the usage of each remembered command to a single equation.

Definition
% The 'xparse' package provides \NewDocumentCommand
\usepackage{xparse}
\NewDocumentCommand{\memint}{sO{}O{}}{%
    \IfBooleanT{#1}%
    {% If a star
        % "\gdef" is used to define a global macro.
        \gdef\memintlimits{_{#2}^{#3}}%
    }
    \int\memintlimits
}

Examples

Code Output
\memint*[1][\infty] \frac{1}{x} 
= \memint \frac{1}{x}
$$\begin{aligned} \int_{1}^{\infty} \frac{1}{x} &= \int_{1}^{\infty} \frac{1}{x} \end{aligned} $$

"Memory" Limit (\memlim)

When evaluating an limit, often the limit expression (e.g., \(\lim_{x \to x_0}\)) repeatedly. A "memory" command \memlim allows for the full limit expression to be typed once when it first appears and abbreviated thereafter. In particular, there are two versions of \memlim: a starred version \memlim*[<lower>] records the lower expression <lower> into memory. From then on, the unstarred version \memlim will insert a limit with the recorded lower expression. In addition to saving time typing, \memlim simplifies the LaTeX code, so it is easier to edit and find mistakes.

WARNING: Be careful while using this command because each time the starred version is called, it changes the definition for all of the unstarred versions until the next starred version. Thus, if you add \memlim* into the middle of text where you are already using \memlim with a different definition, you can unintentionally change the rendered equations. For this reason, I restrict the usage of each remembered command to a single equation.

Definition
% The 'xparse' package provides \NewDocumentCommand
\usepackage{xparse}
\NewDocumentCommand{\memlim}{sO{}}{%
    \IfBooleanT{#1}%
    {% If a star
        % "\gdef" is used to define a global macro.
        \gdef\memlimsubscript{_{#2}}%
    }
    \lim\memlimsubscript
}

Examples

Code Output
\memlim*[x_0 \to 5] 
  \frac{(x+1)(x-5)}{(x-2)(x-5)} 
    = \memlim \frac{x+1}{x-2} 
    = 2
$$\begin{aligned} \lim_{x_0 \to 5} &\frac{(x+1)(x-5)}{(x-2)(x-5)} \\ &= \lim_{x_0 \to 5} \frac{x+1}{x-2} \\ &= 2 \end{aligned} $$

"Memory" Product (\memprod)

When evaluating a product, often the product symbol \(\prod\) appears with the same limits repeatedly. A "memory" command \memprod allows for the limits to be typed once when the product first appears and omitted thereafter. In particular, there are two versions of \memprod: a starred version \memprod*[<lower limit>][<upper limit>] records the lower limit <lower limit> and the upper limit <lower limit> into memory. From then on, the unstarred version \memprod will insert a product with the recorded limits. In addition to saving time typing, \memprod simplifies the LaTeX code, so it is easier to edit and find mistakes.

WARNING: Be careful while using this command because each time the starred version is called, it changes the definition for all of the unstarred versions until the next starred version. Thus, if you add \memprod* into the middle of text where you are already using \memprod with a different definition, you can unintentionally change the rendered equations. For this reason, I restrict the usage of each remembered command to a single equation.

Definition
% The 'xparse' package provides \NewDocumentCommand
\usepackage{xparse}
\NewDocumentCommand{\memprod}{sO{}O{}}{%
    \IfBooleanT{#1}%
    {% If a star
        % "\gdef" is used to define a global macro.
        \gdef\memprodlimits{_{#2}^{#3}}%
    }
    \prod\memprodlimits
}

Examples

Code Output
\memprod*[n=1][\infty] \frac{1}{n} 
= \memprod \frac{1}{n}
$$\begin{aligned} \prod_{n=1}^{\infty} \frac{1}{n} &= \prod_{n=1}^{\infty} \frac{1}{n} \end{aligned} $$

"Memory" Summation (\memsum)

When evaluating an summation, often the summation symbol \(\sum\) appears with the same limits repeatedly. A "memory" command \memsum allows for the limits to be typed once when the summation first appears and omitted thereafter. In particular, there are two versions of \memsum: a starred version \memsum*[<lower limit>][<upper limit>] records the lower limit <lower limit> and the upper limit <lower limit> into memory. From then on, the unstarred version \memsum will insert a summation with the recorded limits. In addition to saving time typing, \memsum simplifies the LaTeX code, so it is easier to edit and find mistakes.

WARNING: Be careful while using this command because each time the starred version is called, it changes the definition for all of the unstarred versions until the next starred version. Thus, if you add \memsum* into the middle of text where you are already using \memsum with a different definition, you can unintentionally change the rendered equations. For this reason, I restrict the usage of each remembered command to a single equation.

Definition
% The 'xparse' package provides \NewDocumentCommand
\usepackage{xparse}
\NewDocumentCommand{\memsum}{sO{}O{}}{%
    \IfBooleanT{#1}%
    {% If a star
        % "\gdef" is used to define a global macro.
        \gdef\memsumlimit{_{#2}^{#3}}%
    }
    \sum\memsumlimit
}

Examples

Code Output
\memsum*[n=1][\infty] \frac{1}{n} 
= \memsum \frac{1}{n}
$$\begin{aligned} \sum_{n=1}^{\infty} \frac{1}{n} &= \sum_{n=1}^{\infty} \frac{1}{n} \end{aligned} $$

Mid-line "And" and "Or" in Display Equations

For display-style equations that contains a list of equations, I found it tedious to write \quad \text{and} \quad before the last item in the list. Instead, I defined a \midand macro.

Definition
\newcommand{\midand}{\quad \textup{and}\quad}%
\newcommand{\midor}{\quad \textup{or}\quad}%

Examples

Code Output
x \geq -1 \midand x \leq 1
$$x \geq -1 \quad \text{and} \quad x \leq 1$$
x \leq -1, \quad x = 0, \midor x \geq 1
$$x \leq -1, \quad x = 0, \quad \text{or} \quad x \geq 1$$

Set Builder Notation (\setdef and \suchthat)

To insert a set \({A | B}\) (using set builder notation), type "\setdef{A \suchthat B}". The heights of the braces and center bar will adjust automatically to the height of the contents.

Definition
\newcommand*{\setdef}[1]{\left\{#1 \right\}} 
\newcommand{\suchthat}{\mathrel{}\ifnum\currentgrouptype=16 \middle\fi|\mathrel{}}

Examples

Code Output
\setdef{A \suchthat B}
$$\left\{A \mid B\right\} $$
\setdef{x \suchthat \frac{1}{1+x} = 0}
$$\left\{x \mathrel{}\middle|\mathrel{} \frac{1}{1+x} = 0\right\} $$
\setdef{A, B, C}
$$\left\{A, B, C\right\} $$