4

This code

\documentclass{book}

\usepackage{answers}

\Opensolutionfile{foo}

\begin{document}

%\Writetofile{foo}{whatever} % OK
\Writetofile{foo}{\noexpand\chapter{bar}} % fails

\end{document}

produces this error

Output from handle foo going to foo.tex
(c:\eb\tex\q.aux)
! Incomplete \iffalse; all text was ignored after line 10.
<inserted text> 
                \fi 
l.10 \Writetofile{foo}{\noexpand\chapter{bar}}
                                               % fails

The construction worked perfectly on another computer (now broken) with a relatively new MiKTeX installation. On this windows 7 machine I'm running

C:\eb\tex>pdflatex -version
MiKTeX-pdfTeX 2.9.4902 (1.40.14) (MiKTeX 2.9)

I have looked at the related questions with a similar \iffalse error message, to no avail.

  • 2
    Use \protect: that's the LaTeX mechanism – Joseph Wright Mar 28 '15 at 20:12
  • Where do I put the \protect? Instead of \noexpand? Any idea why this used to work? Please post as answer so I can accept. – Ethan Bolker Mar 28 '15 at 20:15
  • 1
    @EthanBolker See tex.stackexchange.com/questions/194718/… for understanding why the package was updated in a way that doesn't allow \noexpand any more. – egreg Mar 28 '15 at 20:22
  • This interchange and the quick answer is a tribute to the wizards here. It took me much longer to isolate the MNWE from my big document than for you all to fix the problem. – Ethan Bolker Mar 28 '15 at 20:35
4

The answers package (now) uses the standard LaTeX2e \protect mechanism in writing to external files. The implementation detail here is unimportant, but the key outcome is that you should not use the TeX primitive \noexpand but rather the \protect command

\documentclass{book}

\usepackage{answers}

\Opensolutionfile{foo}

\begin{document}

%\Writetofile{foo}{whatever} % OK
\Writetofile{foo}{\protect\chapter{bar}} % fails

\end{document}

(Back-story: LaTeX2e's mechanism alters the definition of \protect depending on the context. Here, \noexpand is not the right choice.)

1

Here is an other solution

\documentclass{book}

\usepackage{answers}

\Opensolutionfile{foo}

\begin{document}

\begin{Filesave}{foo}
\chapter{bar}
\end{Filesave}



\makeatletter
\immediate\write\foo@file{\unexpanded{\section{too}}}
\makeatother


\end{document}

you can define new command with

\makeatletter
\newcommand{\mtwrite}[2]{%
\immediate\write\csname #1@file\endcsname{\unexpanded{#2}}}
\makeatother

and use it

\mtwrite{foo}{\chapter{too}}
  • Avoiding \write is the exact reason why answers.sty was modified. And \unexpanded is not correct, because there usually are things you want to be expanded in what you write out. – egreg Mar 28 '15 at 22:40
  • @egreg can you explain your comment? (for bad english user) what is the problem of \unexpanded? – touhami Mar 29 '15 at 7:12
  • Nothing in the argument to \unexpanded is expanded; if you want to have, say, \theanswer or any other current value, you won't get it. – egreg Mar 29 '15 at 9:47

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.