如何获取某一节中的定理数量

如何获取某一节中的定理数量

我如何获取特定部分(或任何用户确定的起点和终点之间)出现的定理的数量(即数量)并将其显示在文档中?

答案1

下面是定义一些命令的解决方案:

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

这将执行以下操作:

  • 定义一个新的计数器,每次调用时它将递增\theoremmarker
  • 每次调用\theoremmarker命令时,都会写入文件,该文件定义自上次使用以来使用了\jobname.aux多少个环境theorem\theoremmarker

该命令\theoremcount为您提供当前标记处使用的定理数量的值。

下面的代码写入和读取,jobname.aux因此您需要两次编译才能稳定。

% 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}

答案2

管理定理数量的一个简单方法是使用计数器:

在此处输入图片描述

\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}

\startcountingtheorems计数“区域”使用和指定\stopcountingtheorems,而计数器theoremcount包含最后计数的定理数量。

也可以将其扩展为\label-\ref类数量。这样,您就可以回忆起一定范围内的定理数量(可能是整个文档)任何地方在文档中。目前,您只能获得正确的计数 \stopcountingtheorems

答案3

一个简单的解决方案,使用标准 LaTeX 命令和计数器:

\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}

数字定理计数器

相关内容