I'm actually not sure if the title of this post is correct. However, what is happening is that the following MWE
\documentclass{article}
\begin{document}
\unexpanded{$\undefsym{x}$}
\end{document}
fails with
<recently read> \undefsym
l.3 \unexpanded{$\undefsym$}
I was hoping for this to work and to simply put in the output file the string $\undefsym$.
Am I missing something, and how can I fix it? In reality, I want to write a macro of the form
\newcommand{\addit}[1]
{\addtostream{outstream}{\unexpanded{#1}}}
and be able to feed it any arbitrary stream of characters for later processing (outstream will in fact be a tex file).
In response to egreg's last comment, here is what is going on: the author of the book in question, who is not me, wants the solutions that I write to be interleaved in the text of the book so that if problems move around the solutions can move with them. But the solutions obviously have to be separately processed since they turn into a separate book. So here is the solution that I ended up devising:
\newoutputstream{sol}
\openoutputfile{\jobname_sol.tex}{sol}
\newboolean{firstsection}
% starts the solutions file
\newcommand{\solutionmanuscript}
{}
% add chapter header lines to solutions
% (must manually add after each chapter command)
\newcommand{\addsolutionchapter}[1]% parameter is the chapter name
{%
\addtostream{sol}{\unexpanded{\chapter{#1}}}
\setboolean{firstsection}{true}
}
% add section header lines to solutions
% (must manually add after each section command)
\newcommand{\addsolutionsection}[1]%parameter is the section name
{
\ifthenelse{\boolean{firstsection}}{}{\addtostream{sol}{\unexpanded{\end{enumerate}}}}
\addtostream{sol}{\unexpanded{\section{#1}}}
\addtostream{sol}{\unexpanded{\begin{enumerate}}}
\setboolean{firstsection}{false}
}
% add a solution
\newcommand{\addsolution}[1]
{\addtostream{sol}{\noexpand\setcounter{enumi}{\theprobcounter}
\noexpand\addtocounter{enumi}{-1}}
\addtostream{sol}{\unexpanded{\item #1}}}
% ends the solutions file
\newcommand{\stopsolutionmanuscript}
{\addtostream{sol}{\unexpanded{\end{enumerate}}}}
This is probably more detail than you need or want. But the key stuff is in the \addsolution macro. Most of the other stuff is bookkeeping. In that macro, I needed a combination of \unexpanded and \noexpand to get the desired result (I wanted to expand the problem counter fed me by the text). I'd be happy to hear any better ways to do all of this.
This, among other things, allows me to use my own math commands and macros when writing solutions. It probably makes the original tex source, which has interleaved text and solutions, harder to understand, though, since it has a mixture of two styles.
\unexpandedis prevents expansion inside an\edefor similar, but does not have any effect when executing material. Perhaps you want\detokenize? (Although that will insert a space after control sequences.) – Joseph Wright♦ Apr 19 '12 at 19:38\unexpandedTeX will keep expanding it in order to typeset it (like @JosephWright said) and I think all LaTeX's auxiliary output macros use\protected@writeinternally which uses\edef \reserved@a {\write #1{#3}}. The\edefremoves the\unexpandedand the\writethen expands its contents. So try it with\unexpanded{\unexpanded{$\undefsym{x}$}}instead. – Martin Scharrer♦ Apr 19 '12 at 19:45