推论的动态计数器

推论的动态计数器

我使用该包chngcntr对定理的推论进行编号,以便它们在下一个定理中归零,设置如下

\usepackage{chngcntr}
\counterwithin*{cor}{theo}

并且效果非常好,产生了如下结果:

Theorem 1
  Corollary 1
  Corollary 2
Theorem 2
  Corollary 1
Theorem 3
  Corollary 1
  Corollary 2

但我希望能够从定理 2 的孤立推论 1 中取出数字,并将其标记为简单的“推论”。

有什么想法可以如何自动完成此操作?

显示上述结果的代码非常无聊,但由于 SX 第一响应者要求它在这里:

\documentclass{report}

\usepackage{amsthm}

\newtheorem{teo}{Theorem}
\renewcommand{\theteo}{\arabic{teo}}

\newtheorem{col}{Corolário}
\renewcommand{\thecol}{\arabic{col}}


\newtheorem{cor}{Corollary}

\usepackage{chngcntr} \counterwithin*{col}{teo}

\begin{document}

\begin{teo}
The first theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\begin{teo}
The second theorem
\end{teo}

   \begin{col}
     Unique corollary to the theorem -- should have no number.
   \end{col}

\begin{teo}
The third theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\end{document}

答案1

一个可能的解决方案是xcntperchap,跟踪每个定理的推论数量并检查对于特定的定理是否只有一个推论,然而,这并不是开箱即用的方法——必须进行一些调整。

如果是这种情况,则\thecol\relax并且.通过补丁从推论标题中删除,但仅限于本地。

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}

\newtheorem{teo}{Theorem}
\renewcommand{\theteo}{\arabic{teo}}

\newtheorem{col}{Corolário}
\renewcommand{\thecol}{\arabic{col}}


\newtheorem{cor}{Corollary}

\usepackage{chngcntr} 
\counterwithin*{col}{teo}

\usepackage{xpatch}
\usepackage{xcntperchap}

\RegisterTrackCounter{teo}{col}


\makeatletter
\AtBeginEnvironment{col}{%
  % Check whether there is more than corollary for the current (!) teo environment. If there's only one, \let\thecol\relax and kick the `\the\thm@headpunct` out of `\@begintheorem
  \edef\corollary@num{\ObtainTrackedValueExp{teo}{col}}%
  \ifx\corollary@num\empty
  % Do nothing if the result of \ObtainTrackedValueExp is \empty
  \else
  % check if the number is 1
  \ifnum1=\corollary@num%
  \let\thecol\relax% silence \thecol
  % Kick the . in the theorem header
  \xpatchcmd{\@begintheorem}{\the\thm@headpunct}{}{}{}
  \addtocounter{col}{-1}%
  \fi
  \fi
}
\makeatother

\begin{document}
\begin{teo}
The first theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\begin{teo}
The second theorem
\end{teo}

   \begin{col}
     Unique corollary to the theorem -- should have no number.
   \end{col}

\begin{teo}
The third theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\end{document}

在此处输入图片描述

更新并重置推论

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}

\newtheorem{teo}{Theorem}
\renewcommand{\theteo}{\arabic{teo}}

\newtheorem{col}{Corolário}
\renewcommand{\thecol}{\arabic{col}}


\newtheorem{cor}{Corollary}

\usepackage{chngcntr} 
\counterwithin*{col}{teo}

\usepackage{xpatch}
\usepackage{xcntperchap}

\RegisterTrackCounter{teo}{col}


\makeatletter

\newif\if@resetcorollary
\AtBeginEnvironment{col}{%
  % Check whether there is more than corollary for the current (!) teo environment. If there's only one, \let\thecol\relax and kick the `\the\thm@headpunct` out of `\@begintheorem
  \edef\corollary@num{\ObtainTrackedValueExp{teo}{col}}%
  \ifx\corollary@num\empty
  \else
  \ifnum1=\corollary@num%
  \let\thecol\relax%
  \xpatchcmd{\@begintheorem}{\the\thm@headpunct}{}{}{}
  \addtocounter{col}{-1}%
  \fi
  \fi
}


\AtEndEnvironment{col}{%
  \if@resetcorollary
  \edef\corollary@num{\ObtainTrackedValueExp{teo}{col}}%
  \ifx\corollary@num\empty
  \else
  \ifnum1=\corollary@num%
  \setcounter{col}{0}%
  \fi
  \fi
  \fi
}

\@resetcorollarytrue

\makeatother

\begin{document}
\begin{teo}
The first theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\begin{teo}
The second theorem
\end{teo}

   \begin{col}
     Unique corollary to the theorem -- should have no number.
   \end{col}

\begin{teo}
The third theorem
\end{teo}

   \begin{col}
     The first corollary
   \end{col}
   \begin{col}
     The second corollary
   \end{col}

\end{document}

相关内容