# [OS X TeX] Tried comment environment in Memoir

Mon Apr 6 20:43:29 CEST 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.

\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{%
}%

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
\else
\fi
\def\genericoutfile{#1}%
\writeverbatim{#1}%

}{%

\endwriteverbatim
\ifodd\theexercise
\else
\fi
\@esphack

}

\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$.

$1/2$

\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$.

$1/2$

\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$.

$1/2$

\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$.

$1/2$

\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$.

$1/2$

\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$.

$1/2$

\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}

\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}!

$1/2$
\end{writeverbatim}

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

$1/2$

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}}
> 11 \protect\setcounter{pseudo}{\thechapter}}
> 12 \fi
> 14 \writeverbatim{tryout}}%
> 15 {\endwriteverbatim\finishwritefigure}
> 16 \newcommand{\finishwritefigure}{%
> 18 \newcommand{\printfigures}{%
> 19 \closeoutputstream{tryout}%
>
> So I am really puzzled.
>
> 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
>>>
>>> ?
>>>
>>> Note sure why this doesn't work:
>>>
>>> 	{\ifodd\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.
>>
>>
>> The main tricks are:
>>
>>
>>     to layout the answers as you want, but ...
>>
>> 3.  use  \excludecomment{evenanswer}  to bypass these
>>     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$?
>>>>> $-17$
>>>>> \end{writeverbatim}
>>>>>
>>>>> \end{exercise}
>>
>>
>>
>> The apparent extra \end{writeverbatim}  closes of
>> the  \begin{writeverbatim}  in the expansion of
>>
>> viz.
>>
>> 	{\ifodd\theexercise{%
>> {\theexercise}}%
>> 	 }\else{%
>> {\theexercise}}%
>> 	}\fi
>> 	\begin{writeverbatim}{backansout}}%
>> %
>> 	{\ifodd\theexercise{%
>> 	 }\else{%
>> 	}\fi}
>>
>> \newenvironment{oddexercise}[1]{\par\noindent\textbf{#1.}}{}
>> \newenvironment{evenexercise}[1]{\par\noindent\textbf{#1.}}{}
>>
>> %\includecomment{oddexercise}
>> \excludecomment{evenexercise}
>>
>>
>>> \end{Exercises}
>>>
>>>
>>> \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://tug.org/pipermail/macostex-archives/attachments/20090406/45132631/attachment.html>