@egreg 实现的以下命令创建了一个可以轻松着色的新环境。我想构建一些类似的环境,其中使用不同的文本代替Typex
,用于推论、引理和其他情况。
我可以选择重复相同的代码。但是为了避免多次编写相同的代码,有没有更好的方法,也许从中导出其余部分\teora
?
\newcommand{\teoracolor}{}% initialize
\newtheorem{teorainner}{\color{\teoracolor}Typex}[section]
\NewDocumentEnvironment{teora}{D(){teora}}
{\renewcommand{\teoracolor}{#1}\teorainner}
{\endteorainner}
\colorlet{teora}{green!80!blue}
答案1
您定义所有必要部分的抽象。
我定义\newcolortheorem
的语法与相同\newtheorem
,但在括号中带有一个尾随可选参数,其中包含标签的颜色(可在运行时按照与上一个答案中指定的方式相同的方式进行更改)。
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}
\NewDocumentCommand{\newcolortheorem}{momoD(){black}}{%
% #1 = environment name
% #2 = shared counter
% #3 = label
% #4 = parent counter
% #5 = default color for the label
% first define the theorem-like environment
\IfNoValueTF{#4}{% no parent counter
\IfNoValueTF{#2}{% no shared counter
\newtheorem{#1inner}{\color{\theoremcolor}#3}%
}{% shared counter
\newtheorem{#1inner}[#2]{\color{\theoremcolor}#3}%
}%
}{% parent counter
\newtheorem{#1inner}{\color{\theoremcolor}#3}[#4]%
}%
% define the outer environment
\NewDocumentEnvironment{#1}{D(){#5}}{%
\renewcommand{\theoremcolor}{##1}\UseName{#1inner}%
}{%
\UseName{end#1inner}%
}%
}
\newcommand{\theoremcolor}{}% initialize
\colorlet{teora}{green!80!blue}
\newcolortheorem{teora}{Theorem}[section](teora)
\newcolortheorem{lemma}{Lemma}
\begin{document}
\section{Title}
\begin{lemma}
Standard lemma
\end{lemma}
\begin{lemma}(blue!50)
A blue lemma
\end{lemma}
\begin{teora}
This theorem is green, with no name.
\end{teora}
\begin{teora}[Name]
This theorem is green, with a name.
\end{teora}
\begin{teora}(red!80)
This theorem is red, with no name.
\end{teora}
\begin{teora}(red!80)[Name]
This theorem is red, with a name.
\end{teora}
\end{document}
具有键值系统的版本。
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}
\ExplSyntaxOn
\keys_define:nn { konmi/theorem }
{
name .tl_set:N = \l__konmi_theorem_name_tl,
label .tl_set:N = \l__konmi_theorem_label_tl,
shared .tl_set:N = \l__konmi_theorem_shared_tl,
parent .tl_set:N = \l__konmi_theorem_parent_tl,
labelcolor .tl_set:N = \l__konmi_theorem_labelcolor_tl,
}
\NewDocumentCommand{\newcolortheorem}{m}
{
\konmi_newtheorem:n { #1 }
}
\tl_new:N \l__konmi_theorem_setcolor_tl
\exp_args_generate:n { Ve }
% syntactic sugar
\cs_new_protected:Nn \__konmi_newtheorem_simple:nn
{
\newtheorem{#1_inner}{\color{\l__konmi_theorem_setcolor_tl}#2}
}
\cs_generate_variant:Nn \__konmi_newtheorem_simple:nn { VV }
\cs_new_protected:Nn \__konmi_newtheorem_shared:nnn
{
\newtheorem{#1_inner}[#3]{\color{\l__konmi_theorem_setcolor_tl}#2}
}
\cs_generate_variant:Nn \__konmi_newtheorem_shared:nnn { VVV }
\cs_new_protected:Nn \__konmi_newtheorem_parent:nnn
{
\newtheorem{#1_inner}{\color{\l__konmi_theorem_setcolor_tl}#2}[#3]
}
\cs_generate_variant:Nn \__konmi_newtheorem_parent:nnn { VVV }
% main function
\cs_new_protected:Nn \konmi_newtheorem:n
{
% evaluate the keys
\keys_set:nn { konmi/theorem }
{
% first reset, then apply the input
name =, label =, shared =, parent =, labelcolor = black, #1
}
% first define the theorem-like environment
\tl_if_empty:VTF \l__konmi_theorem_parent_tl
{% no parent counter
\tl_if_empty:VTF \l__konmi_theorem_shared_tl
{
\__konmi_newtheorem_simple:VV
\l__konmi_theorem_name_tl
\l__konmi_theorem_label_tl
}
{% shared counter
\__konmi_newtheorem_shared:VVV
\l__konmi_theorem_name_tl
\l__konmi_theorem_label_tl
\l__konmi_theorem_shared_tl
}
}
{% parent counter
\__konmi_newtheorem_parent:VVV
\l__konmi_theorem_name_tl
\l__konmi_theorem_label_tl
\l__konmi_theorem_parent_tl
}%
% define the outer environment
\__konmi_newtheorem_define:VV \l__konmi_theorem_name_tl \l__konmi_theorem_labelcolor_tl
}
\cs_new_protected:Nn \__konmi_newtheorem_define:nn
{
\NewDocumentEnvironment { #1 } {D(){#2}}
{
\tl_set:Nn \l__konmi_theorem_setcolor_tl { ##1 }
\use:c { #1_inner }
}
{
\use:c { end#1_inner }
}
}
\cs_generate_variant:Nn \__konmi_newtheorem_define:nn { VV }
\ExplSyntaxOff
\colorlet{teora}{green!80!blue}
\newcolortheorem{
name=teora,
label=Theorem,
parent=section,
labelcolor=teora,
}
\newcolortheorem{
name=lemma,
label=Lemma,
}
\begin{document}
\section{Title}
\begin{lemma}
Standard lemma
\end{lemma}
\begin{lemma}(blue!50)
A blue lemma
\end{lemma}
\begin{teora}
This theorem is green, with no name.
\end{teora}
\begin{teora}[Name]
This theorem is green, with a name.
\end{teora}
\begin{teora}(red!80)
This theorem is red, with no name.
\end{teora}
\begin{teora}(red!80)[Name]
This theorem is red, with a name.
\end{teora}
\end{document}