我想在定义 thmxboxes 时删除大量冗余代码。我尝试使用 for-each 循环,但不起作用。以下代码(不起作用)应该可以让您了解我正在尝试做什么:
\documentclass[10pt,a4paper]{article}
\usepackage{thmtools} % boxes
\usepackage{pgffor} % for-Schleifen
\foreach \x in {Definition, Axiom}{
\foreach \y in {definition, axiom}{
\declaretheorem[thmbox=M, within = section,]{\x}%
\newenvironment{\y}[1][]{%
\begin{\x}%
% ... here may be more code
\normalfont%
}%
{\end{\x}}
}
}
% This works, but creates lots of redundant code
%\declaretheorem[thmbox=M, within = section,]{Definition}%
% \newenvironment{definition}[1][]{%
% \begin{Definition}%
% ... here may be more code
% \normalfont%
}%
% {\end{Definition}}
\begin{document}
\begin{definition}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{definition}
\end{document}
如果没有\foreach
(并更改 \x 和 \y),代码可以工作(但会产生大量冗余代码)。我希望我能够解释我的问题,并且有人可以帮助我!
答案1
据我理解意图,你实际上并不想要一个循环,只是一个特定的声明生成器,例如:
\documentclass[10pt,a4paper]{article}
\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}{}
\begin{document}
\begin{definition}[titel]
Ceterum censeo Carthaginem esse delendam.
\end{definition}
\end{document}