# How to pass a \def parameter to an inner command (StrSubstitute)?

I'm attempting to create 2 macros to simulate an array list as in Java for example. I'm using xstring StrSubstitute for this purpose, but I'm having a problem. Below is the code, it's a bit long as it contains the macros plus the test document.

The currently active StrSubstitute line is working as it is specifying the recipient of the substitution explicitly (\Cities).

The 1st commented StrSubstitute line (using \csname #1\endcsname) on the other hand causes the following TeX error :

! Extra \endcsname.
<argument> \csname testAL\endcsname



The 2nd commented line (using just #1) causes the following error:

! Missing control sequence inserted.
<inserted text>
\inaccessible


I tried to read some TeX resources, but I am not able to make it work and I find lots of possibilities (\def, \edef, \noexpand...)

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\RequirePackage{xstring}    %will use this package for the internal list structure

\def\ARRAYENDSENTINEL{@@}   %indicates array end
\def\ARRAYSEPARATOR{,}      %element separator

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Macro to create a new list:
%Parameters: <list name>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\newArrayList#1{%
\expandafter\def\csname#1\endcsname{\ARRAYENDSENTINEL}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Macro to append element on a list:
%Parameters: <list name>, <element to add>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[\Cities] %working
%\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[\csname #1\endcsname] %NOT working!
%\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[#1] %NOT working either!
}

%opening
\title{Testing ArrayList}
\author{}

\begin{document}
\maketitle

\newArrayList{Cities}

Contents of Cities: \textbf{[\Cities]}

Contents of Cities: \textbf{[\Cities]}

Contents of Cities: \textbf{[\Cities]}

Contents of Cities: \textbf{[\Cities]}

\end{document}

-
Please always state the error you get. This can help a lot with tracking down the cause. Just saying "does not work" isn't very helpful. –  Martin Scharrer May 3 '11 at 0:13

You can't use \csname foo\endcsname at locations were \foo is required, e.g. \def\csname foo\endcsname{...} will redefine \csname with foo\endcsname as a parameter text and ... as replacement text. You need to expand \csname foo\endcsname first once to build \foo. The \expandafter primitive helps you here. It expands the token (=~ macro or character) after the following token once. So \expandafter\def\csname foo\endcsname{...} will expand to \def\foo{...} so that \foo is defined. A problem here is that you might need many \expandafter if you want to expand the \csname ...\endcsname at the end of a long line.

In this case it is better to either store the line in a temporary macro:

\def\addElementToArrayList#1#2{%
\begingroup
\def\temp{\endgroup\StrSubstitute[1]{\csname #1\endcsname}{\ARRAYENDSENTINEL}{\ARRAYSEPARATOR #2\ARRAYENDSENTINEL}[}%
\expandafter\temp\csname #1\endcsname]%
}


or by expanding the \csname ...\endcsname first and feeding it to a second macro:

\def\addElementToArrayList#1{%

Here we use two \expandafters in a row to "jump over" \addElementToArrayListX and {. Note that it is perfectly OK to read the second argument only with the second macro.
@John Kirollos: No, that doesn't work. You might just pass \Cities instead of Cities to your macro, then you can use #1 directly (in both places). –  Martin Scharrer May 3 '11 at 11:00