I have a macro \Content wich contains several occurence of a macro \comm. The latter increases a counter a displays the value of the counter. Calling \Content gives the correct output. But i want to write this \Content into a file, and i encounter a problem. I guess, this is because \write does not like \stepcounter. I would like to understand why, and to find a workaround.
\documentclass{article}
\begin{document}
\newcounter{myCounter}
\newcommand{\comm}{%
\stepcounter{myCounter}%
var\arabic{myCounter} %
}
\def\Content{text \comm text \comm text \comm}
\Content %%%% This works
\newwrite\tempfile
\immediate\openout\tempfile=counter.txt
\immediate\write\tempfile{\Content}
\immediate\closeout\tempfile
\end{document}
PS : by the way, the error message is Missing \endcsname inserted. which i don'T really understand...
EDIT1 I formulate more precisely my question : the macro \Content is given, and i dont' want to modify it. I just know that it contains many occurences of \comm. And i want to write the content of \COntent into a file, but replacing each \comm by something like var1, var2 which increases with some counter.