5

Maybe the question is not minimal enough - if so, please tell me.

I do not know what is the correct title since I do not understand the connection of the different packages.

Related

This question is somehow a follow up of

and also includes

Introduction

The basic idea is, that I calculate the progress (in percent) of a beamer presentation using

100*\insertframenumber/\inserttotalframenumber

I added a min(progress in percent,100) around that all because in the first run the \inserttotalframenumber is 1 and I do not want the progress to exceed 100.

At the moment I do not know which LaTeX package is responsible that I can use the min function here.

So it actually looks like

min((100*\insertframenumber/\inserttotalframenumber),100)

This fails if there is an appendix because I do not want that the appendix slides to be taken into account.

If there is an appendix then this approach

min((100*\insertframenumber/(\beamer@startpageofappendix-1))

works. The key is \beamer@startpageofappendix. But it fails if there is no appendix because then \beamer@startpageofappendix-1 is -1 (minus one).

Main Question

I thought that it would help if there was a check if \beamer@startpageofappendix is zero in the code. Depending on the result, approach a) (\inserttotalframenumber) or b) (\beamer@startpageofappendix-1) will be used.

Code

\documentclass[t]{beamer}

\usepackage{tikz}
\usepackage{calc}
\usepackage{xparse}

\ExplSyntaxOn
    \int_new:N \l_beamer_percentage_int
    \prop_new:N \g_beamer_percentage_prop
    \makeatletter
    \NewDocumentCommand{\myProgressPercent}{}{%
    \int_set:Nn  \l_beamer_percentage_int {
        \fp_to_int:n {
            \fp_eval:n {min((100*\insertframenumber/(\beamer@startpageofappendix-1)),100)}
            %\fp_eval:n {min((100*\insertframenumber/\inserttotalframenumber),100)}
            }
        } 
    \prop_gput:NnV \g_beamer_percentage_prop {percentage} { \l_beamer_percentage_int }
    \prop_item:Nn \g_beamer_percentage_prop {percentage}
    }
    \makeatother
\ExplSyntaxOff

\begin{document}

\foreach \n in {1,...,10}{
%------------------
\begin{frame}
    \frametitle{Slide \n}
    Progress is \myProgressPercent\,\%
\end{frame}
%------------------
}

%  comment \appendix in and out to see the effect
\appendix

\foreach \n in {1,...,3}{
%------------------
\begin{frame}
    \frametitle{Appendix Slide \n}
    Progress is \myProgressPercent\,\% (Should always be 100\,\%)
\end{frame}
%------------------
}

\end{document}

It wokred!

Thanks all - here's illustration of what I am trying to achieve.

enter image description here

2 Answers2

6

I'm not sure what's the need of using that indirection: the command can be even made fully expandable:

\documentclass[t]{beamer}

\usepackage{tikz}
\usepackage{calc}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\myProgressPercent}{}
 {
  \fp_to_int:n
   {
    min(
     100*\insertframenumber
     /
     (
      \use:c{beamer@startpageofappendix} - 1 > 0
      ?
      \use:c{beamer@startpageofappendix} - 1
      :
      \inserttotalframenumber
     )
     ,
     100
    )
   }
 }
\ExplSyntaxOff


\begin{document}

\foreach \n in {1,...,10}{
%------------------
\begin{frame}
    \frametitle{Slide \n}
    Progress is \myProgressPercent\,\%
\end{frame}
%------------------
}

%  comment \appendix in and out to see the effect
\appendix

\foreach \n in {1,...,3}{
%------------------
\begin{frame}
    \frametitle{Appendix Slide \n}
    Progress is \myProgressPercent\,\% (Should always be 100\,\%)
\end{frame}
%------------------
}

\end{document}

We check whether \beamer@startpageofappendix is set to some number (greater than one, of course) and, in this case we use it (less one). Otherwise we use \inserttotalframenumber.

Note that, for your intended application, you can even say

\includegraphics{\myProgressPercent.png}

because \myProgressPercent will just expand to a number.

egreg
  • 1,121,712
5
\documentclass[t]{beamer}

\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn

\int_new:N \l_beamer_percentage_int
\prop_new:N \g_beamer_percentage_prop

\NewDocumentCommand \myProgressPercent { }
  {
    \int_set:Nn  \l_beamer_percentage_int
      {
        \fp_to_int:n
          {
            \tl_if_empty:cTF { beamer@startpageofappendix }
              {
                \fp_eval:n
                  {
                    min(100*\insertframenumber/\inserttotalframenumber,100)
                  }
              }
              {
                \fp_eval:n
                  {
                    min(100*\insertframenumber/(\use:c {beamer@startpageofappendix}-1),100)
                  }
              }
         }
      }

    \prop_gput:NnV \g_beamer_percentage_prop {percentage} { \l_beamer_percentage_int }
    \prop_item:Nn \g_beamer_percentage_prop {percentage}
  }

\ExplSyntaxOff

\begin{document}

\foreach \n in {1,...,10}{
  % ------------------
  \begin{frame}{Slide \n}
    Progress is \myProgressPercent\,\%
  \end{frame}
  % ------------------
}

%  comment \appendix in and out to see the effect
%\appendix

\foreach \n in {1,...,3}{
  % ------------------
  \begin{frame}{Appendix Slide \n}
    Progress is \myProgressPercent\,\% (Should always be 100\,\%)
  \end{frame}
  % ------------------
}

\end{document}

enter image description here


A little simplified version.

\documentclass[t]{beamer}

\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn

\DeclareExpandableDocumentCommand \myProgressPercent { }
  {
    \tl_if_empty:cTF { beamer@startpageofappendix }
      {
        \fp_to_int:n
          {
            min(100*\insertframenumber/\inserttotalframenumber,100)
          }
      }   
      {
        \fp_to_int:n
          {
            min(100*\insertframenumber/(\use:c {beamer@startpageofappendix}-1),100)
          }
      }
  }

\ExplSyntaxOff

\begin{document}

\foreach \n in {1,...,10}{
  % ------------------
  \begin{frame}{Slide \n}
    Progress is \myProgressPercent\,\%
  \end{frame}
  % ------------------
}

%  comment \appendix in and out to see the effect
%\appendix

\foreach \n in {1,...,3}{
  % ------------------
  \begin{frame}{Appendix Slide \n}
    Progress is \myProgressPercent\,\% (Should always be 100\,\%)
  \end{frame}
  % ------------------
}

\end{document}
Henri Menke
  • 109,596