Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm actually not sure if the title of this post is correct. However, what is happening is that the following MWE


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


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:



% starts the solutions file

% add chapter header lines to solutions
% (must manually add after each chapter command)
\newcommand{\addsolutionchapter}[1]% parameter is the chapter name

% add section header lines to solutions
% (must manually add after each section command)
 \newcommand{\addsolutionsection}[1]%parameter is the section name

% add a solution
    \addtostream{sol}{\unexpanded{\item #1}}}

% ends the solutions file

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.

share|improve this question
\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
show 3 more comments

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer
If possible it is better to separate the question and the answer. –  qubyte May 9 '12 at 4:44
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.