4

How can I get the number (i.e. amount) of theorems that appear in a particular section (or between any user determined beginning and end point) and display it in the document?

Egbert
  • 899

3 Answers3

6

Here's a solution that defines a few commands:

\newcounter{egbertmarker}
\newcommand{\theoremmarker}{%
    \stepcounter{egbertmarker}
    \immediate\write\@auxout{%
        \string\definetotaltheoremcount\string{\theegbertmarker\string}\string{\the\value{dummytheorem}\string}
    }%
}

This does the following:

  • defines a new counter which will increment each time you call \theoremmarker
  • every time you call \theoremmarker a command will be written to the \jobname.aux file that defines how many theorem environments have been used since the last use \theoremmarker

The command \theoremcount gives you the value of the number of theorems in use at the current marker.

The code below writes and reads to jobname.aux so you'll need two compilations to stabilize.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{report}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}


\makeatletter
\newcommand{\definetotaltheoremcount}[2]{%
    \@ifundefined{c@totaltheorems\@roman{#1}}%
    {%
        \newcounter{totaltheorems\@roman{#1}}
        \setcounter{totaltheorems\@roman{#1}}{#2}
        \typeout{Defining a new counter: totaltheorems\@roman{#1}}
    }%
    {%
        \ifnum\value{totaltheorems\@roman{#1}}=#2
            \typeout{Total theorems for marker #1 match auxilary file (#2)}
        \else
            \typeout{Warning: total theorems for marker #1 updated from \the\value{totaltheorems\@roman{#1}} to #2-- recompile to fix}
        \fi
        \setcounter{totaltheorems\@roman{#1}}{#2}
    }%
}

% make the theorem environment increment 
% counter since last theorem
\newcounter{dummytheorem}
\AtBeginEnvironment{theorem}{\stepcounter{dummytheorem}}

% write the current number of theorems to a counter
\newcounter{egbertmarker}
\newcommand{\theoremmarker}{%
    \stepcounter{egbertmarker}
    \immediate\write\@auxout{%
        \string\definetotaltheoremcount\string{\theegbertmarker\string}\string{\the\value{dummytheorem}\string}
    }%
}

% command to count the theorems 
\newcommand{\theoremcount}{%
    \@ifundefined{c@totaltheorems\roman{egbertmarker}}%
    {%
        ??%
    }%
    {%
        \the\value{totaltheorems\roman{egbertmarker}}
    }%
    \setcounter{dummytheorem}{0}
}
\begin{document}

\newcount\tmp
\tmp=0
\loop
\begin{theorem}
    \lipsum[1]
\end{theorem}
\advance\tmp by 1
\ifnum\tmp<2
    \repeat

    \theoremmarker

    number of theorems since last marker:\theoremcount

    \tmp=0
    \loop
    \begin{theorem}
        \lipsum[1]
    \end{theorem}
    \advance\tmp by 1
    \ifnum\tmp<3
        \repeat

        \theoremmarker
        number of theorems since last marker:\theoremcount

    \tmp=0
    \loop
    \begin{theorem}
        \lipsum[1]
    \end{theorem}
    \advance\tmp by 1
    \ifnum\tmp<4
        \repeat

        \theoremmarker
        number of theorems since last marker:\theoremcount
\end{document}
cmhughes
  • 100,947
1

A simple way to manage counting the number of theorems is to use a counter:

enter image description here

\documentclass{article}
\usepackage{multido}% http://ctan.org/pkg/multido
\newtheorem{theorem}{Theorem}
\newcommand{\createtheorems}[1]{\multido{\i=1+1}{#1}{\begin{theorem}A theorem\end{theorem}}}

\newcounter{theoremcount}
\newcommand{\startcountingtheorems}{\setcounter{theoremcount}{\value{theorem}}}
\newcommand{\stopcountingtheorems}{\setcounter{theoremcount}{\numexpr\value{theorem}-\value{theoremcount}\relax}}

\begin{document}
\section{A section}
\startcountingtheorems% ================= START THEOREM COUNT
\createtheorems{7}
\stopcountingtheorems%  ================= STOP THEOREM COUNT
I counted~\thetheoremcount{} theorems.% 7 theorems

\section{Another section}
\createtheorems{2}
\startcountingtheorems% ================= START THEOREM COUNT
\createtheorems{4}

\section{Final section}
\createtheorems{5}
\stopcountingtheorems%  ================= STOP THEOREM COUNT
I counted~\thetheoremcount{} theorems.% 9 theorems
\end{document}

Counting "zones" are designated using \startcountingtheorems and \stopcountingtheorems, while the counter theoremcount contains the last counted theorem quantity.

It would be possible to extend this to be used as a \label-\ref-like quantity as well. In that way you could recall the number of theorems within a certain scope (could be the entire document) anywhere in the document. At the moment, you would only obtain the correct count after \stopcountingtheorems.

Werner
  • 603,163
1

A simple solution, using standard LaTeX commands and counters:

\documentclass{article}
\newtheorem{theorem}{Theorem}
\newcounter{numthrms}
\newenvironment{Theorem}{\begin{theorem}
                     \stepcounter{numthrms}}{%
                     \end{theorem}}
\newcommand{\numtheorems}{\thenumthrms}
\newcommand{\resetnumthrms{\setcounter{numthrms}{0}}                         
\begin{document} 
\begin{Theorem}
  My first theorem
\end{Theorem}
\begin{Theorem}
  My second theorem
\end{Theorem}
\begin{Theorem}
  My 3th theorem
\end{Theorem}
I have \numtheorems{} theorems.\resetnumthrms
\begin{Theorem}
  Another theorem
\end{Theorem}
Theorems added: \numtheorems.
\end{document}

Num theorems counter