# How can one dynamically insert the number of subsections of the current section?

In a certain section of my document, I want to reference the number of subsections in this section ("In the following X subsections ..."). Is there any way to do this programmatically?

-
it can almost certainly be done with the use of an .aux file and multiple passes. –  barbara beeton Aug 2 '12 at 12:34

Here's a \howmany command that takes as argument a label for referring to the number of times the counter given as optional argument (default subsection) has been stepped since last time it's been reset.

\documentclass{article}

\makeatletter
\newcommand{\howmany}[2][subsection]{%
\begingroup
\@namedef{the#1}{\arabic{#1}}%
\refstepcounter{#1}%
\label{#2}%
\endgroup}
\makeatother

\begin{document}
This document has \ref{sections} sections.
\section{X}
In the following \ref{subsecA} subsections we'll talk about many things.

\subsection{A}
\subsection{B}
\subsection{C}
\subsection{D}
\subsection{E}

\howmany{subsecA}

\section{Y}

\howmany[section]{sections}

\end{document}

-