I have a local class that a number of people use. One of the new things I want it to do is write out various user-defined data to a file, so that a follow-up LaTeX document can do something different, but with the same user-defined parameters. I'm somewhat new to reading and writing external files, so my question may be elementary.
Here's an MWE that demonstrates the problem:
\documentclass{article}
\begin{document}
%% USER CONTROLS THIS DEFINITION
\def\userdef{abc} % Works Great
%\def\userdef{abc\\def} % Breaks \write
%% I CONTROL THE FOLLOWING
\def\userdefContent{\noexpand\def\noexpand\userdef{\userdef}}%
\newwrite\tempfile%
\immediate\openout\tempfile=userdata.tex
\immediate\write\tempfile{\userdefContent}
\immediate\closeout\tempfile
\end{document}
The user defines some known variable using a \def and I'd like to write it to a file in the form of \def\userdef{blah blah blah} where blah blah blah is how the user defined it. In this way, I can \input it into another file and have that second document pre-initialized with the same data the user defined for the first round.
The problem is that there are some user variables I want to write that may be populated with \\ characters which, apparently, break the \write. Uncomment the one line in the MWE to demonstrate this.
I am flexible on a solution. Ideally, I would like my output file to contain
\def \userdef {abc\\def}
but I would be willing to settle for
\def \userdef {abc*LINEFEED*def}
where *LINEFEED* is an arbitrary string sequence to alert me that I will later need to stick back in a \\. I would even, as a last resort, settle for
\def \userdef {abc def}
where the \\ has been stripped.
I suppose I would, if there were no other solution, instruct the userbase not to use \\ in their \def but some other macro that I define (which I can then redefine prior to the \write). But I think they might resist this last approach. I also would resist it, because it would break backward compatibility to older documents.
I am also open to a totally different approach, if there is a standard LaTeX way of transferring data between different documents. I would add that I have a stringstings solution that works when \userdef is composed purely of catcode 11 and 12 material and \\ characters, but I can't guarantee in advance that the user won't stick some other macros in his \def.
{\noexpand\def\noexpand\userdef{\userdef}}hoping to get\def\userdef{abc\\def}in the file, I instead get\def abc\\def{abc\\def}– Steven B. Segletes Apr 26 '13 at 15:02{\def\noexpand\noexpand\noexpand\userdef{\userdef}}– egreg Apr 26 '13 at 15:08\noexpandisn't a LaTeX command, use\protect(also\defisn't expandable anyway) so\protected@iwrite\tempfile{\let\\\relax}{\def\protect\userdef{\userdef}}– David Carlisle Apr 26 '13 at 15:11\noexpandand egreg said to use three of them. (we said same thing really though). – David Carlisle Apr 26 '13 at 15:13\protectapproach inside a class file (rather than inside a document), it left the\protectin the output, which was not desired. – Steven B. Segletes Apr 26 '13 at 15:45