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
\documentclass{...}
and ending with\end{document}
.totcount
package on thesection
andsubsection
counters and then add the numbers ;-)