12.07.2015 Views

The memoir class - The UK TeX Archive

The memoir class - The UK TeX Archive

The memoir class - The UK TeX Archive

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.4. Files{\par\endwriteverbatim\end@nswer\@esphack}\fi % end of \ifdraftdoc ...\else ...When I was testing this code I had a surprise as I got nasty error messages from La<strong>TeX</strong>the first time around, but it worked fine when I processed the source a second time! <strong>The</strong>problem lies in the code line\addtostream{ansout}{\par\noindent\textbf{Answer \ref{#1}:}}%<strong>The</strong> first time around, La<strong>TeX</strong> processed the \ref command and of course it was undefined.In this case \ref gets replaced by the code to print the error message, whichinvolves macros that have @ in their names, which La<strong>TeX</strong> only understands under specialcircumstances. <strong>The</strong> second time around \ref gets replaced by the question number andall is well. I then remembered that some commands need protecting when they are writtenout, so I tried (I’ve wrapped the line to fit)\addtostream{ansout}{\par\noindent\protect\makeatletter\textbf{Answer\protect\ref{#1}:}\protect\makeatother}%which did work but seemed very clumsy.I then took another line of attack, and looked at the definition of \ref to see if I couldcome up with something that didn’t expand into @ names. <strong>The</strong> result of this was\addtostream{ansout}{\par\noindent\textbf{Answer\quietref{#1}:}}%In the kernel file ltxref.dtx I found the definition of \ref and it used a macro \@setref(shown below) to do its work. My \quietref locally changes the definition of \@setrefand then calls \ref, which will then use the modified \@setref.\def\@setref#1#2#3{% %% kernel definition\ifx#1\relax\protect\G@refundefinedtrue\nfss@text{\reset@font\bfseries ??}%\@latex@warning{Reference ‘#3’ on page \thepage \spaceundefined}%\else\expandafter#2#1\null\fi}\DeclareRobustCommand{\quietref}[1]{\begingroup\def\@setref##1##2##3{%\ifx##1\relax ??\else\expandafter##2##1\null\fi\ref{#1}\endgroup}Having gone all round the houses, the simplest solution was actually one that I hadskipped over\addtostream{ansout}{\par\noindent\textbf{Answer\protect\ref{#1}:}}%293

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!