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.

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?

share|improve this question
    
it can almost certainly be done with the use of an .aux file and multiple passes. –  barbara beeton Aug 2 '12 at 12:34
add comment

1 Answer

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}}%
  \addtocounter{#1}{\m@ne}%
  \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}
share|improve this answer
add comment

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.