Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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}. –  Christian Hupfer Jul 21 at 12:56
    
Do you mean the total number of frames??? –  Christian Hupfer Jul 21 at 12:57
    
No, I mean the total number of (sections + subsections) –  Tim S. Jul 21 at 12:57
1  
Try the totcount package on the section and subsection counters and then add the numbers ;-) –  Christian Hupfer Jul 21 at 13:00

1 Answer 1

up vote 5 down vote accepted

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}

enter image description here

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

share|improve this answer
    
Thank you, that works. Unfortunaly it requires two runs. –  Tim S. Jul 21 at 13:12
    
Almost anything connected to labels/counters within LaTeX requires two runs ;-) –  Christian Hupfer Jul 21 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 at 14:40
    
@GonzaloMedina: I know, therefore I used the improved version. The first version was in a hurry –  Christian Hupfer Jul 21 at 14:42
1  
@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! ;-) –  Christian Hupfer Jul 21 at 19:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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