I have several commands, called \cata
to \catd
. Now I need a command which loops over a given, comma-separated list, and calls the respective command. My current approach is:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\newcommand{\cata}{Hello a\newline}
\newcommand{\catb}{Hello b\newline}
\newcommand{\catc}{Hello c\newline}
\newcommand{\catd}{Hello d\newline}
\newcommand{\cat}[1]{%
\expandafter\newcommand\csname cat#1\endcsname%
}
\newcommand{\loopover}[1]{
\foreach \i in {#1}{
\cat\i}
}
\begin{document}
Hello World\\
\loopover{a, b, c, d}
\end{document}
My expected output would be:
Hello World
Hello a
Hello b
Hello c
Hello d
If I change \loopover{a,b,c,d}
to \loopover{a,a,c}
, I want to get this
Hello World
Hello a
Hello a
Hello c
Up till now I get the error LaTeX Error: Command \cata already defined.
, which makes sense. Is there another way to achieve my intention?
Edit: I would like to expand the commands afterwards, so the current case is maybe a bit misleading. I would like to get the command \cata
called itself, and not rewrite it.
The final application looks like:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tocloft}
\newcommand{\lista}{List a}
\newcommand{\listb}{List b}
\newcommand{\listc}{List c}
\newcommand{\listd}{List d}
\newlistof{cata}{expa}{\lista}
\newlistof{catb}{expb}{\listb}
\newlistof{catc}{expc}{\listc}
\newlistof{catd}{expd}{\listd}
\newcommand{\cata}[1]{%
\refstepcounter{cata}
\par\noindent\textbf{cata \thecata. #1}
\addcontentsline{expa}{cata}
{\protect\numberline{\thechapter.\thecata}#1}\par
}
\newcommand{\catb}[1]{%
\refstepcounter{catb}
\par\noindent\textbf{catb \thecatb. #1}
\addcontentsline{expb}{catb}
{\protect\numberline{\thechapter.\thecatb}#1}\par
}
\newcommand{\catc}[1]{%
\refstepcounter{catc}
\par\noindent\textbf{catc \thecatc. #1}
\addcontentsline{expc}{catc}
{\protect\numberline{\thechapter.\thecatc}#1}\par
}
\newcommand{\catd}[1]{%
\refstepcounter{catd}
\par\noindent\textbf{catd \thecatd. #1}
\addcontentsline{expd}{catd}
{\protect\numberline{\thechapter.\thecatd}#1}\par
}
\newcommand{\cat}[2]{%
\expandafter\newcommand\csname cat#1\endcsname{#2}%
}
\newcommand{\loopover}[2]{
\foreach \i in {#1}{
\cat\i{#2}}
}
\begin{document}
\tableofcontents
\newpage
\lista\newline
\listb\newline
\listc\newline
\listd\newline
\chapter{TestA}
\cat{a,b}{Test}
\end{document}
I would like to loop over the list {a,b}
given to \cat
, and call the respective command in order to add {Test}
to this table of content.
\newcommand\loopover[2]{\foreach\x in {#1}{\csname cat\x\endcsname{#2}}
, where's the problem?Undefined control sequence. \loopover{a,b}{Test}
\thechapter
, which is obvious as the class isarticle
. Usebook
.