2

My code looks like this

\documentclass{article}
\usepackage{environ}

\newcommand{\showsolution}{1}
\NewEnviron{mysolution}{
\if\showsolution1
    \fbox{\BODY}
\fi}

\begin{document}

\begin{mysolution}
    \begin{verbatim}
       code code code
    \end{verbatim}
 \end{mysolution}

 \end{document}

where the solution environment is created with the NewEnviron package as I want to display the solution depending on an if statement. However, if I now want to put code inside my solution by using verbatim I get the runaway argument / Paragraph ended before \next was complete. Edit: with the MWE above the error changes to missing \item. However, commenting out the verbatim inside the error disappears so it is still the same issue eventhough the error changed.

I guess the solution is something similar to the fragile option in beamer's frame environment but I dont know wether there are similar options for the environ package. So how can I place verbatim text (or at least code) inside an if statement?

11
  • Welcome to TeX.SE. Please don't post such fragments only. Post a complete document; as it stands there is no \if statement yet. And you environment ends with solution, not with mysolution ;-)
    – user31729
    Commented May 2, 2017 at 17:43
  • sorry, i edited it. Commented May 2, 2017 at 17:51
  • You can't press verbatim content into a macro, i.e. \fbox{\BODY} won't work here
    – user31729
    Commented May 2, 2017 at 17:53
  • But the error still appears even after removing the \fbox command. I.e. it is not possible to display code depending on an if-statement? Commented May 2, 2017 at 17:58
  • It should not even work without the \if statement, since \BODY is a macro too!
    – user31729
    Commented May 2, 2017 at 18:00

4 Answers 4

1

Thanks for all the replies. The suggestion by Steven was very nice however had the disadvantage that one would need multiple boxes (which have to be defined outside the actual solution environment) if one wished to write text between code snippets.

Another way is the lstinline command from the listings package. It works inside the solution environment and one can at least write inline code. But this packages offers another command, lstinputlisting which allows to display code from an external file. One can then specify with the options firstline and lastline which sections of the code one wants to display. Disadvantage of that approach is of course 1) one needs an additional file and 2) adding or deleting code requires to adjust the first/lastline options

\documentclass{article}
\usepackage{environ}
\usepackage{listings}
\usepackage{filecontents}
\begin{filecontents*}{ThisMWEasFile}
 1 blah &$ blah !
 2 Blah!@
 3 blah &$ blah @
 4 Blah!@
 5 blah &$ blah #
 6 Blah!@
 7 blah &$ blah $
 8 Blah!@
 9 blah &$ blah %
10 Blah!@
11 blah &$ blah ^
12 Blah!@
13 blah &$ blah &
14 Blah!@
15 blah &$ blah *
16 Blah!@
\end{filecontents*}

\newif\ifshowsolution
\showsolutiontrue

\NewEnviron{mysolution}{
\ifshowsolution
    \BODY
\fi}

\begin{document}
\begin{itemize}

    \item[Question 1:] Question with solution.\\
        \begin{mysolution}
            \lstinputlisting[firstline=1, lastline=5]{ThisMWEasFile.tex}
        \end{mysolution}
    \item[Question 2:] Question with solution.\\
        \begin{mysolution}
            \lstinputlisting[firstline=6, lastline=10]{ThisMWEasFile.tex}
            \textbf{text text text}
            \lstinputlisting[firstline=11, lastline=15]{ThisMWEasFile.tex}
        \end{mysolution}
\end{itemize}
\end{document}

enter image description here

1
  • 1
    I took the liberty of making your MWE self-contained. I hope you don't mind. Commented May 5, 2017 at 16:02
1

With the scontents package it is very easy to achieve what you are looking for. Everything stored in memory including the content verbatim. If you want you can also save the content to external files using the key write-env=file.tex. I modified your example file a bit, the output is the same :)

\documentclass{article}
\usepackage[store-env=solutions]{scontents}
%\newif\ifshowsolution
%\showsolutiontrue
%\ifshowsolution
\setupsc{print-env=true}
%\fi
\pagestyle{empty}
\begin{document}
\begin{itemize}

\item[Question 1:] Question with solution.\\
\begin{scontents}
\begin{verbatim}
 1 blah &$ blah !
 2 Blah!@
 3 blah &$ blah @
 4 Blah!@
 5 blah &$ blah #
\end{verbatim}
\end{scontents}
\item[Question 2:] Question with solution.\\
\begin{scontents}
\begin{verbatim}
6 Blah!@
7 blah &$ blah $
8 Blah!@
9 blah &$ blah %
10 Blah!@
\end{verbatim}
\textbf{text text text}
\begin{verbatim}
11 blah &$ blah ^
12 Blah!@
13 blah &$ blah &
14 Blah!@
15 blah &$ blah *
16 Blah!@
\end{verbatim}
\end{scontents}
\end{itemize}
\end{document}

output

0

Another option is to use the exam document class. With the solution environment you write your solutions and with the answers option you determine that you want your solutions to be printed out.

4
  • 1
    have you tested this? because I guess that the exam class uses a similar construction method as I do for the solution environment Commented May 5, 2017 at 12:49
  • Yes, I have. It is efficient and flexible. The exam class also has the boolean variable \ifprintanswers. You can use this variable to add further changes to the document beyond printing the answers with the answers option.
    – Andre
    Commented May 5, 2017 at 17:51
  • You can't use verbatim in the exam document class... it's indeed, the same problem.
    – tchevrier
    Commented Nov 6, 2022 at 11:20
  • @tchevrier I have just tested it. Using \documentclass[11pt,addpoints,answers]{exam}. I have tested \verb|...| and \begin{verbatim} content... \end{verbatim} inside and outside the solution environment. All work fine.
    – Andre
    Commented Nov 16, 2022 at 19:04
0

As pointed out by others, verbatim content cannot be processed as the argument to a macro. Here, however, the verbatimbox package allows verbatim content to be placed (unprinted) into a LaTeX box that can be later recalled by way of macro. Then, you can use the if condition to determine whether or not to execute the macro containing the verbatim content.

\documentclass{article}
\usepackage{verbatimbox}
\newif\ifsoln
\begin{document}
\begin{myverbbox}{\solutionA}
\code #$code code
\end{myverbbox}
Here, 
\solntrue
\ifsoln
\fbox{\solutionA}
\fi
is printed, whereas here,
\solnfalse
\ifsoln
\fbox{\solutionA}
\fi
it is not printed, based on an if condition.
\end{document}

enter image description here

Trying to employ the form of the OP's question, this might be useful:

\documentclass{article}
\usepackage{environ,verbatimbox}

\newif\ifshowsolution
\NewEnviron{mysolution}{
\ifshowsolution
    \fbox{\BODY}
\fi}

\begin{document}
\showsolutiontrue
\begin{verbbox}
code code code
\end{verbbox}

Here it shows:
\begin{mysolution}
    \theverbbox
\end{mysolution}

\showsolutionfalse
Here it does not:
\begin{mysolution}
    \theverbbox
\end{mysolution}

 \end{document}

enter image description here

3
  • thanks for this solution. However, what I don't like here is that the code block is defined outside the solution environment (I know and as stated above, this seems to be necessary by the construction of verbatim), but this is then very confusing. Consider a situation like this: code text code text code this would require me to define 3 verbatim boxes... Commented May 5, 2017 at 12:48
  • @SydAmerikaner What you say is correct, and I understand why you consider such an approach non-desirable. However, I will let the code stand until/unless a better answer is posted. Commented May 5, 2017 at 12:50
  • Sure! This is still a good work around and applicable if the solution for instance was code only or one was fine with writing text as comments inside the code (i.e. the verbatimbox). Commented May 5, 2017 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .