Beamer notes and amsmath environments

This problem is caused by the fact that the align environment needs to perform some measurements and computations before actually printing anything. To do this, it first has to typeset everything in box registers that will not be printed, only measured. Everything is typeset a second time afterwards, this time for real.

A consequence of this procedure is that any assignments made inside an align environment are performed twice, which is what is happening in your case. (Note: all LaTeX counters are reset when the measurements are completed, so \setcounter and \stepcounter do work as expected inside an align environment.)

One way to circumvent this problem is by using the conditional \ifmeasuring@, which is set to true while these measurements are being performed. The following comes from the amsmath documentation:

\ifmeasuring@
All display environments get typeset twice—once during a “measuring” phase and then again during a “production” phase; \ifmeasuring@ will be used to determine which case we’re in, so we can take appropriate action.

You could thus redefine \note so that it does nothing during the measuring phase, like this:

\documentclass{beamer}

\usepackage{amsmath,pgfpages}

\makeatletter
\let\originalnote\note
\renewcommand<>\note[2][]{\ifmeasuring@\else\originalnote#3[#1]{#2}\fi}
\makeatother

\setbeameroption{show notes on second screen=right}

\newcounter{notecounter}

\begin{document}
   \begin{frame}
      \begin{align*}
         a & = 1 \note{This is the first note: \arabic{notecounter}\stepcounter{notecounter}\\} \\
         b & = 2 \note{This is the second note \arabic{notecounter}\stepcounter{notecounter}\\}
      \end{align*}
   \end{frame}
\end{document}

enter image description here


For some reason, code within align* is executed twice. Thus, the first two notes are defined twice, too. The notes get the labels p0-1, p0-2, p0-1, p0-2 and p0-3 (note the duplicate labels). Beamer has stored 5 notes now. When typesetting the notes page, your code tries to use the labels p0-1 to p0-5 for the stored notes. And of course, the last two are unknown.

The positions stored by your code are correct. But now, the third stored note is the duplicate of the first. This is set with the position of the third real note. The other notes are not given out, because the labels now used are undefined.

A possible solution is to prevent the first execution of the notes (I also tried to prevent the second one, but this didn't work). Here this is done with \manote (ma for math align), which executes the \note only if the flag \ifexecutenote is true. The flag is set to false with \AtBeginEnvironment (requires the etoolbox package). Unfortunately, it can not be reset with \AtEndEnvironment, because at this point it is already to late. So the flag is set with either a) using \lmanote (sets the flag internally) for the last note or b) using \executemanotes after the last \manote, but in the environment.

For each other environment you have to add a line \AtBeginEnvironment{someenv}{\global\executemanotefalse} in the preamble.

I left some (commented out) debug code in, because it was very useful for figuring out, what happens. It may be helpful in case your complete code shows some problems. The code writes some information to the log file.

enter image description here

\documentclass{beamer}

\usepackage{amsmath,pgfpages,zref-user,zref-savepos}

\usepackage{etoolbox} % <---- new

\setbeameroption{show notes on second screen=right}
\setbeamertemplate{note page}[plain]

\newcounter{notecounter}

\makeatletter
\let\oldnote\note
\newcounter{pposes}
\newcounter{poses}
\AtBeginNote{%
   \setcounter{poses}{0}%
}
\AtEndNote{%
   \setcounter{poses}{0}%
   \stepcounter{pposes}%
}
\renewcommand<>{\note}[1]{%
   \only#2{%
      \stepcounter{poses}%
      \vbox to 0pt{%
         \edef\l{p\the\value{pposes}-\the\value{poses}}%
%         \typeout{XXXXXXXXXXXXXXXXXXXX label: \l}%
         \zsaveposy{\l}%
         \zrefused{\l}%
%         \typeout{AAAAAAAAAAAAAAAAAAAA pos: \the\dimexpr\paperheight - \zposy{\l}sp - 6mm}%
         \oldnote{%
            \stepcounter{poses}%
            \edef\l{p\the\value{pposes}-\the\value{poses}}%
%            \typeout{YYYYYYYYYYYYYYYYYYYY label: \l}%
            \zref@ifrefundefined{\l}{%
%               \typeout{ZZZZZZZZZZZZZZZZZZZZ label undefined: \l}%
            }{%
%               \typeout{ZZZZZZZZZZZZZZZZZZZZ label defined: \l}%
               \parbox[l][0pt][t]{0pt}{%
                  \hfuzz=\maxdimen%
                  \parbox{\textwidth}{%
                     \vspace{\dimexpr\paperheight - \zposy{\l}sp - 6mm}%
%                     \typeout{BBBBBBBBBBBBBBBBBBBB pos: \the\dimexpr\paperheight - \zposy{\l}sp - 6mm}%
                     #1%
                  }%
               }%
            }%
         }%
      }%
   }%
}

%-------------------------------------------------------------------------------
\newif\ifexecutemanote
\newcommand<>{\manote}[1]{%
    \ifexecutemanote
        \note#2{#1}%
    \fi
}
\newcommand<>{\lmanote}[1]{%
    \manote#2{#1}%
    \global\executemanotetrue
}
\newcommand*{\executemanotes}{\global\executemanotetrue}

\AtBeginEnvironment{align*}{\global\executemanotefalse}
%-------------------------------------------------------------------------------

\begin{document}
   \begin{frame}
      \begin{align*}
         a & = 1 \note{This is the first note: \arabic{notecounter}\stepcounter{notecounter}} \\
         b & = 2 \note{This is the second note \arabic{notecounter}\stepcounter{notecounter}}
      \end{align*}
      Problem \note{This is the third note: \arabic{notecounter}\stepcounter{notecounter}}
%      \typeout{EEEEEEEEEEEEEEEEEEEE: with note}
   \end{frame}
%   \typeout{EEEEEEEEEEEEEEEEEEEE: 1. frame end}

   \begin{frame}
      \begin{align*}
         a & = 1 \manote{This is the first note: \arabic{notecounter}\stepcounter{notecounter}} \\
         b & = 2 \lmanote{This is the second note \arabic{notecounter}\stepcounter{notecounter}}
      \end{align*}
      or
      \begin{align*}
         a & = 1 \manote{This is the third note: \arabic{notecounter}\stepcounter{notecounter}} \\
         b & = 2 \manote{This is the fourth note \arabic{notecounter}\stepcounter{notecounter}}
         \executemanotes
      \end{align*}
      Problem \note{This is the fifth note: \arabic{notecounter}\stepcounter{notecounter}}
%      \typeout{EEEEEEEEEEEEEEEEEEEE: with tablenote}
   \end{frame}
%   \typeout{EEEEEEEEEEEEEEEEEEEE: 2. frame end}

   \begin{frame}
      \begin{tabular}{cc}
         a & = 1 \note{This is the first note: \arabic{notecounter}\stepcounter{notecounter}} \\
         b & = 2 \note{This is the second note \arabic{notecounter}\stepcounter{notecounter}}
      \end{tabular}

      Problem \note{This is the third note: \arabic{notecounter}\stepcounter{notecounter}}
%      \typeout{EEEEEEEEEEEEEEEEEEEE: with tabular}
   \end{frame}
%   \typeout{EEEEEEEEEEEEEEEEEEEE: 3. frame end}
\end{document}

Tags:

Beamer

Amsmath