Speaking with Clemens (exsheets maintainer) he pointed me to a workaround by Gonzalo Medina, that helps put verbatim and listings code in the exsheets pseudo (Clemens' word choice) question and solution environments.
Fixing lstlisting inside exsheets question and solution environments
My question pertains to the repeatable sections in the solution to the previous link. Seeing the solution triggers the DRY (Don't repeat yourself) rules from my programming background and makes me want to write functional replacements for reusable sections.
How do I go about extracting the common code, so that I have a new function call (for lack of better phrasing), that I can reuse consistently, without replicating (read: copy pasting) the minibox code every time?
A MWE (modified from the mentioned question) to have a different box for question and 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}}
\newsavebox\myboxa
\newsavebox\myboxb
\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{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{lrbox}{\myboxb}\begin{minipage}{\textwidth}
\begin{lstlisting}[]
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello, world\n");
# some more things that had to be done
}
\end{lstlisting}
\end{minipage}
\end{lrbox}
\begin{question}{6}
Pre-example text\par
\noindent\usebox\myboxa
\\Post-example text
\end{question}
\begin{solution}
Pre-solution text\par
\noindent\usebox\myboxb
\\Post-solution text
\end{solution}
\printsolutions
\end{document}
This generates:

which would be much more usable, if I can extract the sections, indicated in overlay below:

If I can extract those sections and access them with something like:
\verbatimquestionsolution{6}{Preexample text}{Code}{Post-example text}{Pre-solution text}{Solution code}{Post-solution text}
or
\verbatimquestion{6}{Preexample text}{Code}{Post-example text}
\verbatimsolution{Preexample text}{Code}{Post-example text}
(with one's output immediately following other in output.
The pre and post solution text is not typically something I would use as one can do that in comments in the source, but is added here, for sake of discussing repeatable sections.
I know this might seem like a 'too specific' use case, but what I'm trying to learn here, is the extraction of sections (I'm not even really sure I'm using the right terminology here) so that I don't repeat myself. This won't just aid in this specific example but also with other repeatable environments I might encounter.
One thing I have no idea to approach for instance, is the
\newsavebox\myboxa
\newsavebox\myboxb
that comes in the preamble.
Any guidance as to this is much appreciated. What should I be reading up on here? Macros? How do I pass multiple variables? I see hooks are available but how do I get it to write to the preamble (if that is even necessary for the \newsavebox command)?


\ExplSyntaxOnand\ExplSyntaxOffis expl3. I've used it because it makes expansion control way easier and I needed the bodies of thequestionand thesolutionenvironments expanded. – cgnieder Sep 19 '13 at 09:04exsheets-listingsthat builds on the approach above but uses a key/value syntax for adding pre/post stuff. – cgnieder Sep 27 '13 at 12:09