# Call multiple commands in preamble with loop

I want to call multiple commands in the preamble with a for loop in order to define the amount of commands at a different position in the code. So far my code looks like this:

\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}



I wanted to create a list at the beginning of my preamble (e.g.\list = {Definition/definition,...} and then call \mythm with the content of the list (e.g.\foreach \x/\y in \list {\mythm{\x}{\y}{}). My Problem is, that I have no idea how to do it. I tried it with different syntax of for-loops, but none of these implementations was able to actually call \mythm. Can someone help my with that?

• Problem solved? Aug 19, 2021 at 23:32
• Yes, sorry got ill and wasn't checking the web Aug 20, 2021 at 7:39

You can do it in a few steps, but I don't see much gain.

\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}


Note that your \newenvironment part makes the optional “title” argument actually mandatory, with the code above it is really optional.

I'd never use thmbox, to be honest.

Here is a solution using \clist_map_inline:nn and \NewDocumentCommand's \SplitArgument argument processor. Note that I added an \ifstrempty test inside your \mythm macro because the environments defined with \declaretheorem don't like being passed an empty optional argument.

\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}


• That would work with \expandafter\defineTheorems\expandafter{\tmp}, but since there would be a bunch of things executed between \newcommand{\tmp}{...} and \tmp's use, I'd choose a macro name that is less likely to be accidentally overwritten by unrelated code (e.g., \my@theorem@defs or something like that). Aug 25, 2021 at 15:15
• Thank you very much, it works! Because people were wondering: I'm working on a template, which can handle multiple styles and has some simple options. I have no idea if it is good and will be used by someone else... Is there a way to discuss something like that? Aug 26, 2021 at 12:08
• Sorry, I can't say—not very interested in templates myself. Aug 27, 2021 at 7:38