如果只有一项索赔,则自动隐藏索赔编号

如果只有一项索赔,则自动隐藏索赔编号

在编写证明时,我经常需要包含一个声明,为此我定义了一个单独的环境。在我当前的设置中,声明是连续编号的,并且在每个证明中都会重置编号。这是通过我在序言中编写的以下代码实现的:

\newcounter{proofcount} 
\newtheorem{claim}{Claim}
\makeatletter
\@addtoreset{claim}{proofcount}
\makeatother


\AtBeginEnvironment{proof}{\stepcounter{proofcount}}
\theoremstyle{remark}

\makeatletter
\newtheorem*{cproof/}{Proof of claim \rev@cproofmark}

\newenvironment{cproof}[1][\@nil]
  {\def\@tmp{#1}%
   \ifx\@tmp\@nnil
       \def\rev@cproofmark{\theclaim}%
    \else
       \let\rev@cproofmark\@tmp
    \fi
   \renewcommand{\qedsymbol}{$\dashv$}%
   \pushQED{\qed}\begin{cproof/}}
  {\popQED\end{cproof/}}

\makeatother

现在,如果我的主张中只有一个证明,则上述内容会将该主张呈现为“主张 1”,这看起来(对我来说)很奇怪;我宁愿只说“主张”。当然,我可以通过为未编号的主张设置单独的环境来实现这一点,并在我的证明只需要一个主张时使用它,但我想知道是否有办法使该过程自动化?也就是说,有一个单一的主张环境,这样,在每个证明中,如果只有一个主张,则该主张没有编号,如果有多个主张,则它们被编号为主张 1、主张 2 等。

为了澄清起见,如果我写了类似的东西:

\begin{proof}
Proof starts.
\begin{claim}
This is a claim.
\end{claim}
\begin{cproof}
This is the proof the claim.
\end{cproof}
Main proof continues.
\end{proof}

\begin{proof}
Proof starts.
\begin{claim}
This is a claim.
\end{claim}
Proof continues.
\begin{cproof}
This is the proof the claim.
\end{cproof}
Main proof continues.
\begin{claim}
This is a claim.
\end{claim}
\begin{cproof}
This is the proof the claim.
\end{cproof}
Main proof continues.
\end{proof}

我得到:

在此处输入图片描述

我希望第一个证明中的“权利要求 1”显示为“权利要求”。我可以通过在我的序言中为未编号的权利要求设置不同的环境来实现这一点,但我想知道是否有办法自动执行此操作并仅使用一个环境。

答案1

由于只有在关闭证明环境时才能知道证明中的权利要求数量,并且这可能发生在定义第一个权利要求之后的许多页,因此抑制单个权利要求的编号并不容易。以下解决方案将每个证明中的权利要求数量存储到文件中.aux。您可能需要重新运行 LaTeX 才能获得正确的编号(如警告所示)。

\documentclass[11pt]{article}
\usepackage{etoolbox}
\usepackage{amsthm}
\usepackage{xspace}

\newcounter{proofcount}
\newtheorem{claimn}{Claim}
\newtheorem*{claimx}{Claim}
\makeatletter
\@addtoreset{claimn}{proofcount}
\makeatother

\makeatletter
\newif\if@claim@numbered
\newif\if@claim@rerun
\AtBeginEnvironment{proof}{%
  \stepcounter{proofcount}%
  \global\@claim@numberedtrue%
  \ifcsdef{proof\theproofcount}{%
    \ifnum\csuse{proof\theproofcount}=1\global\@claim@numberedfalse\fi%
  }{}%
}
\AtEndEnvironment{proof}{%
  \ifcsdef{proof\theproofcount}{%
    \ifnum\csuse{proof\theproofcount}=\theclaimn\else
    \global\@claim@reruntrue%
    \fi%
  }{\global\@claim@reruntrue}%
  \immediate\write\@auxout{%
    \string\csgdef{proof\theproofcount}{\theclaimn}}%
}
\newenvironment{claim}{%
  \if@claim@numbered\claimn\else\stepcounter{claimn}\claimx\fi%
}{%
  \if@claim@numbered\endclaimn\else\endclaimx\fi%
}
\AtEndDocument{%
  \if@claim@rerun
  \@latex@warning@no@line{Claim numbering may have changed.
    Rerun to get it right}%
  \fi
}
\makeatother
\theoremstyle{remark}

\makeatletter
\newtheorem*{cproof/}{Proof of claim\xspace\rev@cproofmark}

\newenvironment{cproof}[1][\@nil]
  {\def\@tmp{#1}%
   \ifx\@tmp\@nnil
     \if@claim@numbered
     \def\rev@cproofmark{\theclaimn}%
     \else
     \def\rev@cproofmark{}%
     \fi%
   \else
     \let\rev@cproofmark\@tmp
   \fi
   \renewcommand{\qedsymbol}{$\dashv$}%
   \pushQED{\qed}\begin{cproof/}}
  {\popQED\end{cproof/}}

\makeatother
\parindent 0pt

\begin{document}

\begin{proof}
The first proof starts, it only has one claim.
\begin{claim}
This is the single claim.
\end{claim}
\begin{cproof}
This is the proof of the single claim.
\end{cproof}
Main proof continues.
\end{proof}

\begin{proof}
The second proof starts, it has more claims.
\begin{claim}
This is the first claim.
\end{claim}
Proof continues.
\begin{cproof}
This is the proof of the first claim.
\end{cproof}
Main proof continues.
\begin{claim}
This is the second claim.
\end{claim}
\begin{cproof}
This is the proof of the second claim.
\end{cproof}
Main proof continues.
\end{proof}

\end{document}

结果是这样的: 结果

相关内容