9

Problem description

In trying to calculate the progress of a Beamer presentation, one can use

\pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\insertdocumentendpage)}

where \prog tracks the percentage value of the progress.

To take into account that a presentation may possibly contain an appendix, and to exclude slides therein from the calculation of \prog, I naturally thought that one can simply change \insertdocumentendpage to \insertpresentationendpage, or \insertappendixstartpage-1 when an appendix does exist. However, doing so results in an error:

Package PGF Math Error: Unknown operator `=' or `=4' (in '100*(3)/({\beamer@tempcount =4\relax 0})').

If \insertappendixendpage is used, there is no error (but the value of \prog will be wrong).

The following MWE contains no appendix, so the four \insert<...>s should have the same value. However, uncommenting either of the last two frames results in the abovementioned error.

\documentclass{beamer}

\begin{document}

\frame{
    \pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\insertdocumentendpage)}
    progress = \prog
}

\frame{
    \pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\insertappendixendpage)} 
    progress = \prog
}

% \frame{
%     \pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\insertappendixstartpage-1)} 
%     progress = \prog
% }

% \frame{
%     \pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\insertpresentationendpage)} 
%     progress = \prog
% }

\end{document}

Question

Why is this the case, and how can I correct it?


Edit (based on Tom Bombadil's long comment)

Try adding this outside of a frame:

\typeout{======================================}
\show\insertdocumentendpage
\typeout{======================================}
\show\insertpresentationendpage
\typeout{======================================}
\show\insertappendixstartpage
\typeout{======================================}

The equal signs are just there so it's easier to find in the .log file. This will return

======================================
> \insertdocumentendpage=\long macro:
->\beamer@endpageofdocument .
l.27 \show\insertdocumentendpage
======================================
> \insertpresentationendpage=\long macro:
->{\ifx \beamer@startpageofappendix \@empty \beamer@tempcount =\beamer@endpageo
fdocument \relax \else \beamer@prevpage \beamer@startpageofappendix \relax \fi
\the \beamer@tempcount }.
l.29 \show\insertpresentationendpage
======================================
> \insertappendixstartpage=\long macro:
->{\ifx \beamer@startpageofappendix \@empty \beamer@tempcount =\beamer@endpageo
fdocument \relax \else \beamer@tempcount =\beamer@startpageofappendix \relax \f
i \the \beamer@tempcount }.
l.31 \show\insertappendixstartpage
======================================

Apparently, the problem is related to the fact that \insertdocumentendpage just returns a value, while \insertpresentationendpage and \insertappendixstartpage return an expression that needs to be evaluated first. So the question can be refined to either

  • How can one force \pgfmathparse to evaluate this first? or
  • How can one save the result of such an expression in a macro?
  • 2
    That's weired indeed. \xdef\x{\insertappendixstartpage} \x returns a number as expected, but a \pgfmathsetmacro{\prog}{\x} afterwards fails in the same way. – Tom Bombadil Oct 28 '13 at 21:08
5

You can also write your own macros for these and beamer code is really strange. However, as the name implies these should not be expected to be easy numbers since the numbers are inserted hence warning for nonexpansion.

\documentclass{beamer}
\usepackage{tikz}
\usetheme{Warsaw}
\makeatletter
\gdef\appendixstartpageno{%
\ifx\beamer@startpageofappendix\@empty%
  \beamer@endpageofdocument%
\else%
  \beamer@startpageofappendix%
\fi%
}
\gdef\presentationendpageno{%
\ifx\beamer@startpageofappendix\@empty%
  \beamer@endpageofdocument%
\else%
  \number\numexpr\appendixstartpageno-1\relax%
\fi
}
\makeatother
\begin{document}

\foreach \x in {1,...,17}{
\begin{frame}{Frame \x: Progress \pgfmathtruncatemacro{\prog}{round(100*(\insertpagenumber/\presentationendpageno))}\prog \%}
    Dummy frame \x
    \pgfmathtruncatemacro{\prog}{round(100*(\insertpagenumber/\presentationendpageno))}

    \tikz{\fill[blue!20](0,0) rectangle ({(\prog/100)*1cm},5mm) node[below right] {\prog \%};
          \draw(0,0) rectangle (1cm,5mm);
        }
\end{frame}
}


\appendix
\foreach \x in {1,...,10}{
\begin{frame}{Frame \x}
    Dummy appendix frame \x 
\end{frame}
}    
\end{document}

enter image description here

8

With appendix, you can use the TeX count \beamer@startpageofappendix.

\documentclass{beamer}

\makeatletter
\def\insertprogress{
  \pgfmathtruncatemacro{\prog}{100*(\insertpagenumber)/(\beamer@startpageofappendix - 1)}
  \prog
}
\makeatother

\begin{document}
\frame{progress = \insertprogress}
\frame{progress = \insertprogress}
\frame{before appendix; progress = \insertprogress}
\appendix
\frame{Appendix A}
\frame{Appendix B}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.