我想使用 tcolorbox 来调用定理环境。我的 tcolorbox 应该要求一个可选的标题和一个必需的标签,所以在 xparse 规范中我选择了O{}m
。然后,在定义中我使用
before upper={\begin{thm}[#1]\label{#2}},
当标题定义时,效果很好...但不幸的是,当没有定义标题时,这会失败并出现错误
Undefined control sequence.
\ll@thm ...t \thmtformatoptarg {\thmt@shortoptarg
}\fi
l.54 \begin{theorem}{thm:mygreatlabel}
我猜是因为thm
环境不接受像这样的空可选参数\begin{thm}[] ... \end{thm}
。我试图添加条件,但括号被视为真实字符,所以我不知道如何解决这个问题。
谢谢!
梅威瑟:
\documentclass{article}
\usepackage{mathtools}
\usepackage{amssymb, amsthm, amsmath}
\usepackage{thmtools} %%
\usepackage{hyperref}
\newtheorem{thm}{Theorem}[section]
\providecommand*\thmautorefname{Theorem}
\newtheorem{corollary}[thm]{Corollary}
\providecommand*\corollaryautorefname{Corollary}
\newtheorem{lemma}[thm]{Lemma}
\providecommand*\lemmaautorefname{Lemma}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
% \tcbset{customTheorem/.style={
% }
\NewTColorBox[auto counter, number within=section]{theorem}{O{}m}{ %
blank,
breakable,
before upper={\begin{thm}[#1]\label{#2}},
after upper={\end{thm}{\itshape See \hyperref[proof:#1]{proof} on page~\pageref{proof@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=theorem-\thetcbcounter.tex,
record={\string\myproof{\thetcbcounter}{theorem-\thetcbcounter.tex}{#2}},
% #1
}
\NewTotalTColorBox{\myproof}{mmm}{ %
blank,
breakable,
before upper={\begin{proof}[Proof of \autoref{#3}]\label{proof:#3}},
after upper={\end{proof}},
phantomlabel={proof@#1},
}{\input{#2}}
% \tcbset{no proof/.style={no recording,after upper=\end{thm}}}
\begin{document}
\section{Some theorems}
\tcbstartrecording % [theoremsamazing.records]
\begin{theorem}[My title]{thm:mygreatlabel}
This theorem works
\tcblower
Proof is super obvious
\end{theorem}
\begin{theorem}{thm:mygreatlabel}
This fails because the environment thm does not accept empty brackets.
\tcblower
Proof is not super obvious
\end{theorem}
See, I can refer to \autoref{thm:mygreatlabel}!
\lipsum[2]
\tcbstoprecording
\section{Some amazing theorems}
\tcbinputrecords
\end{document}
答案1
定理颜色箱的定义如下:
\NewTColorBox[auto counter, number within=section]{theorem}{om}{ %
blank,
breakable,
IfNoValueTF={#1}{before upper={\begin{thm}\label{#2}}}{before upper={\begin{thm}[#1]\label{#2}}},
after upper={\end{thm}{\itshape See \hyperref[proof:#1]{proof} on page~\pageref{proof@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=theorem-\thetcbcounter.tex,
record={\string\myproof{\thetcbcounter}{theorem-\thetcbcounter.tex}{#2}},
% #1
}
这里,不是使用带有空后备的可选参数,而是使用O{}
没有后备的可选参数,以区分有参数和无参数的情况。o
IfNoValueTF
请注意,\begin{theorem}[]{label}...\end{theorem}
明确给出的空可选参数仍然不起作用。