Defining a new command, every time a command is called

Short summary:

I would like to create a command that creates a new command each time it is called, based on a counter. Then I could use PGF's for-loops to go through the sequence of commands. The non-working example is:

\newcounter{group}
\newcommand{\defgroup}[1]{
\stepcounter{group}
\newcommand{\problem\Alph{group}}[0]{#1}
}
\defgroup{$1*1=$}
\defgroup{$1+1=$}


However, it turns out you cannot do \newcommand{\problem\Alph{group}}. Is there a way to do this?

Longer explanation:

I am writing a jigsaw activity, which requires a sequence of related versions of a document (A,B,C,D,E), each with a different problem. This is currently done as follows (MWE)

\documentclass[12pt]{exam}
\newcounter{group}
\newcommand{\groupname}{\stepcounter{group}\textbf{\Huge \Alph{group}\\}}
\newcommand{\newgroup}[1]{
\newpage
\groupname
\setcounter{section}{0}
\begin{center}
#1
\end{center}
\section{Now copy down other groups problems!}
}

\begin{document}
\newgroup{$1+1=$}
\newgroup{$1*1=$}
\newgroup{$1^1=$}
\end{document}


What I would like to do is be able to to reference the other groups in the second section of each version. Thus, I want group A's document to look like:

A

1. Here is your problem: 1+1 =

2. Now copy down other groups problems:

B: 1*1=

C: 1^1 =

• \expandafter\newcommand\csname problem\Alph{group}\endcsname{#1}%, but you will get into troubles if you have more than 26 problems and again, if you repeat your scheme, since \problemA etc. is tried to be redefined, leading to errors. As it stands, the first code fragment seems to be unrelated to the 2nd code – user31729 Feb 7 '18 at 19:34
• And you have spurious spaces in your macro ;-) – user31729 Feb 7 '18 at 19:37
• A complication is that you want to have knowledge of group B and Group C problems before ever invoking the \newgroup that would define them. In fact, as of group A, you don't even know how many other groups will follow. The traditional approach would be to somehow write things out to the aux file and force a 2nd compilation pass. – Steven B. Segletes Feb 7 '18 at 19:43

You can use \csdef from the etoolbox:

\documentclass{article}

\usepackage{etoolbox}

\newcounter{group}
\newcommand{\defgroup}[1]{
\stepcounter{group}
\csdef{problem\Alph{group}}{#1}
}

\begin{document}

\defgroup{$1*1=$}
\defgroup{$1+1=$}

\problemA

\problemB

\end{document}


\expandafter\newcommand\csname problem\Alph{group}\endcsname{#1}%

will define a macro named \problemX where X is the Alph - variant of the group counter value. The \expandafter will generate the macro name first, then then \newcommand grabs this macro name in order to define the relevant macro.

Please note that this approach will fail as soon as the group counter is reset to zero.

\documentclass[12pt]{exam}
\newcounter{group}

\newcommand{\defgroup}[1]{%
\stepcounter{group}%
\expandafter\newcommand\csname problem\Alph{group}\endcsname{#1}%
}

\begin{document}

\defgroup{$1+1=$}
\defgroup{$1*1=$}
\defgroup{$1^1=$}

\problemA

\problemB

\problemC

%\setcounter{group}{0} This will fail x
\defgroup{$1+1=$}
\defgroup{$1*1=$}
\defgroup{$1^1=$}

\end{document}


As I commented with regard to your longer explanation, a complication is that you want to have knowledge of group B and Group C problems before ever invoking the \newgroup that would define them. In fact, as of group A, you don't even know how many other groups will follow.

Thus, here, I force all problems to be defined at once in a comma separated list, and then the particulars can be invoked as needed. The answer will automatic adjust to the number of problems defined. (so go ahead, try \MakeProblemSheets{$1+1=$,$1*1=$,$1^1=$,$1/1=$} as an alternative).

\documentclass[12pt]{exam}
\usepackage{listofitems}
\newcounter{group}
\newcommand{\Groupname}[1]{\setcounter{group}{#1}\textbf{\Huge \Alph{group}\\}}
\newcommand{\Newgroup}[1]{
\newpage
\Groupname{#1}
\setcounter{section}{0}
\begin{center}
\problems[#1]
\end{center}
\section{Now copy down other groups problems!}
\begin{center}
\foreachitem\i\in\problems{%
\ifnum\icnt=#1\relax\else
\setcounter{group}{\icnt}
\makebox[.3in][l]{\Alph{group}:}%
\makebox[.7in][l]{\i}\\\fi
}
\end{center}
}
\newcommand\MakeProblemSheets[1]{%
\MakeProblemSheets{$1+1=$,$1*1=$,$1^1=$}