我想使用 for 循环在序言中调用多个命令,以便在代码中的不同位置定义命令的数量。到目前为止,我的代码如下所示:
\documentclass[10pt,a4paper]{article}
% Here could be a list, e.g. \list = {Definition/definition, Axiom/axiom, ...}
% ...
\usepackage{thmtools} % boxes
\newcommand\mythm[3]{%
\declaretheorem[thmbox=M, within = section,]{#1}%
\newenvironment{#2}[1][]{%
\begin{#1}[##1]%
#3%
\normalfont%
}%
{\end{#1}}%
}
\mythm{Definition}{definition}{}
\mythm{Axiom}{axiom}{}
\mythm{Notation}{notation}{}
% ...
% Here could be a for-loop
\begin{document}
\begin{definition}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{definition}
\end{document}
我想在序言的开头创建一个列表(例如,\list = {Definition/definition,...}
然后\mythm
使用列表的内容调用(例如\foreach \x/\y in \list {\mythm{\x}{\y}{}
)。我的问题是,我不知道该怎么做。我尝试使用不同的 for 循环语法,但这些实现都无法真正调用\mythm
。有人可以帮我吗?
答案1
您只需几个步骤即可完成,但我看不到太大的收获。
\documentclass[10pt,a4paper]{article}
\usepackage{thmtools} % boxes
\NewDocumentCommand{\mythm}{mmm}{%
% #1 = tag, #2 = environment name, #3 = body font selection
\declaretheorem[
thmbox=M,
within=section,
name=#1,
]{#2inner}
\NewDocumentEnvironment{#2}{o}
{% start
\IfNoValueTF{##1}{\begin{#2inner}}{\begin{#2inner}[##1]}
\normalfont
\IfValueT{#3}{#3}%
}
{\end{#2inner}}
}
\NewDocumentCommand{\mythmslash}{>{\SplitArgument{2}{/}}m}{\mythm#1}
\NewDocumentCommand{\mythms}{>{\SplitList{,}}m}{\ProcessList{#1}{\mythmslash}}
\mythms{
Definition/definition,
Axiom/axiom,
Notation/notation/\itshape
}
\begin{document}
\section{Title}
\begin{definition}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{definition}
\begin{axiom}
Ceterum censeo Carthaginem esse delendam.
\end{axiom}
\begin{notation}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{notation}
\end{document}
请注意,您的\newenvironment
部分使可选的“标题”参数实际上是强制性的,而上面的代码实际上是可选的。
thmbox
说实话,我从来不会用。
答案2
\clist_map_inline:nn
以下是使用和\NewDocumentCommand
的参数处理器的解决方案\SplitArgument
。请注意,我\ifstrempty
在您的宏中添加了一个测试,\mythm
因为 定义的环境 \declaretheorem
不喜欢传递空的可选参数。
\documentclass{article}
\usepackage{xparse} % only necessary if your LaTeX is older than 2020-10-01
\usepackage{etoolbox} % for \ifstrempty
\usepackage{thmtools} % for boxes
\newcommand{\mythm}[3]{%
\declaretheorem[thmbox=M, within=section]{#1}%
\newenvironment{#2}[1][]{%
% The environments defined with \declaretheorem don't like being passed
% an empty optional argument.
\ifstrempty{##1}{\begin{#1}}{\begin{#1}[##1]}%
#3\normalfont
}%
{\end{#1}}%
}
\ExplSyntaxOn
\NewDocumentCommand \defineTheorems { m }
{
\clist_map_inline:nn {#1} { \defineOneTheorem {##1} }
}
\NewDocumentCommand \defineOneTheorem { > { \SplitArgument { 1 } { / } } m }
{ \mythm #1 { } }
\ExplSyntaxOff
\defineTheorems{Definition/definition, Axiom/axiom, Notation/notation}
\begin{document}
\section{A section}
\begin{definition}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{definition}
\begin{axiom}
covfefe
\end{axiom}
\begin{axiom}[foo]
The ``foo'' axiom.
\end{axiom}
\end{document}