[OS X TeX] Tried comment environment in Memoir

David Arnold dwarnold45 at suddenlink.net
Mon Apr 6 14:43:29 EDT 2009


All,

For those who have been following this thread, here is a post by  
Ulrich from comp.text.tex that I find very helpful. Much thanks to  
Ross and Ulrich for helping us with this project.

David wrote:
 > The only sore point in my eyes is having to defer the \end
 > {writeverbatim} into the backanswer environment

Most of the verbatimizing-environments "scan" their
verbatimized input for a string
"\end{<environment-name>}" which is constructed
from the current environment's name which is held in
the macro \@currenvir, which in turn is set by the
\begin-macro.
When defining other environments in terms of
memoir's or comment's verbatim-like-environments,
don't use within your environment the \begin{environment}
..\end{environment}-Syntax but call the underlying
\environment..\endenvironment-macros directly.

E.g., instead of

\newenvironment{myenvironment}{%
   \begin{writeverbatim}{myfile}%

}{%

   \end{writeverbatim}%


}

you'd better write:
\newenvironment{myenvironment}{%
   \writeverbatim{myfile}%



}{%
   \endwriteverbatim
}

This is explained, e.g., in the manuals of the
"verbatim"-package.
The former again executes the \begin-macro
and sets \@currenvir := writeverbatim
thus the \writeverbatim-mechanism scans for
\end{writeverbatim}.

The latter does not again execute the \begin-macro
and thus \@currenvir still is "myenvironment"
thus the \writeverbatim-mechanism scans for
\end{myenvironment}.

The following examole seems to work without problems.

Be aware that you cannot use your environments as
arguments of macros---e.g.,

   \@firstofone{%
     \begin{answer}
     <Answer>
     \end{answer}
   }%

will lead to errors.
Nesting these environments is impossible also.

Ulrich

\documentclass{memoir}
\usepackage{amsmath}
\usepackage{comment}
\chapterstyle{bianchi}

\newoutputstream{backansout}
\newoutputstream{solutionsout}

\newenvironment{Exercises}
{\openoutputfile{\jobname.backans}{backansout}
\openoutputfile{\jobname.solns}{solutionsout}}
{\closeoutputstream{backansout}
\closeoutputstream{solutionsout}}

\newcounter{exercise}[section]
\renewcommand{\theexercise}{\arabic{exercise}}

\newenvironment{exercise}%
{\refstepcounter{exercise}
\par\noindent\textbf{\theexercise}.}
{\par}

\makeatletter

\newenvironment{genericout}[1]{%
   \@bsphack
   \ifodd\theexercise
     \addtostream{#1}{\protect\begin{oddexercise}{\theexercise}}%
   \else
     \addtostream{#1}{\protect\begin{evenexercise}{\theexercise}}%
   \fi
   \def\genericoutfile{#1}%
   \writeverbatim{#1}%

}{%

   \endwriteverbatim
   \ifodd\theexercise
     \addtostream{\genericoutfile}{\protect\end{oddexercise}}%
   \else
     \addtostream{\genericoutfile}{\protect\end{evenexercise}}%
   \fi
   \@esphack


}

\newenvironment{backanswer}{%
   \genericout{backansout}%


}{%
   \endgenericout
}%

\newenvironment{solution}{%
   \genericout{solutionsout}%


}{%
   \endgenericout
}%

\newenvironment{oddexercise}[1]{\par\noindent\textbf{#1.}}{}
\newenvironment{evenexercise}[1]{\par\noindent\textbf{#1.}}{}
%\includecomment{oddexercise}
\excludecomment{evenexercise}

\begin{document}

\chapter{The Whole Numbers}

\section{Introduction to the Whole Numbers}

Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.
Now is the time for all good men to come to the aid of their country.

\subsection{Exercises}

\begin{Exercises}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}

Next, raise each side to the second power.
\begin{align*}
x&=\frac{1}{2}\\
x^2&\left(\frac12\right)^2\\
x^2&=\frac14
\end{align*}

This is the square of the solution.
\end{solution}

\end{exercise}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}
\end{solution}

\end{exercise}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}
\end{solution}

\end{exercise}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}
\end{solution}

\end{exercise}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}
\end{solution}

\end{exercise}

\begin{exercise}
Solve for $x$:\quad $2x+3=5-2x$.

\begin{backanswer}
$1/2$
\end{backanswer}

\begin{solution}
Isolate terms with $x$ on the left, simplify, then divide by the
coefficient of $x$.
\begin{align*}
2x+3&=5-2x\\
2x+2x&=5-3\\
4x&=2\\
\frac{4x}{4}&=\frac{2}{4}\\
x&=\frac{1}{2}
\end{align*}
\end{solution}

\end{exercise}

\end{Exercises}

\subsection{Answers}

\input{\jobname.backans}

\subsection{Solutions}

\input{\jobname.solns}

\end{document}



On Apr 5, 2009, at 10:14 PM, David Arnold wrote:

All,

Aha! There is is the \end{writeberbatim}!

\begin{backanswer}
$1/2$
\end{writeverbatim}
\end{backanswer}

However, this might scare a few of our faculty that are already  
nervous about Latex. Is there any way we can do this:

\begin{backanswer}
$1/2$
\end{backanswer}

And somehow get the \end{writeverbatim} into the enviroanment?

D.

On Apr 5, 2009, at 10:03 PM, David Arnold wrote:

> Ross,
>
> This works!
>
> Thanks. This is extremely helpful.
>
> I would never have found the \excludecomment macro, which is really  
> a treasure! I've never seen this one before.
>
> However, It is very hard for me to understand why we don't write a  
> closing \end{writeverbatim} (I've tried. It doesn't work.).
>
> In memman.pdf, there always seems to be a closing \endwriteverbatim.
>
> 1 \newoutputstream{tryout}
> 2 \openoutputfile{\jobname.fig}{tryout}
> 3 \newcounter{pseudo}
> 4 \renewcommand{\thefigure}{\thepseudo.\arabic{figure}}
> 5 \newenvironment{writefigure}{%
> 6 \ifnum\value{chapter}=\value{pseudo}\else
> 7 \setcounter{pseudo}{\value{chapter}}
> 8 \addtostream{tryout}{\protect\stepcounter{chapter}}
> 9 \addtostream{tryout}{\protect\addtocounter{chapter}{-1}}
> 10 \addtostream{tryout}{%
> 11 \protect\setcounter{pseudo}{\thechapter}}
> 12 \fi
> 13 \addtostream{tryout}{\protect\begin{figure}}
> 14 \writeverbatim{tryout}}%
> 15 {\endwriteverbatim\finishwritefigure}
> 16 \newcommand{\finishwritefigure}{%
> 17 \addtostream{tryout}{\protect\end{figure}}}
> 18 \newcommand{\printfigures}{%
> 19 \closeoutputstream{tryout}%
>
> So I am really puzzled.
>
> But because of your reply, progress is being made at this end,  
> which is nice, particularly after a long day of no progress.
>
> Thanks.
>
> D.
>
>
> On Apr 5, 2009, at 9:08 PM, Ross Moore wrote:
>
>> Hi David,
>>
>> On 06/04/2009, at 12:38 PM, David Arnold wrote:
>>
>>> OK. Tried the following, but no go. Getting this error:
>>>
>>> ! Argument of \verbatim at start has an extra }.
>>> <inserted text>
>>>               \par
>>> l.47 \begin{backanswer}
>>>
>>> ?
>>>
>>> Note sure why this doesn't work:
>>>
>>> \newenvironment{backanswer}%
>>> 	{\ifodd\theexercise{%
>>> 		\addtostream{backansout}{\par\noindent\textbf{\theexercise.}}
>>> 		\begin{writeverbatim}{backansout}}\else{\begin{comment}}\fi}
>>> 	{\ifodd\theexercise{%
>>> 		\end{writeverbatim}}\else{\end{comment}}\fi}
>>
>> This cannot work, as the first instance of
>>  \begin{writeverbatim}
>> will never finish.
>>
>> It never encounters the corresponding  \end{writeverbatim} ,
>> as the first (and every subsequent) \end{backanswer}  does not
>> expand --- since we are in verbatim-mode at that point.
>>
>>
>>
>>
>>
>>>
>>> Here's the full code example:
>>
>> The coding that I attach works just fine.
>>
>> <arnoldAnswers.tex>
>>
>> The main tricks are:
>>
>> 1.  write all the answers into the back-answer file,
>>     tagged as {evenanswer} or {oddanswer} environments;
>>
>> 2.  use \newenvironment{oddanswer}
>>     and \newenvironment{evenanswer}
>>     to layout the answers as you want, but ...
>>
>> 3.  use  \excludecomment{evenanswer}  to bypass these
>>     when you read the answers back in.
>>     If you choose to show all the answers, simply
>>     comment-out this single line!
>>
>> 4.  organise your questions&solutions as follows:
>>
>>
>>>>> \begin{exercise}
>>>>> What is the sum of $-2$ and $-15$?
>>>>> \begin{backanswer}
>>>>> $-17$
>>>>> \end{writeverbatim}
>>>>> \end{backanswer}
>>>>>
>>>>> \end{exercise}
>>
>>
>>
>> The apparent extra \end{writeverbatim}  closes of
>> the  \begin{writeverbatim}  in the expansion of
>> \begin{backanswer} .
>>
>> viz.
>>
>> \newenvironment{backanswer}%
>> 	{\ifodd\theexercise{%
>> 		\addtostream{backansout}{\protect\begin{oddexercise} 
>> {\theexercise}}%
>> 	 }\else{%
>> 		\addtostream{backansout}{\protect\begin{evenexercise} 
>> {\theexercise}}%
>> 	}\fi
>> 	\begin{writeverbatim}{backansout}}%
>> %
>> 	{\ifodd\theexercise{%
>> 		\addtostream{backansout}{\protect\end{oddexercise}}%
>> 	 }\else{%
>> 		\addtostream{backansout}{\protect\end{evenexercise}}%
>> 	}\fi}
>> 	
>> \newenvironment{oddexercise}[1]{\par\noindent\textbf{#1.}}{}
>> \newenvironment{evenexercise}[1]{\par\noindent\textbf{#1.}}{}
>>
>> %\includecomment{oddexercise}
>> \excludecomment{evenexercise}
>>
>>
>>> \end{Exercises}
>>>
>>> \subsection{Answers}
>>>
>>> \input{\jobname.backans}
>>>
>>> \end{document}
>>
>>
>> Hope this helps,
>>
>> 	Ross
>>
>> --------------------------------------------------------------------- 
>> ---
>> Ross Moore                                       ross at maths.mq.edu.au
>> Mathematics Department                           office: E7A-419
>> Macquarie University                             tel: +61 (0)2  
>> 9850 8955
>> Sydney, Australia  2109                          fax: +61 (0)2  
>> 9850 8114
>> --------------------------------------------------------------------- 
>> ---
>>
>>
>>
>> ----------- Please Consult the Following Before Posting -----------
>> TeX FAQ: http://www.tex.ac.uk/faq
>> List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
>> List Archive: http://tug.org/pipermail/macostex-archives/
>> TeX on Mac OS X Website: http://mactex-wiki.tug.org/
>> List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex
>>
>
> ----------- Please Consult the Following Before Posting -----------
> TeX FAQ: http://www.tex.ac.uk/faq
> List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
> List Archive: http://tug.org/pipermail/macostex-archives/
> TeX on Mac OS X Website: http://mactex-wiki.tug.org/
> List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex
>

----------- Please Consult the Following Before Posting -----------
TeX FAQ: http://www.tex.ac.uk/faq
List Reminders and Etiquette: http://email.esm.psu.edu/mac-tex/
List Archive: http://tug.org/pipermail/macostex-archives/
TeX on Mac OS X Website: http://mactex-wiki.tug.org/
List Info: http://email.esm.psu.edu/mailman/listinfo/macosx-tex


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://email.esm.psu.edu/pipermail/macosx-tex/attachments/20090406/45132631/attachment.html>


More information about the MacOSX-TeX mailing list