![自定义定理编号](https://linux22.com/image/249103/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AE%9A%E7%90%86%E7%BC%96%E5%8F%B7.png)
对于大师来说,这个应该很容易……
假设我想定义一个具有自定义编号方案的定理环境。也就是说,它不是根据某个计数器进行编号,而是根据传递的参数进行编号。因此,编写类似
\begin{customtheorem}{8}
Text.
\end{custom theorem}
会产生
定理8。文本。
我意识到我可以自己定义一个新环境,但这样标题就会变成粗体、小写字母或我定义的任何内容,因此如果我切换 documentclass,标题可能会与文档的其余部分不同。所以我想要一些也与常规定理标题相匹配的内容。
提前感谢您的任何想法。
答案1
一个简单的方法是
\documentclass{article}
%\usepackage{amsthm} %% uncomment to see the difference
\newtheorem{innercustomthm}{Theorem}
\newenvironment{customthm}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}
\begin{document}
\begin{customthm}{8}\label{eight}
Every theorem must be numbered by hand.
\end{customthm}
Here is a reference to theorem~\ref{eight}.
\end{document}
您还可以使用可选参数进行归因:
\begin{customthm}{99}[Somebody]\label{ninetynine}
Statement.
\end{customthm}
用于定义其中几个环境的更通用的接口;这不尊重定理风格;但它可以被适应。
\documentclass{article}
\usepackage{amsthm}
\newtheorem{innercustomgeneric}{\customgenericname}
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
\newenvironment{#1}[1]
{%
\renewcommand\customgenericname{#2}%
\renewcommand\theinnercustomgeneric{##1}%
\innercustomgeneric
}
{\endinnercustomgeneric}
}
\newcustomtheorem{customthm}{Theorem}
\newcustomtheorem{customlemma}{Lemma}
\begin{document}
\begin{customthm}{8}\label{eight}
Every theorem must be numbered by hand.
\end{customthm}
Here is a reference to theorem~\ref{eight} and
one to the important lemma~\ref{life-universe-everything}
\begin{customlemma}{42}\label{life-universe-everything}
This lemma explains everything.
\end{customlemma}
\end{document}
添加
cleveref
评论中询问了对的支持。
\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}
\usepackage{cleveref}
\newtheorem{innercustomgeneric}{\customgenericname}
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
\newenvironment{#1}[1]
{%
\ifdefined\crefalias\crefalias{innercustomgeneric}{#2}\fi
\renewcommand\customgenericname{#2}%
\renewcommand\theinnercustomgeneric{##1}%
\innercustomgeneric
}
{\endinnercustomgeneric}%
\ifdefined\crefname\crefname{#2}{#2}{#2s}\fi
}
\newcustomtheorem{customthm}{Theorem}
\newcustomtheorem{customlemma}{Lemma}
\begin{document}
\begin{customthm}{8}\label{eight}
Every theorem must be numbered by hand.
\end{customthm}
Here is a reference to \cref{eight} and
one to the important \cref{life-universe-everything}
\begin{customlemma}{42}\label{life-universe-everything}
This lemma explains everything.
\end{customlemma}
\end{document}
这些\ifdefined
位允许解决方案独立于cleveref
。
答案2
您可以暂时重置定理计数器:
\documentclass{article}
\usepackage{amsthm}
\newtheorem{Theorem}{Theorem}
\makeatletter
\newenvironment{customTheorem}[1]
{\count@\c@Theorem
\global\c@Theorem#1 %
\global\advance\c@Theorem\m@ne
\Theorem}
{\endTheorem
\global\c@Theorem\count@}
\makeatother
\begin{document}
\begin{Theorem}
Dummy text
\end{Theorem}
\begin{customTheorem}{99}
Dummy text
\end{customTheorem}
\begin{Theorem}
Dummy text
\end{Theorem}
\end{document}
答案3
如果只是为了几个定理,一个简单的方法是
\newtheorem*{theorem8}{Theorem 8}
答案4
另一种简单的方法适用于一些定理,但从技术上讲并不能解决确切的问题,因为它没有定义新的环境。
在声明定理之前,将计数器重置为您想要的数字之前的数字。
% For Theorem 9, with the default counter named "theorem"
\setcounter{theorem}{8}
% Now declare the theorem
\begin{Theorem}
Dummy text
\end{Theorem}