LaTeX Macros

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

Arguments of the Minima and Maxima (\argmin and \argmax)

Macro Code
% Define "\argmax" and "\argmin" macros.
% These definitions require the amsmath package, 
% which provides "\DeclareMathOperator*".
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

The macros \argmin and \argmax have the following features:

  1. Correct spacing depending on the surrounding context (e.g., "$\operatorname{arg\hspace{1mu}min}h(x)$" instead of "$\mathrm{arg\hspace{3mu}min}h(x)$").
  2. Always uses the upright mathematics font (they will not become italicized when used inside a theorem body).
  3. Correct placement of limits, e.g., \argmin_{x > 0} is rendered as $\operatorname{arg\,min}_{x > 0}$ in inline equations, whereas in display equations it is rendered as $$\displaystyle\operatorname*{arg\hspace{1mu}min}_{x > 0}$$
  4. a slight space is included between "arg" and "min".
As a side note, $\operatorname{arg\hspace{1mu}min} f$ (and $\operatorname{arg\hspace{1mu}max} f$) is a set, since there is not, in general, $f$ can have multiple minimizing arguments. Consider, for example, $(x, y) \mapsto f(x, y) := x^2$. Then, $$\operatorname{arg\hspace{1mu}min} f = \{(0, y) : y \in \reals\}$$ Thus, in general, one should write $x \in \operatorname{arg\,min} f$ instead of $x = \operatorname{arg\,min} f$ unless $f$ is known to be minimized at a unique point (e.g., if $f$ is strictly convex).

Examples

Code Output
$\argmin_x f(x)$
$$\operatorname{arg\hspace{1mu}min}_{x} f(x)$$
\[
  \argmax_x f(x)
\]
$$\operatorname*{arg\hspace{1mu}min}_{x} f(x)$$

Limits

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

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^+}\)."

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

Macro Code
\newcommand{\fracshort}[2]{\left.#1 \middle/ #2\right.}
\newcommand{\oneover}[1]{\frac{1}{#1}}
\newcommand{\doneover}[1]{\dfrac{1}{#1}} % \displaystyle fraction
\newcommand{\oneovershort}[1]{\fracshort{1}{#1}}
% Common Fractions
\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{\eighth} [1][1]{\frac{#1}{8}}
\newcommand{\tenth}  [1][1]{\frac{#1}{10}}
\newcommand{\twelfth}[1][1]{\frac{#1}{12}}
% Short fractions for inline equations.
\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{\eighthshort} [1][1]{\fracshort{#1}{8}}
\newcommand{\tenthshort}  [1][1]{\fracshort{#1}{10}}
\newcommand{\twelfthshort}[1][1]{\fracshort{#1}{12}}
% Display style fractions.
\newcommand{\dhalf}   [1][1]{\dfrac{#1}{2}}
\newcommand{\dthird}  [1][1]{\dfrac{#1}{3}}
\newcommand{\dquarter}[1][1]{\dfrac{#1}{4}}
\newcommand{\dfifth}  [1][1]{\dfrac{#1}{5}}
\newcommand{\dsixth}  [1][1]{\dfrac{#1}{6}}
\newcommand{\deighth} [1][1]{\dfrac{#1}{8}}
\newcommand{\dtenth}  [1][1]{\dfrac{#1}{10}}
\newcommand{\dtwelfth}[1][1]{\dfrac{#1}{12}}

This collection of LaTeX macros make it easier to insert fractions, including defining a \fracshort macro that causes a fraction to be rendered like $a/b$ (making it easier to switch between $\frac{a}{b}$ and $a/b$). The slash inserted by \fracshort scales with the height of the numerator and denominator. Also included are common fractions, $\frac{1}{2}$ (\half), $\frac{1}{3}$ (\third), etc. Each of these commands can take one optional argument that sets the numerator to the given values, such as \half[x] ($\frac{x}{2}$), and have short versions (\quartershort[\theta] creates $\theta/4$) and a display style version (e.g., \dtenth[n] creates $\dfrac{n}{10}$, but you should never use display style fractions in a line of text). Finally, the \oneover, \oneovershort, and \doneover macros inserts fractions with $1$ in the numerator and the denominator given as an argument: \oneover{x} creates $\frac{1}{x}$.

When using the short fractions, make sure you insert parentheses, as needed, around the numerator and denominator!

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} $$
\thirdshort[a] + \fourthshort[b] 
$$a/3 + b/4 $$

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

Macro Code
\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}

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.

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)

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

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

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)

Macro Code
% 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
}

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.

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)

Macro Code
% 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
}

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.

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)

Macro Code
% 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
}

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.

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)

Macro Code
% 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
}

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.

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

Macro Code
\newcommand{\midand}{\quad \textup{and}\quad}%
\newcommand{\midor}{\quad \textup{or}\quad}%

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.

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$$

Optimization Problems (\minimize, \maximize, \subjectto)

Macro Code
% ╭──────────────────────────────────────────────╮
% │             Optimization Problems            │
% ╰──────────────────────────────────────────────╯
% Requires the amsmath package, which provides 
% "\DeclareMathOperator" and "\DeclareMathOperator*".
% Example usage:
%   \begin{align*}
%       \minimize_{x \in \reals^n} \quad  & f(x)      \\
%       \subjectto                 \quad  & Ax \leq 0 \\
%                                         & Bx = 0.
%   \end{align*}
\DeclareMathOperator*{\minimize}{minimize}
\DeclareMathOperator*{\maximize}{maximize}
\DeclareMathOperator {\subjectto}{subject~to}
% Aliases for \maximize and \minimize with British spelling.
\let\minimise=\minimize
\let\maximise=\maximize

I define \minimize, \maximize, and \subjectto as math operators using the amsmath package's macros \DeclareMathOperator and \DeclareMathOperator* (the resulting macros are equivalent to placing \operatorname*{minimize} in the body of your document). Defining the macros as math operators ensures that the font matches the roman math font (cf. using \text{minimize} which will become italicized when used in the body of a theorem), and gives us control over the placement of "limits" on the text, e.g., "\minimize_{x \in \reals}". In particular, using \DeclareMathOperator* instead of \DeclareMathOperator causes subscripts to be placed below the text in display equations, so \[\minimize_{x \in \reals}\] is rendered as $$\operatorname*{minimize}_{x \in \reals}$$ instead of $$\operatorname{minimize}_{x \in \reals}.$$ For inline equations, limits are still placed to the side, e.g., "$\operatorname*{minimize}_{x \in \reals}$".

For \subjectto, we use a fixed-width nonbreaking space "~" in \operatorname to preserve the space in the name, since \operatorname gobbles regular spaces.

See “Minimize” vs. “Min” in Optimization Problems for discussion of it is better to not use "\min" when writing optimization problems.

For the Britishly-inclined, I have included aliases \minimise and \maximise. Depending on the publication venue, you can change the spelling in the definitions of "minimize" and "maximize", but to enforce consistency, I advise against defining macros that insert different variants of the spelling.

Examples

Code Output
\begin{align*}
    \minimize_{x \in \reals^n} \quad & f(x)      \\
    \subjectto                 \quad & Ax \leq 0 \\
                                     & Bx = 0.
\end{align*}
$$\begin{align*} \operatorname*{minimize}_{x \in \reals^n} \quad & f(x) \\ \operatorname{subject~to} \quad & Ax \leq 0 \\ & Bx = 0. \end{align*}$$

Set Builder Notation

Macro Code
% 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.
\newcommand*{\setdef}[1]{
    \begingroup
        \def\or{\textup{ or }}%
        \def\and{,\ }%  
        \left\{#1 \right\}%
    \endgroup
} 
\newcommand{\suchthat}{%
    \ifnum\currentgrouptype=16
        % If the command is used between "\left" and "\right", 
        % then use "\middle" to make "|" match the height of 
        % the delimiters.
        \mathrel{}\middle|\mathrel{}
    \else
        % Otherwise, we just insert "|" with the spacing of a 
        % math relation.
        \mathrel{|}
    \fi
}

I created macros to simplify writing sets with set builder notation, so that "\setdef{x \suchthat x < 0}" produces \(\{x \mathrel{|} x < 0\}\). The heights of the braces and center bar automatically adjust to the height of the contents of the braces, and the center bar has appropriate spacing on either side.

Set definitions often include conjunctions ("and"s) and disjunctions ("or"s), so we define \and and \or so they can be used within the argument of \setdef to create nicely formatted logical statements. I used the convention that "or" is written out, but "and" is represented by a comma. To use \(\vee\) for "or" and \(\wedge\) for "and", modify the definitions to \def\or{\vee} and \def\and{\wedge}.

Examples

Code Output
\setdef{x \suchthat P(x) \or Q(x)}
$$\left\{x \mid P(x) \text{ or } Q(x) \right\} $$
\setdef{x \suchthat \frac{1}{1+x^2} = 1 \and x > 0}
$$\left\{x \mathrel{}\middle|\mathrel{} \frac{1}{1+x^2} = 1,\ x > 0\right\} $$
\setdef{A, B, C}
$$\left\{A, B, C\right\} $$
VS Code Snippets In VS Code, the following code can be added to the LaTeX snippet file to enable nice autocompletion of the macro:
"Set builder notation":{
  "prefix": ["\\setbuild"],
  "body": [
    "\\setdef{$TM_SELECTED_TEXT${1:x} \\suchthat $2} $0"
  ]
},
"Set definition":{
  "prefix": ["\\setdef"],
  "body": [
    "\\setdef{$TM_SELECTED_TEXT$1} $0"
  ]
}