# Problem with labeled equations in beamer: Why do the labels have different positions after each compilation?

I'd like to make a slide in beamer with a few equations that labels the parts of the last equation shown. I'm doing this with overlays so one equation is added at a time, and only that last equation shows is labeled.

In one approach I've tried, the position of the labels changes each time I compile the file. Specifically, the labels move something like \vspace{0.05cm} upward or \vspace{-0.05cm} downward each time I compile, depending on whether the label is above or below the equation. I want these labels to stop moving on their own. Compile the file about 20 times and you'll see a huge change.

I am also having the problem sometimes that the equation is not all aligned. In the following example, the "Max" is not aligned with the rest of the equation.

\documentclass[professionalfonts, smaller]{beamer}
\usetheme{CambridgeUS}
\usepackage{tikz}
\usepackage{iwona}
\usepackage{amsmath}
\usepackage{mathtools}
\usetikzlibrary{arrows,shapes}
\DeclareMathOperator*{\Max}{Max}

\begin{document}

\tikzstyle{every picture}+=[remember picture]

\everymath{\displaystyle}

\begin{frame}
\frametitle{Slide Title}

\tikzstyle{na} = [baseline=-.5ex]

\vspace{1cm}
\hspace{0.0cm} Comment on x \tikz[na] \node[coordinate] (n1) {};
\hspace{1.25cm} \tikz[na] \node[coordinate] (n2) {}; Comment on p(x)
\vspace{0.0cm}
\begin{equation*}
\hspace{-4.5cm}
\tikz[overlay, remember picture, baseline]{
\node[anchor=base](t1){$\Max\limits_x$};
}
\tikz[overlay, remember picture, baseline]{
\node[anchor=base, right of=t1, node distance=30pt](t2){$L(x) =$};
}
\tikz[overlay, remember picture, baseline)]{
\node[anchor=base, right of=t2, node distance=35pt](t3){$p(x) +$};
}
\tikz[overlay, remember picture, baseline)]{
\node[anchor=base, right of=t3, node distance=29pt](t4){$\gamma d(x)$};
}
\end{equation*}

\vspace{0.4cm}
\hspace{0cm} Comment on L(x) \tikz[na]\node [coordinate] (n3) {}; \hspace{1.5cm}   Comment on \tikz[na]\node [coordinate] (n4) {}; d(x)

\begin{tikzpicture}[overlay]
\path[->](n1) edge [bend left] (t1);
\path[->](n2) edge [bend right] (t3);
\path[->](n3) edge [bend right] (t2);
\path[->](n4) edge [bend right] (t4);
\end{tikzpicture}

\end{frame}
\end{document}

-
Hi, welcome to TeX.SE. –  Matthew Leingang Feb 8 '12 at 15:25
You can probably pare down your example file. Looking at it a bit more closely I see that t1's placement depends on t1's baseline, and so on. So TikZ is constantly recalculating. Indeed, latexmk gives up recompiling, suspecting an infinite loop. –  Matthew Leingang Feb 8 '12 at 15:37
Matthew -- thank you so much for your warm welcome and for your feedback. What do you mean by "pare down?" What, for example, would you suggest that I do in Beamer? –  J G Feb 8 '12 at 15:40
Is this the effect you're trying to get? Note the use of the anchor=base key. –  Matthew Leingang Feb 8 '12 at 16:13
Yes! I worked off of that link. That is exactly what I'd like to achieve. However, I wanted to remove the color from the equations. Also, what that slide did in 3 overlays, I wanted to do in one overlay. I was having one problem, however. There was too much space within the equation depending on where I placed the nodes and arrows. I wanted to close the space in to make the equation appear more natural. I changed the nodes only for that goal, but then got this new problem. –  J G Feb 8 '12 at 16:22

OK, I trimmed down the file quite a bit but here is something I think is closer to you want:

\documentclass{beamer}
\usetheme{CambridgeUS}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\DeclareMathOperator*{\Max}{Max}

\tikzstyle{every picture}+=[remember picture]
\newcommand\na[1]{\tikz[baseline=-.5ex]{\node[coordinate] (#1) {};}}
\newcommand\ta[2]{\tikz[baseline]{\node[anchor=base] (#1) {#2};}}

\everymath{\displaystyle}

\begin{document}

\begin{frame}
\frametitle{Slide Title}

Comment on x  \na{n1}
\na{n2} Comment on $p(x)$
\begin{equation*}
\ta{t1}{$\Max\limits_x$} \ta{t2}{$L(x)$} = \ta{t3}{$p(x)$} + \ta{t4}{$\gamma d(x)$}
\end{equation*}

Comment on L(x) \na{n3}
Comment on \na{n4} $d(x)$

\begin{tikzpicture}[overlay]
\path[->](n1) edge [bend left] (t1);
\path[->](n2) edge [bend right] (t3);
\path[->](n3) edge [bend right] (t2);
\path[->](n4) edge [bend right] (t4);
\end{tikzpicture}

\end{frame}
\end{document}

• I deleted all the \hspace and \vspace commands. Usually there's a better way to achieve the spacing you want than to hardcode it.
• I macro-fied the tikz nodes that just mark nodes for the arrows to connect. It makes the code within \begin{frame}...\end{frame} a bit more readable.
• I deleted the only<1>{...} part because you said you wanted it all one slide/overlay. If you want fancier revealing ask another question.

Hope that helps.

-
Thank you so much for doing this!! I'm looking forward to playing around with the code to better understand it. When you say that there is usually a better way to achieve vspace and hspace without resorting to those commands, may I please ask what that method would be? For example, how would I move all of this up by say 1 inch? Second, how would I move the comments for p(x) and d(x) more to the right so there is more space between the comments on the same line? –  J G Feb 9 '12 at 15:02
This code also removes some features of the code that I initially had. I had tried to reduce the space between the + the \gamma to make the equation appear a little more naturally. This version removes what I had done and they are now spaced further apart than I would like. –  J G Feb 9 '12 at 16:10
Rather than seek absolute placement of text I let the document class's defaults take care of it. If you want to override than you can insert hspace and vspace but I rarely do. If your comments are the only other item on the slide other than the equation you can use TikZ callout nodes and place them wherever you want. The extra space might be coming from the TikZ nodes...i'll take a look later. –  Matthew Leingang Feb 9 '12 at 18:28