# Mastering Math Typesetting With Latex

## Formatting Inline and Display Math

LaTeX offers robust tools for typesetting both inline and display style mathematical expressions. Inline math enclosed in $ $ executes in text style and size, while display math set in \[ \] or within equation blocks renders at a larger size more suited for showcasing formulas.

### Using $ $ for Inline Math Equations

To insert a short math expression within a sentence, enclose it in a $ delimiter. For example, Einstein's famous equation $E=mc^2$ elegantly relates energy and mass. LaTeX will typeset the formulas using its default math font and style. The resulting text mode inline math blends seamlessly with the surrounding text.

### Using \[ \] and Equation Environment for Display Math

For emphasizes formulas that need to stand out from the body text, use the display math delimiters \[ \]. LaTeX renders display math in its own paragraph centered on the page. For multi-line formulas, the equation environment functions similarly but enables numbering each equation for later reference. Display the Lorenz system below in an equation block:

\[

\begin{aligned}

\dot{x} & = \sigma(y-x) \\

\dot{y} & = \rho x - y - xz \\

\dot{z} & = -\beta z + xy

\end{aligned}

\]

Observe how this more accurately showcases the symbolic relationship.

### Adding Math Mode Commands

Within math mode, LaTeX provides a vast array of special commands for typesetting operators, relations, delimiters, functions and more. For example, inline fractions like $\frac{x+3}{x-2}$ automatically adjust size and position. Other useful math operators include roots $\sqrt[n]{x}$, integrals $\int_{a}^{b}$, summations $\sum^{n}_{k=0}{x_k}$, and absolute values $|x|$. LaTeX takes care of spacing and style concerns so authors can focus on the math itself.

### Adjusting Sizes with \displaystyle and Other Commands

While display math renders larger than inline math, authors may want full \displaystyle size even within inline formulas especially to match the size of surrounding text. Compare the inline root in the following sentences:

The smaller $\sqrt{x}$ causes an uneven baseline compared to the full-size \displaystyle \sqrt{x} where the formula matches the body text height.

For display math, adding \large, \Large, and \huge commands makes it possible to grow formulas well beyond \displaystyle without changing document font size.

## Creating Math Environments

Along with its many predefined math environments like equation and align, LaTeX enables authors to design custom mathematical environments suited for specific tasks.

### Defining New Math Environments with \newenvironment

To build a new mathematical environment, use the \newenvironment command to set up the desired formatting. For example, define a proof environment that italicizes the optional title, indents the body text, and adds a QED box at the end.

\newenvironment{proof}[1][\proofname]{\begin{itshape}#1\\\end{itshape}\begin{quote}}{\qed\end{quote}}

Then format proofs like the following:

\begin{proof}[Proof of the Mean Value Theorem]

Let $f$ be a continuous function on $[a,b]$. By Bolzano's Theorem, $f$ must attain both a maximum and minimum value on $[a,b]$. Call these $m$ and $M$. Then by the Extreme Value Theorem...

\end{proof}

The custom proof text stands out and proves the mean value theorem in elegant mathematical style.

### Customizing Existing Environments

In case LaTeX's predefined math layouts like align and gather don't format perfectly for a specific application, authors can copy the environment definitions into the document preamble and customize spacing, alignment, numbering, and other details.

### Adding Tags and Labels for Numbered Equations

For important formulas that authors plan to reference later, adding tags and labels enables citing by a symbolic moniker rather than the raw number. Define meaningful keys like \label{mean-value-theorem} for quick access.

\begin{equation} \label{mean-value-theorem}

f'(c) = \frac{f(b)-f(a)}{b-a}

\end{equation}

As seen in the mean value theorem proof (\ref{mean-value-theorem}),...

### Configuring Spacing Around Math Blocks

While LaTeX tries to choose appropriate vertical space before and after display math, authors can exert more control with spacing helpers like \\[2em] or the \abovedisplayskip \belowdisplayskip length registers. This positions artfully spaced math precisely where needed.

## Advanced Math Constructs

Beyond fundamentals like fractions and roots, LaTeX supports formatting intricate mathematical structures from stacked expressions to multi-line matrices and binomial coefficients.

### Building Multi-line Expressions with Cases, Matrices, Brackets

Showcase complex equations with cases that set specialized piecewise renditions for different domains:

\[

u(x) =

\begin{cases}

\exp{x} & \text{if } x \geq 0 \\

1 & \text{if } x < 0
\end{cases}
\]

Matrices align elements cleanly in rows and columns:

\[

A =\begin{pmatrix} a & b \\ c & d \end{pmatrix}

\qquad

B =\begin{bmatrix} e & f \\ g & h \end{bmatrix}

\]

Or use stacked notation for tall algebraic combinations:

\[

\begin{Vmatrix}

x \\

y \\

f(x)+y

\end{Vmatrix}

=

g(t)

\]

### Typesetting Complex Symbols, Operators, and Accents

Blackboard bold variables $\mathbb{N}$ and $\mathbb{C}$, script letters $\mathcal{H}$ and $\mathcal{M}$, specialized operators like $\partial$ and $\nabla$, and accented symbols like $\hat{y}$ and $\vec{x}$ help LaTeX represent nearly any mathematical object.

### Creating Custom Math Operators and Symbols

If built-in math operators seem lacking, create custom commands with intuitive markup like \diff to differentiate functions $\diff{f}{x}$ and operators like \prob for probability formulas like $P(\text{Heads} \mid \prob(\text{Heads}) = 0.5 )$.

### Using Math Packages like amsmath, mathtools, etc.

Packages like amsmath, mathtools, nccmath, and mhchem provide 100s more math features like extensible brackets \left\lbrace...\right\rbrace, combined operator commands \mathtoolsset{xspacebefore={}xspaceafter=} for consistent spacing, and chemical molecule rendering \ce{C6H12O6}.

## Math Fonts and Style

While formula rendering defaults to italicized variables in the LaTeX math font, authors can override styling details where needed.

### Changing Default Math Font with \mathnormal, \mathrm, etc.

Functions and multi-letter operators like \sin, \lim, \det, and \ker traditionally render in upright roman font among slanted variables. For one-off changes, toggle \mathnormal{Beta} or \mathrm{m}. Or redefine document-wide defaults:

\everymath{\mathnormal} % Set everything upright

\everymath{ } % Reset to italic

### Italicizing Variables versus Making Functions Roman

Since mathematical conventions vary, override tradition with \mathit for italic multi-character names $\mathit{Beta}$ and $X$ or \mathrm for upright one-letter variables \mathrm{x}. Authors choose the desired rendition.

### Using Bold Math Symbols for Emphasis

Highlight key parts of formulas with \mathbf{vectors}, \boldsymbol{matrices}, or \pmb parametric variables. Display style math even supports \boldmath $\mathbf{Sin}(x)$ for completely bold lines.

### Accessing Alternate Math Symbol Shapes and Sizes

Some math operators and glyphs change form depending on usage such as summation symbols that grow to encompass multi-line bounds:

\[

\sum_{i=1}^n i = \frac{n(n+1)}{2}

\]

Authors can override defaults with \textstyle for cramped inline spacing $\textstyle\sum$ or \displaystyle for loose unconstrained placement $\displaystyle\int$. LaTeX adapts symbols to match.

## Troubleshooting Math Rendering Issues

While LaTeX excels at math typesetting, some situations require troubleshooting for optimal formula display.

### Debugging Missing Package Errors

When using specialized math functionality, LaTeX complains if the associated package fails to load with errors like:

! Package mathtools Error: You need to load the mathtools package.

Simply add \usepackage{mathtools} to the document preamble to resolve missing package dependencies.

### Fixing Bad Boxes and Overfull Lines

Improperly spaced math can spill out of its container with warnings like:

Overfull \hbox (6.11223pt too wide)

[]|\OMEGA

[]

Track down the faulty math fragment and balance its delimiters {...} or add line breaks \\ to fix bad boxes and overfull math.

### Adjusting Inline Math Spacing

Compare the spacing around inline formulas $x \gt 3$ versus {\setlength\mathsurround{0pt}$x \gt 3$}. The \mathsurround length governs padding and helps inline math blend with body linespacing.

### Handling Font Conflicts with Other Document Fonts

If text and math fonts clash stylistically, load separate matched symbol sets with:

\usepackage[math-style=TeX, vargreek-shape=unicode]{mathastext}

This overrides the defaults to enable harmonious document fonts.