# Misalignment of equation numbers in beamer

As is suggested by @daleif, this is a related question of the my previous post, now I have the following code in beamer,

\documentclass{beamer}

\begin{document}
\begin{frame}
\texttt{align} environment:
\begin{align}
\label{eq:1}
\begin{bmatrix}
x\\y\\z
\end{bmatrix}&=
\begin{bmatrix}
1&&\\&2&\\&&3
\end{bmatrix}
\begin{bmatrix}
1\\2\\3
\end{bmatrix} \\
\label{eq:2}
&=
\begin{bmatrix}
1&&\\&2&\\&&3
\end{bmatrix}
\begin{bmatrix}
d\\e\\f
\end{bmatrix}\\
\label{eq:3}
a&=b
\end{align}
\texttt{equation} environment:
$$\label{eq:4} a^{2}+b^{2}=c^{2}$$
\end{frame}
\end{document}


which will produce the following slide, where we can see that the misalignment of the equation numbers.

As is sugessted by @daleif in the comments of his answer, change \label{eq:1} to {\label{eq:1}} would help.

It is also suggested that wrap {} around bmatrix environment would also help.

Also just to mention my original cumbersome solution, which is to add && before (and possibly after) each line to fake it as multiple alignment, then the numbers would also line up.

Surely for now {\label{eq:1}} is the simplest solutions, but why is this happening, and is there a more "standard" way to handle this?

# The problem

The problem is actually not restricted to the bmatrix environment, as can be seen from the following example:

\documentclass{beamer}
\usepackage{showframe} %% <- make the text area visible

\begin{document}
\begin{frame}
\begin{align}
\label{eq:1}
A                             %% <- This is fine
\end{align}
\begin{align}
\label{eq:2}
\left(A\right)                %% <- Not fine
\end{align}
\begin{align}
\label{eq:3}
-A                            %% <- Not fine
\end{align}
\begin{align}
\label{eq:4}
\begin{bmatrix}A\end{bmatrix} %% <- Not fine
\end{align}
\begin{align}
\begin{bmatrix}A\end{bmatrix} %% <- This is fine
\end{align}
\end{frame}
\end{document}


This occurs due to the fact that beamer modifies \label and that the modified command creates an \hbox. This \hbox itself takes up no space, but since it is part of the equation it can cause horizontal skips to be inserted when it is followed or preceded by particular types of objects. This for instance happens if it is preceded/followed by a binary operator (like + or -), a binary relation (like = or <), an operator like (\sin or \sum) OR an "inner atom" like \left[ and \right]. (I'm not going to list all possibilities, but see the table in this answer.)

In your case, a \thinmuskip (as produced by \,) is inserted between this \hbox and the \left[ at the start of a bmatrix. In the -A example above, a \medmuskip (as produced by \:) is inserted before the - and another \medmuskip is inserted after it because this operator is now interpreted as a binary operator!

The reason why this messes up the placement of the equation label is that align typesets its contents twice: once to measure its size and once for real. During the "measuring phase", \label is redefined to do nothing, which in particular means that it will not insert the aforementioned \hbox when it is just being measured. Consequently, amsmath ends up measuring the width of the cell incorrectly and using this incorrect value to determine how much space should be inserted before/after/between columns.

# Solutions

### (1) Manual solution

Using {\label{<name>}} (or e.g. \label{<name>}{} or \label{<name>}\hbox{}) only partially solves this problem. If this is used, amsmath will still see an empty group ({}) during the measuring phase, which is the same as a \hbox{…} and {\hbox{…}} for spacing purposes. It thus fixes the placement of the equation number by introducing the same unnecessary skip during the measuring phase. (Also: the space between - and A in the third equation above will still be wrong!)

If you want to solve this without hacking beamer's redefinition of \label, you can replace \label{<name>} by \mathopen{\label{<name>}} if used at the start of a cell. This works because no space is ever inserted after an Open atom.

\documentclass{beamer}
\usepackage{showframe} %% <- make the text area visible

\begin{document}
\begin{frame}
\begin{align}
\label{eq:1}
A                             %% <- Still fine
\end{align}
\begin{align}
\mathopen{\label{eq:2}}
\left(A\right)                %% <- Now fine!
\end{align}
\begin{align}
\mathopen{\label{eq:3}}
-A                            %% <- Now fine!
\end{align}
\begin{align}
\mathopen{\label{eq:4}}
\begin{bmatrix}A\end{bmatrix} %% <- Now fine!
\end{align}
\begin{align}
\begin{bmatrix}A\end{bmatrix} %% <- Still fine
\end{align}
\end{frame}
\end{document}


(Note that also -A is now also spaced correctly!)

(Replacing \label{<name>} by \mathclose{\label{<name>}} at the end of a cell usually works, but not quite always: a skip will still be inserted if the label is preceded by punctuation (like a ,).)

### (2) Patching beamer

You can also patch beamer to make sure that no \hbox is ever inserted in the first place. The offending \hbox can be found here (in beamerbasenavigation.sty):

\def\beamer@inserttarget#1{%
\ifbeamer@inframe%
\ifvmode%
\nointerlineskip\vbox to0pt{#1}%
\else%
\hbox{#1}% %% <- This is the problem!
\fi%
\else% defer to next frame
\expandafter\gdef\expandafter\beamer@framehypertargets\expandafter{\beamer@framehypertargets#1}%
\fi%
}


(#1 is of the form \hypertarget{<label name>}{} and it sets up the target for hyperlinks pointing to this equation.)

The problem is solved if this line is removed, but we don't want to do that since that'd destroy hyperlinks. It would also be solved if \hbox{#1} is replaced by #1, but we don't want to do that either since that \hbox is probably there for a reason (which probably does not apply in math mode).

So here is a proposal: if you add the following to your preamble, #1 will be inserted directly in math mode, and it will be \hboxed when used elsewhere.

\makeatletter %% <- make @ usable in command names
\patchcmd{\beamer@inserttarget}
{\hbox{#1}}{\ifmmode#1\else\hbox{#1}\fi}{}{}
\makeatother  %% <- revert @


(\patchcmd is part of the etoolbox package, which is already loaded by beamer.)

Here is the result:

\documentclass{beamer}
\usepackage{showframe} %% <- make the text area visible

\makeatletter %% <- make @ usable in command names
\patchcmd{\beamer@inserttarget}
{\hbox{#1}}{\ifmmode#1\else\hbox{#1}\fi}{}{}
\makeatother  %% <- revert @

\begin{document}
\begin{frame}
\begin{align}
\label{eq:1}
A                             %% <- Still fine
\end{align}
\begin{align}
\label{eq:2}
\left(A\right)                %% <- Also fine!
\end{align}
\begin{align}
\label{eq:3}
-A                            %% <- Also fine!
\end{align}
\begin{align}
\label{eq:4}
\begin{bmatrix}A\end{bmatrix} %% <- Also fine!
\end{align}
\begin{align}
\begin{bmatrix}A\end{bmatrix} %% <- Still fine
\end{align}
\end{frame}

\begin{frame}
The hyperlinks in the references \ref{eq:1} and \ref{eq:4} still work.
\end{frame}
\end{document}


• @samcarter: Writing short answers is something I seem to be incapable of, but you're welcome :). This problem seemed to make no sense at first, so I just had to find out what was going on. – Circumscribe Jan 2 at 15:35
• But the solution looked so good :( Anyway thanks for diagnosing the problem, maybe it will help some other users – user36296 Jan 2 at 16:02
• @samcarter: There was a problem with the earlier version, but I think the current version should work correctly. – Circumscribe Jan 2 at 16:28
• Good to hear! Thanks again for your answer! (awarding the bounty won't be possible until tomorrow) – user36296 Jan 2 at 16:29
• @samcarter: I've opened an issue on the beamer github page: github.com/josephwright/beamer/issues/523 – Circumscribe Jan 4 at 16:26