7

I have the following code in \documentclass{beamer},

\documentclass{beamer}

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

which gives the following slide.

Enter image description here

And we can see that the last equation number does not line up. I did not have such a problem if I used \documentclass{article} instead.

Why is this happening, and how can I fix this?

davyjones
  • 935
  • 1
    If you add \tag{a\rlap{\smash{\rule[-10cm]{0.4pt}{12cm}}}} to the first row in the align, the problem is easily seen. – daleif Jan 09 '18 at 08:30
  • Theory: in beamer, perhaps equation is redefined to be gather, because gather shows the same problem. Might want to add gather as an example as well. – daleif Jan 09 '18 at 08:32
  • Further experiment, it is something inside the beamer frame env, if you redefine frame to do nothing (\renewenvironment) then the placement is correct. We probably need some of the beamer people to have a look at this. – daleif Jan 09 '18 at 08:37
  • 1
    @daleif I noticed just now that not only the label, the two matrices does not align either. btw, glad to know the trick to show this. – davyjones Jan 09 '18 at 08:40
  • Yes, now you mention it. I'm currently updating my TeXLive to see if there are any updates that I have missed. The (r/l)lap+smash+rule trick is rather useful. – daleif Jan 09 '18 at 08:43
  • Have you checked to see if it is \label that does something it should not? Just checked, \label seems to leave a space behind. – daleif Jan 09 '18 at 08:44
  • It seems that it is the \label what moves everything. Out comment all \labels, and it looks like it should. – daleif Jan 09 '18 at 08:46
  • @daleif, OH, I think it IS \label after some checking! – davyjones Jan 09 '18 at 08:47
  • Hmm, I think that is a feature, not a bug. You should not place eq:4 there, move it to be the first on the row, then everything is ok again – daleif Jan 09 '18 at 08:47
  • the \label probably leaves a "whatsit", which = and the matrix reacts to and this creates an extra small space. I'll add that as an answer – daleif Jan 09 '18 at 08:48

2 Answers2

9

After a bit of messing around, the issue seems to be a bad \label placement.

               \end{bmatrix} \\
&=
\label{eq:4} %<----
             \begin{bmatrix}

Note that the marked label is after &=, the whatsit probably ends up confusing the math spacing. Moving the \label up as the first thing on this line, removes the problem.

The solution is to start or end the row with \label, don't add it the middle of the formula. For the sniplet above use

               \end{bmatrix} \\
\label{eq:4} %<----
&=
             \begin{bmatrix}

I have no idea why this causes the equation numbers to be placed misaligned.

daleif
  • 54,450
  • excuse me, could you pls provide a complete code sample so that I can test it? currently any \label inside align will mess up the alignment for me. thanks~ – davyjones Jan 09 '18 at 09:02
  • 2
    Oh, I had to put \label{eq:4} above &= to make everything (matrices + equation numbers) align – davyjones Jan 09 '18 at 09:06
  • 1
    Thanks, I see where I did wrong in my original code. Another questions is that if I add a \label{eq:xx} above the first equations (below \begin{align}), the misalignment reappears. This time it's just the numbering, not the matrices. How can I fix that? – davyjones Jan 09 '18 at 09:14
  • @davyjones Please post it as a new question. Then we need the beamer maintainers to look at it. – daleif Jan 09 '18 at 09:16
  • 1
    sure, I'll try it out and post it soon. I think this new question is the real question I originally want to ask. I'll do it more carefully next time. Thanks! – davyjones Jan 09 '18 at 09:17
  • @davyjones might even be an idea to make the MWE have two aligns and an equation, then only add \label to one of the aligns. My guess is that there is a sporadic space somewhere on the code. – daleif Jan 09 '18 at 09:21
  • 2
    @davyjones seems {\label{...}} helps... – daleif Jan 09 '18 at 09:56
  • I think the problem is caused by the bmatrix environment and not the \label and align! (Try the code without bmatrix.) –  Jan 09 '18 at 11:02
4

I think the problem is caused by the bmatrix environment. Use {\begin{bmatrix}...\end{bmatrix}}! Try this code:

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

enter image description here

  • yes, I saw something similar in the chatroom, this is really a unconventional strike for bmatrix fans. I'll post another question to attract beamer maintainers' attention as suggested. – davyjones Jan 09 '18 at 10:54