自定义环境编号:例如 1A、1B、2A、2B 等

自定义环境编号:例如 1A、1B、2A、2B 等

我在一篇论文中有三个主要例子,我一直在回顾它们。因此,我想按以下方式构建内容。一种反转编号/字母的解决方案(例如示例 A1、A2、B1、B2 等)在这个答案,但我更喜欢下面的样式。需要澄清的是,该解决方案通常需要与 hyperref 和 cross-ref 兼容。

我也愿意接受针对该问题的其他创造性解决方案。

结构

示例 1A。这是第一个例子的定义以及一些基本属性。

示例 2A。这是第二个示例的定义及其一些基本属性。

示例 3A。以下是第三个示例的定义及其一些基本属性。

(在引入定理 1 等之后)

示例 1B。定理 1 在示例 1 中的应用示例。

示例 3B。定理1到例3的一个例子应用。

(在引入定理 2 等之后)

示例 1C。定理 2 在示例 1 中的应用示例。

(本文稍后刊登)

示例 2B。对示例 2 的其他内容的示例应用。

(编辑:修改示例以强调编号/字母不是结构化的。)

答案1

我相信你需要提前规划一下你心中有多少种例子。

\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}

\newcommand{\newexample}[1]{%
  \theoremstyle{definition} % upright type for examples
  \newtheorem{example#1}{Example}%
  \expandafter\renewcommand\csname theexample#1\endcsname{#1\Alph{example#1}}%
}

\newexample{1}
\newexample{2}
\newexample{3}

\begin{document}

\begin{example1}\label{ex1-first}
Here is the definition of the first example with some elementary properties.
\end{example1}

\begin{example2}
Here is the definition of the second example with some elementary properties.
\end{example2}

\begin{example3}
Here is the definition of the third example with some elementary properties.
\end{example3}

\begin{theorem}
This is the first theorem
\end{theorem}

\begin{example1}
An example application of Theorem 1 to Example 1.
\end{example1}

\begin{example2}\label{ex2-second}
An example application of Theorem 1 to Example 2.
\end{example2}

\begin{example3}
An example application of Theorem 1 to Example 3.
\end{example3}

\begin{theorem}
This is the second theorem
\end{theorem}

\begin{example1}
An example application of Theorem 2 to Example 1.
\end{example1}

\begin{example2}
An example application of Theorem 2 to Example 2.
\end{example2}

\begin{example3}
An example application of Theorem 2 to Example 3.
\end{example3}

\ref{ex1-first} and \ref{ex2-second}

\end{document}

在此处输入图片描述

如果您始终具有相同的结构,即三个示例品种始终组合在一起,您可能会像下面这样操作。

\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}

\theoremstyle{definition} % upright type for examples
\newtheorem{exampleinner}{Example}

\newcounter{examples}
\newcounter{example}
\newenvironment{examples}
 {\setcounter{examples}{0}\stepcounter{example}}
 {}
\newenvironment{example}
 {%
  \renewcommand{\theexampleinner}{\theexamples\Alph{example}}%
  \stepcounter{examples}%
  \exampleinner
 }
 {\endexampleinner}

\begin{document}

\begin{examples}
\begin{example}\label{ex1-first}
Here is the definition of the first example with some elementary properties.
\end{example}

\begin{example}
Here is the definition of the second example with some elementary properties.
\end{example}

\begin{example}
Here is the definition of the third example with some elementary properties.
\end{example}
\end{examples}

\begin{theorem}
This is the first theorem
\end{theorem}

\begin{examples}
\begin{example}
An example application of Theorem 1 to Example 1.
\end{example}

\begin{example}\label{ex2-second}
An example application of Theorem 1 to Example 2.
\end{example}

\begin{example}
An example application of Theorem 1 to Example 3.
\end{example}
\end{examples}

\begin{theorem}
This is the second theorem
\end{theorem}

\begin{examples}
\begin{example}
An example application of Theorem 2 to Example 1.
\end{example}

\begin{example}
An example application of Theorem 2 to Example 2.
\end{example}

\begin{example}
An example application of Theorem 2 to Example 3.
\end{example}
\end{examples}

\ref{ex1-first} and \ref{ex2-second}

\end{document}

输出与以前相同。

答案2

也许我误解了要求,但以下内容不需要对示例数量进行硬编码。结构也不是固定的,即,您可以在每个块中使用任意数量的项目,跳过数字或字母等。这确实需要将\refstepcounter(或可能\setcounter)放在相关位置,无论是数字还是字母。

它的工作原理是定义两个计数器,一个用于字母,一个用于数字,每次字母计数器增加时,数字计数器都会重置\counterwithin。这会重置为 0,以便打印1A等等,而不是0A打印计数器值+1。

然后myexample定义一个环境,打印格式化的示例数字,设置命令的文本,并为with\label创建锚点。在环境结束时,数字会自动增加。hyperref\phantomsection

每当您想要转到下一个“块”时,您都需要手动调用\refstepcounter{exletter},即,它不与任何定理定义相关联。

\documentclass{article}
\usepackage{hyperref}
\usepackage{chngcntr}
\newcounter{exletter}
\newcounter{exnumber}
\setcounter{exletter}{1}
\setcounter{exnumber}{0}
\counterwithin{exnumber}{exletter}
\makeatletter
\newenvironment{myexample}{%
\textbf{Example \the\numexpr\value{exnumber}+1\relax\Alph{exletter}}%
\def\@currentlabel{\the\numexpr\value{exnumber}+1\relax\Alph{exletter}}%
\phantomsection%
}{\refstepcounter{exnumber}\par}
\makeatother

\begin{document}
\begin{myexample}
Here is the definition of the first example with some elementary properties.
\end{myexample}
\begin{myexample}
Here is the definition of the second example with some elementary properties.
\end{myexample}
\begin{myexample}
\label{thirdex}
Here is the definition of the third example with some elementary properties.
\end{myexample}

\refstepcounter{exletter}
\begin{myexample}
An example application of Theorem 1 to Example 1
\end{myexample}
\begin{myexample}
An example application of Theorem 1 to Example 2
\end{myexample}
\begin{myexample}
An example application of Theorem 1 to Example 3
\end{myexample}

\refstepcounter{exletter}
\begin{myexample}
An example application of Theorem 2 to Example 1
\end{myexample}
\refstepcounter{exnumber}
\begin{myexample}
\label{exappl}
An example application of Theorem 2 to Example 3
\end{myexample}

In Example \ref{exappl} a theorem is applied to Example \ref{thirdex}.
\end{document}

结果:

在此处输入图片描述

答案3

如果您的定理/示例设置足够一致,则以下将给出您想要的输出:

\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}

\theoremstyle{definition}
\newtheorem{example}{Example}[theorem]

% option 1
\renewcommand{\theexample}{%
\addtocounter{theorem}{1}%
\arabic{example}\Alph{theorem}%
\addtocounter{theorem}{-1}%
}

% option 2
%\renewcommand{\thetheorem}{\Alph{theorem}}
%\renewcommand{\theexample}{\arabic{example}\thetheorem}

\newcommand{\exampleautorefname}{Example}

\begin{document}

\begin{example}\label{egone}
Here is the definition of the first example with some elementary properties.
\end{example}
\begin{example}\label{egtwo}
Here is the definition of the second example with some elementary properties.
\end{example}
\begin{example}\label{egthree}
Here is the definition of the third example with some elementary properties.
\end{example}

\begin{theorem}\label{thmone}
This is the first theorem
\end{theorem}

\begin{example}
An example application of \autoref{thmone} to \autoref{egone}.
\end{example}
\begin{example}\label{thmoneEgtwo}
An example application of \autoref{thmone} to \autoref{egtwo}.
\end{example}
\begin{example}
An example application of \autoref{thmone} to \autoref{egthree}.
\end{example}

\begin{theorem}\label{thmtwo}
This is the second theorem
\end{theorem}

\begin{example}
An example application of \autoref{thmtwo} to \autoref{egone}.
\end{example}
\begin{example}
An example application of \autoref{thmtwo} to \autoref{egtwo}.
\end{example}
\begin{example}
An example application of \autoref{thmtwo} to \autoref{egthree}.
\end{example}

\autoref{egone} and \autoref{thmoneEgtwo}

\end{document}

但我发现示例 #C 引用了定理 2,这令人困惑。使用选项 2,可得到以下输出:

选项 2 输出

相关内容