As I have stated already in other questions I am writing a problem/solution package. There are many of them, for sure, I have made much progress, using it in 'everydays' productivity for my needs, but is not designed for other users so far. I came across some strange behaviour, which I initially thought is my fault of wrong usage.
Basically I use a parent environment which is on one hand responsible for formatting the problem (configurable via xkeyval keys), on the other hand is is the driver, that stores the solution (contained in a similar environment, say called 'child') to an extern file, which is afterwards read at the position the user wants to have it.
The problem is now, that this will fail as soon as there are other commands handed over to the key values.
This is a MWE which fails during the \immediate\write process when the second parent call is done with the ChildOutputStyle=\YetAnotherOutputStyle.
\documentclass{article}
\usepackage{xkeyval}
\usepackage{xcolor}
\newcommand{\DefaultTextStyle}[1]{%
\textbf{\textcolor{blue}{#1}}%
}%
\newcommand{\YetAnotherOutputStyle}[1]{%
\textbf{\textcolor{green}{#1}}%
}%
\newwrite\OutputFileHandle%
\makeatletter%
\define@key{FamilyA}{ChildOutputStyle}[\DefaultTextStyle]{%
\def\KVMacroChildOutputStyle{#1}%
}%
\makeatother
\newcommand{\Child}[2][]{%
\setkeys{FamilyA}{#1}%
\KVMacroChildOutputStyle{#2}%
}% End of \newcommand{\Child} %
\newcommand{\Parent}[3][]{%
\setkeys{FamilyA}{#1}%
% Do some stuff before with 2nd arg of command ('problem')
#2\par %%%%%
%%%%
%
% Now write solution ('child') content to filehandle
\immediate\write\OutputFileHandle{%
\string\Child[#1]{#3}%
}%
}% End of \Parent command
\begin{document}
\presetkeys{FamilyA}{ChildOutputStyle={\DefaultOutputStyle}}{}%
\immediate\openout\OutputFileHandle=\jobname.myext
\textbf{\textcolor{violet}{First 'Parent' content}}%
\par
\Parent{One Ring To Rule Them All}{One Ring To Find Them} % I hope, Tolkien does not mind!
%%% This code breaks when tried to save it immediately to another file!!!!
\Parent[ChildOutputStyle=\YetAnotherOutputStyle]{When shall we three meet again}{In Thunder, lightning or in rain} % William Shakespeare will not mind ;-)
%%%%
\immediate\closeout\OutputFileHandle
\textbf{\textcolor{red}{Now 'Child' content}}%
\par
\input{\jobname.myext} % replace it afterwards with \IncludeIfFileExists... etc.
\end{document}
The compilation fails with this message
! Use of \XKV@resa doesn't match its definition.
\XKV@for@n #1#2#3->\XKV@tempa@toks {#1}\edef #2{
\the \XKV@tempa@toks }\ifx #...
l.57 ...t again}{In Thunder, lightning or in rain}
Is there any workaround (besides using the verbatim/moreverb packages?)
Some remarks at the end:
- I have replaced the
\newenvironmentstatements from my original package by\newcommands, to make the example shorter, but I prefer the environment style. - The
\DefaultTextStyleand\YetAnotherOutputStylecommands are just placeholders for some 'arbitrary' commands. - I omitted
\AtBeginDocumentfor opening the file andAtEndDocumentfor closing it also to keep the code short.
I am sure other users will profit from a solution as well.

\protected@write, but also to protect\textcolorwhich is fragile. See http://tex.stackexchange.com/questions/75951/how-to-write-the-symbol-into-a-file-when-using-package-utf8inputenc for\protected@iwrite. Add\protectbefore\textcoloror use\DeclareRobustCommandfor the styles. – egreg Mar 04 '14 at 18:54