# How to get the sum of all sections and subsections in beamer

I want to determine how many pages I need for my toc which will be displayed in two coloumns with 10 (sub)sections each page.

So if I had 8 sections and 20 subsections the code should return 28.

Edit: If someone knows a solution with only one run it's highly appreciated.

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jul 21 '14 at 12:56
• Do you mean the total number of frames??? – user31729 Jul 21 '14 at 12:57
• No, I mean the total number of (sections + subsections) – user49121 Jul 21 '14 at 12:57
• Try the totcount package on the section and subsection counters and then add the numbers ;-) – user31729 Jul 21 '14 at 13:00

## 1 Answer

See the improved version below!!!

First: Quick and dirty solution: This works, as long there are no \chapter commands, which reset the section counter.

However, the subsection counter is reset by \section: Either one drops the resetting or a special redefinition of \subsection is applied, using another totalsubsection counter.

After all, the value is stored in the counter variable \mytotalcounter.

\documentclass{beamer}

\usepackage{totcount}

\newcounter{mytotalcounter}

\begin{document}
\regtotcounter{section}
\regtotcounter{subsection}

\newcounter{totalsubsection}

\regtotcounter{totalsubsection}%

\let\BeamerSubsection\subsection

\renewcommand{\subsection}[1]{%
\refstepcounter{totalsubsection}%
\BeamerSubsection{#1}%
}%

\setcounter{mytotalcounter}{\totvalue{section}}
\addtocounter{mytotalcounter}{\totvalue{totalsubsection}}

There are \themytotalcounter~section units

\section{A}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{B}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{C}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{D}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{E}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{F}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{G}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\section{H}
\subsection{A1}
\subsection{A2}
\subsection{A3}

\end{document}


Improved version

This does not redefine the \subsection command, but uses the \pretocmd macro from etoolbox package.

\documentclass{beamer}%
\usepackage{etoolbox}%
\usepackage{totcount}%
\usepackage{forloop}%

\newcounter{mytotalcounter}%
\newcounter{totalsubsection}%

\regtotcounter{totalsubsection}%
\regtotcounter{section}%

\AtBeginDocument{%
\pretocmd{\subsection}{\refstepcounter{totalsubsection}}{\typeout{Yes, prepending was successful}}{\typeout{No, prepending was not it was successful}}%

\CalculateTotalSectionSubsection%
}%

\newcommand{\CalculateTotalSectionSubsection}{%
\setcounter{mytotalcounter}{\totvalue{section}}%
\addtocounter{mytotalcounter}{\totvalue{totalsubsection}}%
}%

\newcounter{loopsections}
\newcounter{loopsubsections}

\begin{document}

There are \themytotalcounter~(sub)section units
\forloop{loopsections}{1}{\number\value{loopsections} < 9}{%
\section{Section \Alph{loopsections}}
\forloop{loopsubsections}{1}{\number\value{loopsubsections} < 4}{%
\subsection{Subsection \Alph{loopsections}.\arabic{loopsubsections}}
}
}%

\end{document}


The output does not change, so I kept the old screen shot

• Thank you, that works. Unfortunaly it requires two runs. – user49121 Jul 21 '14 at 13:12
• Almost anything connected to labels/counters within LaTeX requires two runs ;-) – user31729 Jul 21 '14 at 14:13
• Your redefinition of \subsection might be better; in its present form, you are suppressing the optional argument, so \subsection[titleA]{titleB} will produce undesired results. – Gonzalo Medina Jul 21 '14 at 14:40
• @GonzaloMedina: I know, therefore I used the improved version. The first version was in a hurry – user31729 Jul 21 '14 at 14:42
• @TimS: Don't worry, even if you provide a theme, most TeX documents require at least two runs, so an experienced user will have issue with that. Happy TeXing! ;-) – user31729 Jul 21 '14 at 19:36