Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 '13 at 16:52
    
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. –  Jubobs Jan 13 at 14:50

2 Answers 2

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

share|improve this answer
    
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 4 '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 4 '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

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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