14

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}

3 Answers3

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

Gonzalo Medina
  • 505,128
  • 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? – Stefano Bragaglia Sep 04 '13 at 14:02
  • You are right, I forgot to include relsize package. I changed \smaller to \small as you did to fix the problem. – Stefano Bragaglia Sep 04 '13 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. – Forkrul Assail Sep 11 '13 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}
Thomas
  • 51
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

cgnieder
  • 66,645
  • 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 '13 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 '13 at 14:29
  • @Romildo The solution of Gonzalo would of course work with more than one listing – cgnieder Dec 18 '13 at 10:05