I'm attempting to automatically generate a BiBTeX file from within LaTeX. I'd like to expand all control sequences, but disable any active character expansion in the output. The idea is that the following pseudo-code:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\def\macro{ěščř}
\output{\jobname.output}{\macro žýáíé}
\end{document}
should generate a file containing ěščřžýáíé in UTF-8.
Plain TeX
I first arrived at this Plain TeX solution:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\def\macro{ěščř}
\immediate\newwrite\fd
\immediate\openout\fd=\jobname.output
\immediate\write\fd{\macro žýáíé}
\immediate\closeout\fd
\end{document}
This generated a file containing
\unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {e\global \mathchardef \accent@spacefactor \spacefactor }\accent 20 e\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {s\global \mathchardef \accent@spacefactor \spacefactor }\accent 20 s\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {c\global \mathchardef \accent@spacefactor \spacefactor }\accent 20 c\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {r\global \mathchardef \accent@spacefactor \spacefactor }\accent 20 r\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {z\global \mathchardef \accent@spacefactor \spacefactor }\accent 20 z\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {y\global \mathchardef \accent@spacefactor \spacefactor }\accent 19 y\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {a\global \mathchardef \accent@spacefactor \spacefactor }\accent 19 a\egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {\OT1\i \global \mathchardef \accent@spacefactor \spacefactor }\accent 19 \OT1\i \egroup \spacefactor \accent@spacefactor \unhbox \voidb@x \bgroup \let \unhbox \voidb@x \setbox \@tempboxa \hbox {e\global \mathchardef \accent@spacefactor \spacefactor }\accent 19 e\egroup \spacefactor \accent@spacefactor
Whoa. Inserting \input{\jobname.output} typesets the expected output, so this is clearly an expanded form of the ěščřžýáíé string, but not what I wanted.
The newfile LaTeX package
The \addtostream command
The next thing I tried was the \addtostream command provided by the newfile LaTeX package.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{newfile}
\begin{document}
\newoutputstream{out}
\openoutputfile{\jobname.output}{out}
\def\macro{ěščř}
\addtostream{out}{\macro žýáíé}
\closeoutputstream{out}
\end{document}
This generated a file containing
\IeC {\v e}\IeC {\v s}\IeC {\v c}\IeC {\v r}\IeC {\v z}\IeC {\'y}\IeC {\'a}\IeC {\'\i }\IeC {\'e}
Again, not what I wanted.
The writeverbatim environment
Next I tried the writeverbatim environment provided by the package.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{newfile}
\begin{document}
\newoutputstream{out}
\openoutputfile{\jobname.output}{out}
\begin{writeverbatim}{out}ěščřžýáíé\end{writeverbatim}
\closeoutputstream{out}
\end{document}
This generated a file containing ěščřžýáíé in UTF-8, just as I wanted, but I need control sequence expansion. Any ideas?

a,...,z,A, ...,Z. – Nicola Talbot Mar 22 '15 at 18:30\@onelevel@sanitize\macrobefore writing? – Nicola Talbot Mar 22 '15 at 18:37inputencpackage fixed it. – Witiko Mar 22 '15 at 19:31sed -i "s/\\\\IeC //g" jobname.output. – koppor Jan 09 '17 at 07:13