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.

  • 2
    \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, 2012 at 19:38
  • 1
    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. Apr 19, 2012 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, 2012 at 19:50
  • 1
    @rogerl: No, please keep it. It's not a bad question and might be useful for others. What exactly fixed it? Apr 19, 2012 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, 2012 at 19:54

1 Answer 1


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

  • 1
    If possible it is better to separate the question and the answer.
    – qubyte
    May 9, 2012 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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