%\newcommand{\solution}[1]{\textbf{Answer:} #1}
\newcommand{\solution}[1]{}
I post the assignment with the latter command, and then swap the
definition out when I post the answers. This normally works fine but
I find a weird interaction with the verbatim environment when I use
the former definition of \solution. Here's a sample snippet of the
file:
\solution{
\begin{verbatim}
***SOME PROGRAMMING COMMANDS HERE***
\end{verbatim}
} % <-- this is line 100
And the output:
$ latex homework.tex
[...]
Runaway argument?
***SOME PROGRAMMING COMMANDS HERE*** \end {verbatim}
! Paragraph ended before \@xverbatim was complete.
<to be read again>
\par
l.100 }
Does anyone have any ideas on what's going wrong here?
Many thanks,
Roger
You can't put \verb and the verbatim environment into arguments
to commands.
A way out is to use the "verbatim" package:
\newenvironment{verbsolution}{\verbatim}{\endverbatim}
%\newenvironment{verbsolution}{\comment}{\endcomment}
So you can write
\begin{verbsolution}
***SOME PROGRAMMING COMMANDS HERE***
\end{verbsolution}
With the uncommented definition the solution will come out. Otherwise
it will be ignored. You can also put something before \comment, in
the second definition, leaving some white space, for example.
There are other ways.
Ciao
Enrico
> <"sino...@gmail.com"> wrote:
>
> > For writing the answers to homework assignments in a class, I
> > typically use a self-defined "solution" command:
> >
> > %\newcommand{\solution}[1]{\textbf{Answer:} #1}
> > \newcommand{\solution}[1]{}
[...]
> > \solution{
> >
> > \begin{verbatim}
> > ***SOME PROGRAMMING COMMANDS HERE***
> > \end{verbatim}
> >
> > } % <-- this is line 100
> >
> > And the output:
> >
> > $ latex homework.tex
> > [...]
> > Runaway argument?
[...]
> You can't put \verb and the verbatim environment into arguments
> to commands.
>
> A way out is to use the "verbatim" package:
[...]
> There are other ways.
I think so too.
The first definition does print in bold-face the phrase "answer ".
The second definition does gobble away its argument.
As long as no assignments take place within \solution, you
might consider making the first definition without any argument
at all. This way the verbatim-code (and other solutions) is not
part of a macro-argument at all but is enclosed into a group
which should not matter:
%\newcommand{\solution}{\textbf{Answer:} }
\newcommand{\solution}[1]{}
You could also make an if-switch \ifsolution and
set \solutiontrue \solutionfalse in the preamble:
\newcommand\answerphrase{\textbf{Answer:} }
\newif\ifsolution
\solutiontrue
%\solutionfalse
\ifsolution
\answerphrase
\begin{verbatim}
***SOME PROGRAMMING COMMANDS HERE***
\end{verbatim}
\fi % <-- this is line 100
Ulrich
Many thanks Enrico, unfortunately I was also hoping to combine
verbatim and non-verbatim text in the solutions. Do you see a way to
do this with an environment?
Best
Roger
Dear Ulrich,
Many thanks! However,I have a followup question: I combined with this
an environment:
\newif\ifsolution
\solutiontrue
%\solutionfalse
\newenvironment{mysolution}{\ifsolution \textbf{Answer:}}{\fi}
With \solutiontrue this works fine, but with \solutionfalse I get the
following problem:
! Incomplete \iffalse; all text was ignored after line 104.
<inserted text>
\fi
<*> homework_1
[line 104 was the line where I inserted the \begin{mysolution}
command.] Do you have any ideas on why this could happen?
Many thanks,
Roger
> > You could also make an if-switch \ifsolution and
> > set \solutiontrue \solutionfalse in the preamble:
> >
> > \newcommand\answerphrase{\textbf{Answer:} }
> > \newif\ifsolution
> > \solutiontrue
> > %\solutionfalse
> >
> > \ifsolution
> > \answerphrase
> > \begin{verbatim}
> > ***SOME PROGRAMMING COMMANDS HERE***
> > \end{verbatim}
> > \fi % <-- this is line 100
>
>I combined with this
> an environment:
[...]
> With \solutiontrue this works fine, but with \solutionfalse I get the
> following problem:
>
> ! Incomplete \iffalse; all text was ignored after line 104.
> <inserted text>
> \fi
> <*> homework_1
If the condition is false, (La)TeX does not expand or execute
trailing stuff until it encounters the closing \fi.
In your case the closing \fi should come from expanding a
sequence of trailing stuff².
Thus if the switch is "false", the "\fi" is never "seen" by
(La)TeX and at some stage of compilation you get an error.
²That trailing stuff is "\end{mysolution}",
that means an instance of the \end-macro whose argument is the
letter-character-token-sequence "mysolution".
Ulrich
using a conditional inside an environment decl isn't going to work
(because tex doesn't expand before looking for \fi, when it's
ignoring).
use the comment package (or similar) --
cf. http://www.tex.ac.uk/cgi-bin/texfaq2html?label=conditional
with comment.sty (if that's the way you choose to go):
\includecomment{handout} % maybe you don't need handout-specific stuff?
\excludecomment{cribsheet}
and then
...
\begin{cribsheet}
blah blah
\begin{verbatim}
lots of lovely code
\end{verbatim}
\end{cribsheet}
...
there are other ways to go about it all...
--
Robin Fairbairns, Cambridge