I am experimenting a little bit with automatic generation of file handles via \newwrite.
I want to achieve following behaviour:
A user provides a list of file categories, for example Hints, Solutions, Problems etc. and a list of corresponding extensions, say
.hints,.sol,.prbin the correct order (how shouldTeXknow which category belongs to which extension and vice versa), e.g. as 1st and 2nd arguments to a command,\RegisterOutputFiles.The connection between the category and the extension is stored in two text labels with different names but same counter number, the basics are described in another question of mine(already solved, see details here: Writing text content as labels and refer to them with \nameref*)
\RegisterOutputFilesshould create the file handles with some nice name, to remember them, say\HintsFile,\SolutionsFileetc. and open them for writing via\immediate\openout\HintsFile=\jobname.hintsetc., all in a loop.
Now in the loop comes the failure, when the selfdefined command \NewWrite shows up the
error message
! Missing \endcsname inserted. <to be read again> \protect l.84 ....prb,.hints,.sol,.explain,.idea,.concepts}
I am pretty sure, that I miss some expansion or similar, but I can not figure out, since \NewWrite works with direct injection of words like Hints etc.
Here is the (usual non-working) MWE
\documentclass{article}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{blindtext}
\usepackage{morewrites}
\usepackage{xcolor}
\usepackage{hyperref}%
\newrobustcmd{\NewWrite}[1]{%
\expandafter\newwrite\csname#1\endcsname%
}%
%% Later on... Create a handle from #1 and directly combine
%% it with filename #2 with \immediate\openout%
\newrobustcmd{\NewWriteOpenOut}[2]{%
\expandafter\newwrite\csname#1\endcsname%
\immediate\openout\csname #1\endcsname=#2%
}%
\makeatletter
\newcounter{pc@@filecounter}%
\newcounter{pc@@totalfilecounter}%
\newcounter{pc@@loopcounter}%
%% Hopefully only text is in #1%
\newrobustcmd{\WriteOutputFileLabel}[1]{%
\refstepcounter{pc@@filecounter}%
\immediate\write\@auxout{%
\string\newlabel{pc::outputfile::\number\value{pc@@filecounter}}{{\thesection}{\thepage}{#1}{}}%
}% End of writing to AUX file
}%
\newrobustcmd{\WriteOutputFileNameLabel}[1]{%
\refstepcounter{pc@@filecounter}%
\immediate\write\@auxout{%
\string\newlabel{pc::outputfilename::\number\value{pc@@filecounter}}{{\thesection}{\thepage}{#1}{}}%
}% End of writing to AUX file
}%
\newrobustcmd{\RegisterOutputFiles}[2]{%
%%% Other code before
%
\setcounter{pc@@filecounter}{0}%
\forcsvlist{\WriteOutputFileLabel}{#1}% Store the output file `categories` to label names
\setcounter{pc@@totalfilecounter}{\number\value{pc@@filecounter}}%
\setcounter{pc@@filecounter}{0}%
\forcsvlist{\WriteOutputFileNameLabel}{#2}% Store the extensions to label 'names'
%
\noindent%
\textbf{\Large \textcolor{blue}{There are \number\value{pc@@totalfilecounter} file categories!}}
%
\setcounter{pc@@filecounter}{1}%
\setcounter{pc@@loopcounter}{\number\value{pc@@totalfilecounter}}
\addtocounter{pc@@loopcounter}{1}%
%
%
\begin{center}
\textbf{\textcolor{red}{Diagnostics}}
\end{center}
%
\whiledo{\number\value{pc@@filecounter} < \number\value{pc@@loopcounter}}{%
\noindent \nameref*{pc::outputfile::\number\value{pc@@filecounter}} \(\longrightarrow \) \jobname\nameref*{pc::outputfilename::\number\value{pc@@filecounter}}%
\stepcounter{pc@@filecounter}%
%% Creating the file handles file with error message `\protect`
\NewWrite{\nameref*{pc::outputfile::\number\value{pc@@filecounter}}}% %Creating file handle name `\anyname`
\newline%
} %
%%% Other code after
}%
\makeatother
\begin{document}
\RegisterOutputFiles{Problem,Hints,Solution,Explanation,Ideas,Concepts}{.prb,.hints,.sol,.explain,.idea,.concepts}%
\blindtext
\end{document}
Notes:
You have to remove the % before \NewWrite in the loop and run pdflatex twice to see the final error message, the first one complains about missing references.
The code is not finished and not polished, it is pre-stage of some concept study for another package.

\nameref*isn't fully expandable, so it's illegal in\csname...\endcsname. – egreg May 30 '14 at 20:37key value hash(category -> file extension), I tried other approaches also viaforcsvlistchoosing elements from the lists, but failed also. – May 30 '14 at 20:38\nameref, of course. But I should study the macros for understanding how you could do it. Surely not with\label; after all, you open the file after you have created the handle. – egreg May 30 '14 at 20:40\labelway. What is wrong with opening the file after creating the handle? It would be nice feature to open it 'just' after creating the handle, but that is also not mandatory. – May 30 '14 at 20:46x1from list1and theny1from2, being at the same position, but this is tedious. I already tried this, with\forcsvlistbut somehow the elements were not expaneded too. As far as I know, there are no hash arrays inTeX? – May 30 '14 at 21:05