# \unexpanded fails with undefined LaTeX symbols?

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.

-
\unexpanded is prevents expansion inside an \edef or 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
If your simply write \unexpanded TeX will keep expanding it in order to typeset it (like @JosephWright said) and I think all LaTeX's auxiliary output macros use \protected@write internally which uses \edef \reserved@a {\write #1{#3}}. The \edef removes the \unexpanded and the \write then expands its contents. So try it with \unexpanded{\unexpanded{$\undefsym{x}$}} instead. – Martin Scharrer Apr 19 '12 at 19:45
User error. Question retracted. I would never have found it, though, without your help. Should I just delete the question? – rogerl Apr 19 '12 at 19:50
@rogerl: No, please keep it. It's not a bad question and might be useful for others. What exactly fixed it? – Martin Scharrer Apr 19 '12 at 19:51
The actual material I am writing to an auxiliary file is tex for later processing. The later processing uses a different set of macros than the original file does. There was an option in the macro to also reproduce the exported text at the point of export in the original tex file (original usage called for the same set of macros to be available at the first run as well as when the auxiliary output was processed). So the original processing attempted to evaluate \undefsym when placing the output in the first output file; the export itself was done perfectly. – rogerl Apr 19 '12 at 19:54

## 1 Answer

The question has been added to to further explain both the problem and the solution.

-
If possible it is better to separate the question and the answer. – qubyte May 9 '12 at 4:44