Automating Multi-Line Math Displays With Compatible Packages
The process of formatting mathematical expressions and equations for academic papers and technical documents has traditionally required tedious manual work to adjust the visual styling and layout. However, with the right combination of LaTeX packages like amsmath, mathtools, and compatible extensions, authors can automate much of the display math typesetting workflow for multi-line equations, matrices, piecewise functions, and other complex constructs needed to convey mathematical ideas.
This article provides an overview of complementary packages that allow authors to streamline the formatting of advanced math displays without conflicts. We discuss basic usage and more advanced customization techniques for automating math typesetting with align environments, manual spacing adjustment, automated delimiter resizing, and tools for troubleshooting package clashes.
The Tedium of Manual Display Math Formatting
Theprocess of formatting professional mathematical expressions requires aligning elements into visually clear multi-line arrangements that convey relationships and meaning at a glance. However, basic math environments provided natively in LaTeX such as equation and eqnarray have limited capabilities for automating the alignment and layout of complex multipart displays.
Without the right packages, authors face tedious manual work adjusting column alignment, spacing around operators, delimiter sizes, and matrix styling to achieve well-structured multi-line equations, piecewise definitions, theorem statements,and other display math formatting needs in academic papers or technical documentation.
Leveraging amsmath for Automation
The amsmath package provides essential tools that form the foundation for streamlining display math typesetting tasks that would otherwise need to be done manually. Many extensions like mathtools build directly upon amsmath's core environments and commands.
Basic amsmath Usage
To activate amsmath features, simply include \usepackage{amsmath} in the preamble. This opens up access to dozens of new environments like align, gather, and multline for automating the layout and alignment of multi-line equations with separation into lefthand sides and righthands sides for equal signs.
Multi-line Equations with align
The align environment is one of the most versatile for typesetting professional multi-line math displays. The & operator separates alignment points across each line, ensuring elements match up vertically from one row to the next. For example, an equation for projectile motion analysis split over three lines:
\begin{align} v_x(t) &= v_{x0} \\ v_y(t) &= v_{y0} - gt\\ y(t) &= y_0 + v_{y0}t - \frac{1}{2}gt^2 \end{align}
Which formats neatly as:
The equation number is automatically placed for reference, spacing around relation operators =, - , + is adjusted, and columns align correctly.
Matrix Environments with pmatrix
Matrices occur frequently when conveying linear transformations and for expressing vector-matrix equations. The amsmath package provides pmatrix for small matrix displays inline:
$A = \begin{pmatrix} a & b\\ c & d \end{pmatrix}$
Formats as the 2x2 matrix: $A = \begin{pmatrix} a & b\\ c & d \end{pmatrix}$
For large matrices that need to be displayed centered on their own line,equation can handle simple cases safely without conflicts:
\begin{equation} M = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \end{equation}
However, more specialized environments like bmatrix and Bmatrix from amsmath adjust spacing and delimiters to be more aesthetically pleasing:
\begin{bmatrix*}[r] a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix*}
The Bmatrix version uses large delimiters around the matrix for enhanced readability.
Integrating mathtools for Extra Control
While amsmath handles much of the foundation of streamlining display math, the mathtools package provides useful extensions for fine-tuned manual spacing adjustments, advanced multiline typessetting,and delimiter sizing customization.
Extending amsmath Commands
Mathtools adds a wider range of predefined multirow alignment environments like split and gathered for more flexibility right alongside the standard ams alignments.
It also extends common math structures like cases and matrix:
\begin{dcases} a=b, & if \quad primal \\ y=z, & if \quad dual \end{dcases}
The syntax works analogously to amsmath align but with
Manual Math Spacing Adjustments
While amsmath and mathtools handle much of the automated spacing requirements so the author doesn't need to micromanage, manual override is still sometimes required.
Commands like \mspace and \mkern inserted manually enable finer spacing changes around operators:
$$ f(x) = \underbrace{x - 3}_{mkern12mu \mathstrut} $$
Produces the spacing tweak:
Where the kern paramater is adjusted until the overlap with x is resolved.
Advanced Matrix Functionalities
Building on amsmath, mathtools adds specialized environments for advanced matrix formatting needs:
- psdmatrix - positive semidefinite matrix symbols
- bdcases - braces and cases combined
- rcases - right-aligned cases
The RCases environment right aligns each case condition from the endpoint like:
Authors can achieve further customization by comma separation and use of the \\ row separator.
Recommended Delimiter Sizes
Choosing the properly sized delimiters around displayed equation components ensures uniformity and enhances legibility. The mathtools package provides additional guidance and automation capabilities through an extensible \\DeclarePairedDelimiter command compared to the more rudimentary \\left \\right syntax:
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert} $\abs{x - 3}$
Renders the properly sized vertical bars on either side of the absolute value expression, even in more complex nested scenarios like $\abs{\frac{1}{2}x -3}$.
Common Fraction Notations
Both inline and displayed fractions should use properly sized delimiters for uniformity and to avoid awkward oversized cases:
We can declare a \nicefrac command to automate the correct fence sizes:
\DeclarePairedDelimiter{\nicefrac}{\lvert}{\rvert} This displays properly $\nicefrac{1}{2}$
Ensuring the fences don't grow beyond the size of typical binary fractions.
Matrix and Cases Environments
The vertical curly brace delimiters for both matrices and cases statements should fit snugly around the content without excessive height:
\DeclarePairedDelimiter\matrix{\lbrace}{\rbrace} \begin{matrix} a & b \\ c & d \end{matrix}
Analogously, cases can be sized automatically without overflow beyond the argument rows:
\DeclarePairedDelimiter\mycases{\lbrace}{\rbrace} \begin{mycases} 1, & \text{if } x \gt 0\\ -1, & \text{if } x \lt 0 \end{mycases}
Piecewise Functions
The spacing and delimiters around inline and display piecewise functions needs particular attention for clarity. Overly large fences or inconsistent interim vertical spacing degrades legibility:
Mathtools provides \DeclarePairedDelimiter to customize spacing and delimiters around cases:
\DeclarePairedDelimiter{\pieces}{\{ \{ \}} {\rbrace} $f(x) = \pieces{ -1, & x < 0\\ 0, & x = 0\\ 1, & x > 0 } $
Which helps streamline piecewise definitions for consistency.
Troubleshooting Incompatible Packages
While mathtools extends amsmath functionality, conflicts can still occur when adding certain other third party packages naively alongside it. However, by analyzing error messages and through strategic load order and usage adjustment, major issues can usually be worked around.
Detecting Clashes in the Log File
When an equation fails to compile or exhibits strange behavior like missing elements or bad spacing, the first place to diagnose issues is the log file generated during pdflatex compilation. Warning messages here often shed light on package conflicts.
For example, upon adding the \usepackage{mathtoolbox} we see clues:
! Package mathtoolbox Error: Cannot use 'mathtoolbox' and 'mathtools' at the same time.
This points us directly to the crux of the issue for troubleshooting.
Strategies for Resolving Conflicts
When package clashes occur, there are a few general strategies to try:
- Check load order - Sometimes order of \usepackage matters
- Adjust offending code - Avoid directly conflicting commands
- Use ifthen conditionals - Load problem package only when needed
- Patch commands - Redefine problematic functions
Applying one or combining several techniques usually helps arrive at a stable document configuration.
Example Resolution for mathtoolbox
We can resolve the mathtoolbox conflict by checking for other activations of mathtools first before allowing it to load:
\usepackage{mathtools} % First load regular one \IfPackageLoaded{mathtools}{% \usepackage{etoolbox} \providecommand{\detectMathtools}{\usepackage{mathtoolbox}}% }{% \usepackage{mathtoolbox}% }
This way document sections that don't rely on mathtools features can benefit from mathtoolbox without destabilizing the rest.