我想创建两个环境,problem
和subproblem
。我将使用problem
环境来定义主要问题,并subproblem
定义主要问题的变体。我希望将编号subproblem
作为problem
编号的子项。我的意思是,我喜欢有一些东西
Problem 1: Definition of Problem 1
Problem 2: Definition of Problem 2
Problem 1.1: Definition of the first variation of Problem 1
Problem 1.2: Definition of the second variation of Problem 1
Problem 2.1: Definition of the first variation of Problem 2
等等。
problem
我通过命令创建了环境\newtheorem
,第一个problem
是
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\begin{problem}
\label{prob1}
Definition...
\end{problem}
现在我想创建类似的subproblem
环境。我该怎么做,以便subproblem
引用关联的编号problem
?
答案1
您必须记住最后一个“子问题”编号。为此,problem
您计划定义变体的每个环境都必须有一个\label
。
在处理时problem*
,它会查看引用相同标签的最后一个数字并对其进行处理。
您还可以标记问题变化,如示例所示。
\documentclass{article}
\usepackage{amsthm}
\usepackage{refcount}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Problem}
\newenvironment{problem*}[1]
{%
\ifcsname problem@#1\endcsname
\expandafter\xdef\csname problem@#1\endcsname{%
\the\numexpr\csname problem@#1\endcsname+1\relax
}%
\else
\expandafter\xdef\csname problem@#1\endcsname{1}%
\fi
\edef\thesubproblem{%
\getrefnumber{#1}.\csname problem@#1\endcsname
}%
\subproblem
}
{\endsubproblem}
\begin{document}
\begin{problem}\label{first}
Definition of Problem 1
\end{problem}
\begin{problem}\label{second}
Definition of Problem 2
\end{problem}
\begin{problem*}{first}
Definition of the first variation of Problem 1
\end{problem*}
\begin{problem*}{first}\label{secondoffirst}
Definition of the second variation of Problem 1
\end{problem*}
\begin{problem*}{second}
Definition of the first variation of Problem 2
\end{problem*}
As we saw in problem~\ref{secondoffirst}
\end{document}
请注意,添加时需要运行两到三次 LaTeX problem
,但您会收到消息
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
以防需要重新运行。
答案2
该宏\newtheorem
有一个选项,您可以使用它来引用其父级。因此,您可以使用它\newtheorem{subproblem}{Subproblem}[problem]
来实现您想要的(至少,如果我理解正确的话)。
\documentclass{article}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Subproblem}[problem]
\begin{document}
\begin{problem}
\label{prob1}
Definition ...
\end{problem}
\begin{subproblem}
\label{subprob1}
Definition ...
\end{subproblem}
\end{document}
输出结果如下:
如果您需要按随机顺序排列问题和子问题,您可以手动设置问题计数器:
\documentclass{article}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Subproblem}[problem]
\begin{document}
\begin{problem}
\label{prob1}
Definition ...
\end{problem}
\begin{problem}
\label{prob2}
Definition ...
\end{problem}
\setcounter{problem}{1}
\begin{subproblem}
\label{subprob11}
Definition ...
\end{subproblem}
\begin{subproblem}
\label{subprob12}
Definition ...
\end{subproblem}
\stepcounter{problem}
\begin{subproblem}
\label{subprob21}
Definition ...
\end{subproblem}
\end{document}