The Power of a Well-Curated Macro Library
Every experienced LaTeX user has a personal collection of macros—shortcuts that eliminate repetitive typing, ensure consistency, and encode best practices. This guide provides a curated set of production-ready macros organized by use case.
Unlike generic tutorials, these macros come from real academic workflows. Each one solves a specific problem you'll encounter when writing papers, theses, or technical documents.
Mathematical Notation Macros
Mathematics is where LaTeX shines, and good macros make it shine brighter.
Vector and Matrix Notation
Different fields use different conventions. Here are the common ones:
% Bold vectors (physics, engineering)
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\mat}[1]{\mathbf{#1}}
% Arrow vectors (some mathematics)
\newcommand{\veca}[1]{\vec{#1}}
% Underline vectors (crystallography)
\newcommand{\vecu}[1]{\underline{#1}}
% Usage:
% The vector \vect{x} is multiplied by matrix \mat{A}.Probability and Statistics
Essential for any data-driven paper:
\usepackage{amsmath}
\usepackage{amssymb}
% Probability
\newcommand{\prob}[1]{\mathbb{P}\left(#1\right)}
\newcommand{\condprob}[2]{\mathbb{P}\left(#1 \,\middle|\, #2\right)}
% Expected value and variance
\newcommand{\expect}[1]{\mathbb{E}\left[#1\right]}
\newcommand{\condexpect}[2]{\mathbb{E}\left[#1 \,\middle|\, #2\right]}
\newcommand{\var}[1]{\text{Var}\left(#1\right)}
\newcommand{\cov}[2]{\text{Cov}\left(#1, #2\right)}
% Distributions
\newcommand{\normal}[2]{\mathcal{N}\left(#1, #2\right)}
\newcommand{\uniform}[2]{\text{Uniform}\left(#1, #2\right)}
% Indicators
\newcommand{\indic}[1]{\mathbf{1}\left\{#1\right\}}
% Usage:
% Let $X \sim \normal{\mu}{\sigma^2}$. Then $\expect{X} = \mu$.
% The conditional probability is $\condprob{A}{B}$.Derivatives and Calculus
Clean, consistent derivative notation:
% Ordinary derivatives
\newcommand{\deriv}[2]{\frac{d #1}{d #2}}
\newcommand{\dderiv}[2]{\frac{d^2 #1}{d #2^2}}
\newcommand{\nderiv}[3]{\frac{d^{#3} #1}{d #2^{#3}}}
% Partial derivatives
\newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\ppderiv}[2]{\frac{\partial^2 #1}{\partial #2^2}}
\newcommand{\pmderiv}[3]{\frac{\partial^2 #1}{\partial #2 \partial #3}}
% Evaluated at
\newcommand{\evalat}[2]{\left. #1 \right|_{#2}}
% Usage:
% \deriv{f}{x}, \pderiv{f}{x}, \evalat{\deriv{f}{x}}{x=0}Sets and Logic
For discrete mathematics and theoretical computer science:
% Common sets
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\C}{\mathbb{C}}
% Set operations
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\setof}[2]{\left\{#1 \,\middle|\, #2\right\}}
% Common operators
\newcommand{\abs}[1]{\left|#1\right|}
\newcommand{\norm}[1]{\left\|#1\right\|}
\newcommand{\floor}[1]{\left\lfloor#1\right\rfloor}
\newcommand{\ceil}[1]{\left\lceil#1\right\rceil}
% Usage:
% For all $x \in \R$, we have $\abs{x} \geq 0$.
% The set $\setof{x \in \N}{x > 5}$ is infinite.Linear Algebra
Common operations and notation:
% Transpose and inverse
\newcommand{\trans}[1]{#1^{\mkern-1.5mu\mathsf{T}}}
\newcommand{\inv}[1]{#1^{-1}}
\newcommand{\pinv}[1]{#1^{\dagger}}
% Trace and determinant
\DeclareMathOperator{\tr}{tr}
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\spn}{span}
% Inner product
\newcommand{\inner}[2]{\langle #1, #2 \rangle}
% Usage:
% For matrix $\mat{A}$, $\trans{\mat{A}}$ is its transpose.
% The inner product is $\inner{\vect{x}}{\vect{y}}$.Text and Document Macros
Common Abbreviations
Properly formatted with correct spacing:
\usepackage{xspace}
% Latin abbreviations (italicized)
\newcommand{\eg}{\textit{e.g.},\xspace}
\newcommand{\ie}{\textit{i.e.},\xspace}
\newcommand{\cf}{\textit{cf.}\xspace}
\newcommand{\viz}{\textit{viz.}\xspace}
\newcommand{\nb}{\textit{N.B.}\xspace}
% Author references
\newcommand{\etal}{\textit{et al.}\xspace}
% Figure/Table references
\newcommand{\figref}[1]{Figure~\ref{#1}}
\newcommand{\tabref}[1]{Table~\ref{#1}}
\newcommand{\secref}[1]{Section~\ref{#1}}
\newcommand{\eqnref}[1]{Equation~\eqref{#1}}
% Usage:
% There are several approaches, \eg neural networks and random forests.
% See \figref{fig:results} for the results.Draft Mode Annotations
Essential during writing and revision:
\usepackage{xcolor}
% Basic annotations
\newcommand{\todo}[1]{\textcolor{red}{\textbf{TODO: #1}}}
\newcommand{\note}[1]{\textcolor{blue}{\textbf{Note: #1}}}
\newcommand{\fixme}[1]{\textcolor{orange}{\textbf{FIXME: #1}}}
% Margin notes
\newcommand{\margintodo}[1]{\marginpar{\footnotesize\textcolor{red}{#1}}}
% Strikethrough for deleted text
\usepackage{soul}
\newcommand{\deleted}[1]{\textcolor{gray}{\st{#1}}}
% Highlighted additions
\newcommand{\added}[1]{\textcolor{blue}{#1}}
% Toggle for final version (comment/uncomment to hide annotations)
% \renewcommand{\todo}[1]{}
% \renewcommand{\note}[1]{}
% \renewcommand{\fixme}[1]{}
% Usage:
% The algorithm \todo{add complexity analysis} runs in linear time.
% \deleted{old approach} \added{new approach}Author-Specific Comments
For collaborative writing:
\usepackage{xcolor}
% Define author colors
\definecolor{alice}{RGB}{0, 100, 180}
\definecolor{bob}{RGB}{180, 60, 0}
\definecolor{carol}{RGB}{0, 130, 60}
% Author-specific comments
\newcommand{\alice}[1]{\textcolor{alice}{[Alice: #1]}}
\newcommand{\bob}[1]{\textcolor{bob}{[Bob: #1]}}
\newcommand{\carol}[1]{\textcolor{carol}{[Carol: #1]}}
% Usage:
% The results show \alice{should we include confidence intervals?}
% a significant improvement \bob{agreed, let's add them}.Environment Shortcuts
Figure Insertion
Streamlined figure placement:
\usepackage{graphicx}
\usepackage{xparse}
\NewDocumentCommand{\insertfig}{O{htbp} O{0.8\textwidth} m m m}{%
% #1 = placement (default htbp)
% #2 = width (default 0.8\textwidth)
% #3 = filename
% #4 = caption
% #5 = label
\begin{figure}[#1]
\centering
\includegraphics[width=#2]{#3}
\caption{#4}
\label{#5}
\end{figure}
}
% Usage:
% \insertfig{diagram.pdf}{A system diagram}{fig:system}
% \insertfig[t][0.6\textwidth]{chart.pdf}{Performance chart}{fig:perf}Side-by-Side Figures
Common in comparative analyses:
\usepackage{graphicx}
\usepackage{subcaption}
\newcommand{\twofigs}[6]{%
% #1, #2 = filenames
% #3, #4 = subcaptions
% #5 = main caption
% #6 = label
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\textwidth]{#1}
\caption{#3}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.48\textwidth}
\centering
\includegraphics[width=\textwidth]{#2}
\caption{#4}
\end{subfigure}
\caption{#5}
\label{#6}
\end{figure}
}
% Usage:
% \twofigs{before.png}{after.png}{Before}{After}{Comparison of methods}{fig:compare}Quick Lists
For frequently-used list patterns:
\usepackage{enumitem}
% Compact numbered list
\newenvironment{numlist}{%
\begin{enumerate}[noitemsep, topsep=0pt]
}{%
\end{enumerate}
}
% Compact bullet list
\newenvironment{bulletlist}{%
\begin{itemize}[noitemsep, topsep=0pt]
}{%
\end{itemize}
}
% Inline list (horizontal)
\newcommand{\inlinelist}[1]{%
\begin{enumerate*}[label=(\roman*)]
#1
\end{enumerate*}
}Domain-Specific Macro Collections
For Physics Papers
\usepackage{siunitx}
\usepackage{physics} % Provides many physics macros
% Physical quantities with units
\newcommand{\qty}[2]{#1\,\si{#2}}
% Common physics notation
\newcommand{\bra}[1]{\langle #1 |}
\newcommand{\ket}[1]{| #1 \rangle}
\newcommand{\braket}[2]{\langle #1 | #2 \rangle}
% Order of magnitude
\newcommand{\order}[1]{\mathcal{O}\left(#1\right)}
% Usage:
% The energy is \qty{5.2}{eV}.
% The state $\ket{\psi}$ evolves as $\braket{\phi}{\psi}$.For Computer Science Papers
% Complexity classes
\newcommand{\bigO}[1]{\mathcal{O}\left(#1\right)}
\newcommand{\bigTheta}[1]{\Theta\left(#1\right)}
\newcommand{\bigOmega}[1]{\Omega\left(#1\right)}
% Algorithms
\newcommand{\algname}[1]{\textsc{#1}}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\funcname}[1]{\texttt{#1}()}
% Common operators
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator{\softmax}{softmax}
% Usage:
% The \algname{QuickSort} algorithm runs in $\bigO{n \log n}$ expected time.
% Call \funcname{initialize} before processing.For Economics Papers
% Utility and optimization
\DeclareMathOperator*{\maximize}{maximize}
\DeclareMathOperator*{\minimize}{minimize}
\newcommand{\st}{\text{subject to}\quad}
% Game theory
\newcommand{\nash}{\text{Nash Equilibrium}}
\newcommand{\strategy}[1]{\mathcal{S}_{#1}}
% Elasticity
\newcommand{\elasticity}[2]{\varepsilon_{#1,#2}}
% Usage:
% $\maximize_{x} f(x) \st g(x) \leq c$Organizing Your Macro Library
Project-Specific vs. Universal Macros
Organize macros into categories:
Universal macros (use in every project):
- Text abbreviations (\eg, \ie, \etal)
- Basic math operators (\abs, \norm)
- Reference shortcuts (\figref, \tabref)
Domain-specific macros (use in related projects):
- Physics notation packages
- Statistics notation packages
- Computer science notation packages
Project-specific macros (use in one document):
- Your method's name
- Paper-specific notation
- Draft annotations
Creating Reusable Packages
Save universal macros in a .sty file:
% ~/texmf/tex/latex/personal/mymacros.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mymacros}[2024/01/01 Personal Macro Collection]
\RequirePackage{xspace}
\RequirePackage{amsmath}
\RequirePackage{amssymb}
% ... macro definitions ...
\endinputThen use in any document:
\usepackage{mymacros}Best Practices for Macro Design
1. Use Semantic Names
% Good: name describes the concept
\newcommand{\gradient}[1]{\nabla #1}
% Bad: name describes the appearance
\newcommand{\upsidedowntriangle}[1]{\nabla #1}2. Document Your Macros
% \deriv{numerator}{denominator}
% Produces a fraction for ordinary derivatives.
% Example: \deriv{f}{x} produces df/dx
\newcommand{\deriv}[2]{\frac{d #1}{d #2}}3. Make Macros Robust When Needed
\usepackage{etoolbox}
\robustify{\mycommand} % Prevents issues in captions/titles4. Test Edge Cases
Before using a new macro extensively, test it with:
- Empty arguments
- Long arguments
- Special characters
- In math mode and text mode
- In captions and section titles
Conclusion
A well-crafted macro library is an investment that pays dividends throughout your academic career. Start with the essentials—mathematical notation and text shortcuts—then expand as you identify patterns in your writing.
The macros in this guide are battle-tested across dissertations, journal papers, and technical reports. Copy what you need, adapt to your conventions, and build from there.
Remember: the best macro library is the one you'll actually use. Start small, stay organized, and iterate.
Put these macros to work in Thetapad—compile locally, see instant previews, and keep your documents private.