修复 exsheets 问题和解决方案环境中的 lstlisting

修复 exsheets 问题和解决方案环境中的 lstlisting

我正在尝试使用exsheets来准备编程基础考试。我需要在一些question和中包含 C 代码片段solution,我将使用listings(使用最小工作示例question下面)。然而,我意识到如果将代码注入到或中,代码就会变得混乱solution

你知道如何解决这个问题吗?


最小工作示例

\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}

答案1

您可以先将列表框起来,然后在问题和/或解决方案中使用该框:

\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}

在此处输入图片描述

答案2

我被引导到这篇文章,因为我也遇到了verbatim问题exsheets。再谷歌一下,我找到了这个包保护它应该能解决我们所面临的问题(在本来就不应该使用的环境中使用逐字翻译:-))

我在原始示例中添加了一个\usepackage{cprotect} ,并\cprotEnv在问题和解决方案环境前面放置了一个:

\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}

答案3

自 0.10 版本(2013/10/24)起,exsheets附带一个名为的附加包,exsheets-listings该包提供环境lstquestionlstsolution可使用:

\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}

在此处输入图片描述

相关内容