LaTeX mathematics hacks

September 8, 2014 — July 18, 2024

computers are awful
faster pussycat
LaTeX
plain text
typography
UI
workflow
Figure 1

Tips for LaTeX specific to mathematical typesetting. See also Chris Cheung’s list.

1 Spacing and line breaking

Weird in maths. The breqn package position paper explains many of the issues. NB: the solutions that actually work in the javascript-backed LaTeX maths are multline (nb only one i) and split environments, so in practice I use those to ensure cross-compatibility of copy-pasta.

2 Opt-in equation numbering: \numberthis

The latex macro \numberthis is satisfying. Thanks Russell Tsuchida for showing it to me. It allows me to number only needed lines in multi-line equations. The macro goes like this:

\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}

A minimal example in context:

\documentclass{article}
\usepackage{amsmath}
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}
\begin{document}
\begin{align*}
a &=b \\
  &=c \numberthis \label{eqn}
\end{align*}
Equation \eqref{eqn} shows that $a=c$.
\begin{equation}
d = e
\end{equation}
\end{document}

NB I no longer do this, because it annoys me when other people do this. Number all your equations, you don’t know which ones I want to refer to when you write the paper.

3 Math size

I forget this all the time. Explained by overleaf, Math font size ordering is

\displaystyle         % Size for equations in display mode
\textstyle          % Size for equations in text mode
\scriptstyle          % Size for first sub/superscripts
\scriptscriptstyle  % Size for subsequent sub/superscripts

4 Arrays

There is an array environment which is good for typesetting equations, but it is too verbose for typesetting arrays of other things like numbers.

Use amsmath matrix for that, e.g.

\begin{pmatrix}
1 & 2 & 3\\
a & b & c
\end{pmatrix}

5 Underbraces

If we want a brace underneath part of an equation, amsmath provides underbrace.

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
$$
\underbrace{u'-P(x)u^2-Q(x)u-R(x)}_{\text{=0, since~$u$ is a particular solution.}}
$$
\end{document}

6 Defining new operators

“Without Limits” i.e. limits on the side, \({\mathop{\mathrm{arg\,max}}\nolimits}_{x\to\infty} x\). Plain style (works everywhere including old MathJax):

\newcommand{\sech}{\mathop{\mathrm{sech}}\nolimits}

amsmath style (works in AMSMath environments):

\DeclareMathOperator{\sech}{sech}

If we want limits underneath (Does not seem to display correctly on this blog so we will have to use our imaginations)

Vanilla:

\newcommand{\sech}{\mathop{\mathrm sech}\limits}

7 Symbols

Start from the Latex Math Symbols Cheat sheet. Then try the Not so short introduction to LaTeX. Still have problems? Maybe the following tips help:

7.1 Emergency short minus

One should never hack the minus sign. However, sometimes one must because one is trying to fit equations of time series analysis into a multi column layout. Here is how I squeezed down \(t-1\) to \(t{\operatorname{-}}1\).

\newcommand{\tm}{t{\operatorname{-}}1} % "t-1" squeezed down

7.2 Stochastic independence symbol

A case study in doing typography right. The probabilistic independence symbol ⫫, unicode U+2AEB, UTF-8: E2 AB AB, (“double up tack”) does not ship in normal LaTeX maths systems for some reason. So how do you fake it? In one of many slightly unsatisfactory ways!

Jason Blevins suggests the following hacks:

\newcommand{\indep}{\perp \! \! \! \perp}

The shortcoming of this solution is that it does not set the symbol up as a proper operator, which probably means something bad in the complicated world of LaTeX spacing. Perhaps this would be better:

\newcommand{\indep}{\mathop{\perp \! \! \! \perp}}

Alternatively, the following does more specific space management.

\newcommand\indep{\protect\mathpalette{\protect\independenT}{\perp}}
\def\independenT#1#2{\mathrel{\rlap{$#1#2$}\mkern2mu{#1#2}}}

Fibo Kowalsky adds the alternative:

\newcommand{\indep}{\raisebox{0.05em}{\rotatebox[origin=c]{90}{$\models$}}}

Ashwin Khadke notes that for classic LaTeX one can import the symbol in one of the massive math symbol fonts, e.g. mdsymbol:

\usepackage{mdsymbol}
\newcommand{\indep}{\upvDash}

All of these would probably benefit from declaring the created symbol to be a mathematical operator via \mathrel.

Note that mdsymbol is incompatible with amssymb and amsfonts although notionally it renders them unneeded. Also it is a sans serif math font, so may not fit with your aesthetic. And it redefines various useful characters and is generally a mess.

The generic glyph import also presumably works.

If I am using unicode-math, it is very simple. We can type it as

\usepackage{unicode-math}
\newcommand{\indep}{⫫}
\newcommand{\indep}{\symbol{"2AEB}}

Any of the above should result in the independence symbol being available for use as

$X \indep Y$

AFAIK only the Jason Blevins double \perp trick works for js mathematics, although in that case I believe you can just type ⫫ since js mathematics is happy with unicode.

7.3 Conditional |

The stochastic conditional symbol is also fiddly to type. Jason Blevins observes that spacing is differently allocated in normal and big sizes.

Normal size:

\Pr( A \mid B )

will get us

\[\Pr(A \mid B)\]

Big size needs a \; spacer set around a \middle\vert, e.g.:

\Pr\left( A \;\middle\vert\; \sum_{i=1}^N B_i \right)

for

\[\Pr\left( A \;\middle\vert\; \sum_{i=1}^N B_i \right)\]

Every moment spent thinking about this nonsense is a moment spent not saving the world or eating profiteroles or having sex or such.

7.4 Intercal

What is the \intercal symbol? No one knows, but possibly it is a reference to the Roman god Terminus which got into character sets in 1986 because of a single book in 1975 and has remained by inertia.

8 Bold and other font variants in mathematics

8.1 Classic bold

tl;dr: In mainline LaTeX, \usepackage{bm} then \bm everywhere In Mathjax, \boldsymbol everywhere.

I want to bold certain symbols in equations. Sometimes these symbols are greek letters, and sometimes roman letters. Possibly other things.

Back to the more immediate problem. Roman letters are bolded by \mathbf{x} and greek letters by \boldsymbol{\xi}.

If I write a macro which attempts to bold whatever, this leads to sadness. I need to know ahead of time what it is that I need to bold. How can I bold any old thing? The canonical answer from the AMS guide seems to be \boldsymbol and \bm with the bm package. This is fine, except that the bm package does not exist for Mathjax.Which is not fatal as such. \boldsymbol in Mathjax is more powerful than in vanilla LaTeX and will in fact bold anything, unlike in vanilla LaTeX. So there is a solution for each, just not seamless translation between them. I could try to work around this by writing macros to redefine \boldsymbol but this feels like it is asking for trouble, and macro support in Mathjax is not universal (e.g. it does not work if you are outputting to a powerpoint presentation.

8.2 Blackboard bold for non-letters

a.k.a. doublestroke. Notoriously annoying for numerals. Historically, for legacy fonts one used

\documentclass{article}
\usepackage{bbm}
\begin{document}
$$
  \mathbbm{1}
$$
\end{document}

This looks bad on modern PDFs, often coming out pixelly, and does not work in web browser maths. Here are some alternatives.

Davislor’s recent advice suggests that the problem does not arise in unicode mathematics, but that a modern solution for legacy PdfTeX is

\documentclass{article}
\usepackage{amsmath}
\usepackage[bb=dsserif]{mathalpha}
\begin{document}
$$ \mathbb{1} \Bbbbone
$$
\end{document}

This one works for some documents and not others; I am not sure why. When that one does not work for me, the following does:

\documentclass{article}
\DeclareSymbolFont{bbold}{U}{bbold}{m}{n}
\DeclareSymbolFontAlphabet{\mathbbold}{bbold}
\begin{document}
$$ \mathbbold{1}
$$
\end{document}

In MathJax 2 the syntax is \mathbb{1} but you need to cross your fingers and hope because it depends upon something complicated about users’ browsers. MathJax 3 apparently works but all the software I use assumes MathJax 2. Some people endorse \unicode{x1D7D9} to write 𝟙 but reportedly it is unreliable and also it looks awful. Maybe in a macro?