13

I am trying to use exsheets to prepare the tests for Foundation of Programming exam. I need to include snippets of C code in some question and solution and I am going to use listings (with the options specified in the minimal working example below). However I realised that the code is messed up if injected inside a question or a solution.

Do you know how to work around the problem?


Minimal Working Example

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{exsheets}

\lstset{
  frame=single,
  xleftmargin=20pt,
  numbers=left,
  numberstyle=\small,
  tabsize=2,
  breaklines,
  showspaces=false,
  showstringspaces=false,
  language=C,
  basicstyle=\small\ttfamily,
  commentstyle=\itshape\color{gray}
}

\begin{document}

\begin{question}{6}
Consider the following C program.
\begin{lstlisting}
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstlisting}
\end{question}

\begin{solution}
Consider the following C program.
\begin{lstlisting}
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstlisting}
\end{solution}

\pagebreak
\printsolutions 

\end{document}
2
  • exsheets uses environ's \NewEnviron internall which effectively means that the body of the question and solution are treated like arguments of a macro. As a consequence verbatim material is not allowed inside. @Gonzalo's solution thus is the way to go.
    – cgnieder
    Sep 4, 2013 at 16:52
  • 1
    Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jub0bs
    Jan 13, 2014 at 14:50

3 Answers 3

8

You can box your listing first, and then use the box in questions and/or solutions:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{exsheets}

\lstset{
frame=single,
xleftmargin=20pt,
numbers=left,
numberstyle=\small,
tabsize=2,
breaklines,
showspaces=false,
showstringspaces=false,
language=C,
basicstyle=\small\ttfamily,
commentstyle=\itshape\color{gray}}


\newsavebox\myboxa

\begin{document}

\begin{lrbox}{\myboxa}\begin{minipage}{\textwidth}
\begin{lstlisting}[]
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstlisting}
\end{minipage}
\end{lrbox}

\begin{question}{6}
Consider the following C program.\par
\noindent\usebox\myboxa
\end{question}

\begin{solution}
Consider the following C program.\par
\noindent\usebox\myboxa
\end{solution}
\printsolutions 

\end{document}

enter image description here

3
  • Hi @Gonzalo, thanks for your kind reply! I am not confident with minipages and boxes, however I would say that the solution is a bit cumbersome... I would like to put a question and a solution in a separate file to include, while I would have to add definitions to the preamble as well... am I right? Sep 4, 2013 at 14:02
  • You are right, I forgot to include relsize package. I changed \smaller to \small as you did to fix the problem. Sep 4, 2013 at 14:08
  • This helped a lot, I just spent an afternoon trying to panelbeat code into the pseudo environment, when Clemens pointed me to this post. Sep 11, 2013 at 20:26
5

I was directed to this post because I also had a verbatim problem with exsheets. Some more google directed me to the package cprotect which is supposed to solved exactly the problem we are facing (using verbatim environments where they should not be usable :-))

I got working the original example with the addition of a \usepackage{cprotect} and putting a \cprotEnv in front of the question and solution environments:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{exsheets}
\usepackage{cprotect}

\lstset{
  frame=single,
  xleftmargin=20pt,
  numbers=left,
  numberstyle=\small,
  tabsize=2,
  breaklines,
  showspaces=false,
  showstringspaces=false,
  language=C,
  basicstyle=\small\ttfamily,
  commentstyle=\itshape\color{gray}
}

\begin{document}

\cprotEnv \begin{question}{6}
Consider the following C program.
\begin{lstlisting}
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstlisting}
\end{question}

\cprotEnv \begin{solution}
Consider the following C program.
\begin{lstlisting}
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstlisting}
\end{solution}

\pagebreak
\printsolutions 

\end{document}
1
  • 1
    This breaks optional arguments to \begin{solution}!
    – MaxNoe
    Nov 11, 2016 at 12:07
2

Since version 0.10 (2013/10/24) exsheets comes with an additional package called exsheets-listings which provides the environments lstquestion and lstsolution that can be used:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{exsheets}
\usepackage{exsheets-listings}
\lstdefinestyle{mystyle}{
  frame=single,
  xleftmargin=20pt,
  numbers=left,
  numberstyle=\small,
  tabsize=2,
  breaklines,
  showspaces=false,
  showstringspaces=false,
  language=C,
  basicstyle=\small\ttfamily,
  commentstyle=\itshape\color{gray}
}
\SetupExSheets{
  question/listings={style=mystyle} ,
  solution/listings={style=mystyle}
}
\begin{document}

\begin{lstquestion}[pre=Consider the following C program.,points=6]
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstquestion}

\begin{lstsolution}[pre=Consider the following C program.]
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("hello, world\n");
}
\end{lstsolution}

% \pagebreak
\printsolutions 

\end{document}

enter image description here

3
  • This solution allows the inclusion of exactly one listing in a question, and it is awkward to use, as the text preceding or following the listing has to be passed using an option. What about if more than a listing is needed in a question? And what about other verbatim-like environments (minted, for instance)? Maybe it is reasonable to not use environ in the package at all. Can you comment on that?
    – Romildo
    Dec 17, 2013 at 14:01
  • @Romildo If I don't use environ I can't allow the selective inclusion of questions and solutions which is one of the main features and to me personally is more important than code listings.
    – cgnieder
    Dec 17, 2013 at 14:29
  • @Romildo The solution of Gonzalo would of course work with more than one listing
    – cgnieder
    Dec 18, 2013 at 10:05

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.