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^+}\)."
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$$ |
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^+}}Output:
Fractions
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 $$ |
Definition
\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}}Output:
"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.
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 ...}$$ |
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}Output:
Matrices (\mat
)
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} $$ |
Definition
% Insert a matrix. Usage: \mat{a & b \\ c & d} or \mat[Output:]{a & b \\ c & d} \newcommand{\mat}[2][1]{\begingroup \renewcommand*{\arraystretch}{#1} \begin{bmatrix}#2\end{bmatrix} \endgroup}
"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.
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} $$ |
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 }Output:
"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.
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} $$ |
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 }Output:
"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.
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} $$ |
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 }Output:
"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.
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} $$ |
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 }Output:
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.
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$$ |
Definition
\newcommand{\midand}{\quad \textup{and}\quad}% \newcommand{\midor}{\quad \textup{or}\quad}%Output:
Set Builder Notation
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\} $$ |
Definition
% 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 }Output:
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" ] }