I want to create a macro \foo which creates another macro which a custom name set just before the execution of \foo via a macro \defcommand{myCustomName} and saves/adds something to it just as the following pseudo-latex-code suggests. Is it possible to translate this to a working LaTeX example?

%\defcommand needs to be defined
%addtocommand needs to be defined
\newcommand{\foo}[1]{
%If the command \???CustomString??? doesn't exist, create it and then add the content of #1 to it.
\addtocommand{???CustomString???}{#1}
}
Some text
\defcommand{customA}
\foo{A1} \foo{A2} \foo{A3} %should create a command named `customA` (instead of ???CustomString???) which I can use in the whole document.
\customA %this schould print A1 A2 A3
some text...
\defcommand{customB}
\foo{B1},\foo{B2} %now \foo should create another command named `customB`...
\customB %should print A1 A2 A3
\customA %should print B1 B2

Note that I don't want to use an argument of \foo to do this. I.e. I don't want to write \foo{Ax}{\customA}.

A syntax such as \foo{A}{1} would be preferable, unless the prefix is always one letter only.
–
egregOct 1 '12 at 14:26

In my use cases I would have to write lots of foo's wit the same second argument. For example \foo{A1}{\mymacroA}\foo{A2}{\mymacroA}\foo{A3}{\mymacroA}... so in my case it would much easier to write something like \defcommand{\mymacroA} and then just \foo{A1}\foo{A2}\foo{A3}...
–
studentOct 1 '12 at 14:32

If we say \defcommand{\mymacro} the effect will be to initialize \mymacro to have empty expansion. Then the (global) definition will make \foo@current@command expand to \mymacro (not to the contents of \mymacro, at least at the first expansion).

One has to know that \g@addto@macro adds its second argument to the (parameterless) macro given as first argument. So, assume we have declared \defcommand{\mymacro} and then we do \foo{x}: this becomes

\expandafter\g@addto@macro\foo@current@command{x}

and now \expandafter comes into action, changing \foo@current@command to its first level expansion, which gives \mymacro, so now we have

\g@addto@macro\mymacro{x}

which does the desired thing.

The more complex definition is used to ensure that no space is added in front of the first item we add:

Let's see assuming that now \defcommand{\bar} has just been given, so that now \foo@current@command expands to \bar, whose expansion is empty. As soon as we say \foo{abc}\foo{def}, TeX will transform it into

so that, now, the expansion of \bar will be abc and TeX will examine \foo{def}. This will do exactly the same as before, but following the "false" branch, because \bar is no longer empty. So we'll have

I just realized that my question was not clear enough... The first character in the argument of \foo doesn't have to match the (last character) of the custom macro. I.e. \defcommand{A} \foo{one}\foo{two}\foo{three} should add onetwothree to the macro \A (or \customA)
–
studentOct 1 '12 at 17:35

@student Maybe the third version is what you need.
–
egregOct 1 '12 at 17:59

...but I don't really understand why this works. With my very limited TeX knowledge I think that: When I call \defcommand{\macroA}, TeX creates an empty macro named \macroA and then puts this empty content to another macro called \foo@current@command. When then \foo{x} is called, \foo@current@command will contain x. When I finaly call \macroA I would expect that it prints nothing. I guess that \gdef\foo@current@command\macroA somehow ensures that \macroA contains everything of \foo@current@command since the last call of \defcommand but I don't see why.
–
studentOct 1 '12 at 20:03

Perhaps this is because my understanding of the \def command is to limited... Would be nice if you could add some more explanations that I can grasp why it works.
–
studentOct 1 '12 at 20:04