3

I'm writing a documentclass for creating exercise sheets and exams at school. For this purpose I created a boolean variable \g_test_showresults_bool which can be used to conditionally display the solutions/results of the exercises, so I can create one document without results for the students and one with the results for me.

I already created such a environment for "normal" content (text, math) but I am not able to create one for listings. I tried the solutions explained here but without success. I also found \lstnewenvironment, but I don't know how to include my boolean \g_test_showresults_bool here.

Here is my current template. I would also appreciate a macro instead of an environment.

\documentclass{article}

\usepackage{xparse}
\usepackage{listings}


\ExplSyntaxOn

\bool_new:N \g_test_showresults_bool
\bool_set_eq:NN \g_test_showresult_bool \c_true_bool

\NewDocumentEnvironment { reslstlisting } { } {

}{

}
\ExplSyntaxOff


\begin{document}

% Display this listing only if \g_test_showresults_bool is true
\begin{reslstlisting}
float i;
i = i + 6;
\end{reslstlisting}


\end{document}
  • 1
    I don't see an advantage of an expl3 \bool -'variable' over a traditional \newif\ifshowresult and \showresulttrue and \showresultfalse usage here – user31729 Jun 24 '17 at 16:20
  • @ChristianHupfer In the real documentclass I work with l3keys and tried to focus to the usage of LaTeX3. A solution compatible with l3keys would also be helpful. – dawu Jun 24 '17 at 16:29
  • l3keys also sets booleans. But your main problem is to produce the verbatim environment. Maybe you should search for a way to do that. Btw: Why don't you use one of the existing exercise packages? – TeXnician Jun 24 '17 at 17:19
  • @TeXnician I already tried to create a conditionally displayed verbatim (see the link I've added) for hours, but without success. That's why I am asking for help here. – dawu Jun 24 '17 at 17:31
  • See the xsim (specifically package xsimverb) manual page 50/51. That would support verbatim/listings... – TeXnician Jun 24 '17 at 17:44
4

Here's an adapted version from the xsimverb manual, which does the job for you.

\documentclass{article}
\usepackage{xsimverb,listings}

\makeatletter
\ExplSyntaxOn
\bool_new:N \g_test_showresults_bool
\bool_set_eq:NN \g_test_showresult_bool \c_true_bool
\NewDocumentEnvironment{reslstlisting}{}{%
    \XSIMsetfilebegin{\@percentchar\space file `\jobname.tmp'}
    \XSIMsetfileend{\@percentchar\space bye bye}
    \XSIMfilewritestart{\jobname.tmp}
}{
    \XSIMfilewritestop
    \bool_if:NT \g_test_showresult_bool
        {
            \lstinputlisting[language={[LaTeX]TeX}]{\jobname.tmp}
        }
}
\ExplSyntaxOff
\makeatother

\begin{document}
    Listing:
    \begin{reslstlisting}
bla bla \LaTeX
    \end{reslstlisting}
\end{document}
|improve this answer|||||
  • It works great, thanks! An addition (which can be found at the xsimverb manual): \XSIMsetfilebeginand \XSIMsetfileend add the specified content to the listing. I removed them, because I didn't need it. – dawu Jun 25 '17 at 10:29
  • @dawu I just did c&p with minimal modifications... – TeXnician Jun 25 '17 at 10:38
  • No problem! I just wanted to point out for other readers that these lines aren't necessary. – dawu Jun 25 '17 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.