I'd like to make a "codeillustration" environment that's something like:
\newenvironment{codeillustration}[1]{
\begin{samepage}
{\ill{#1}}
\VerbatimEnvironment
\begin{Verbatim}{\end{Verbatim}}
\end{samepage}
}
An example:
\codeillustration{Some great code.}{
code = 'great'
print(code)
}
would act like
\begin{samepage}
{\ill{Some great code.}}
\begin{verbatim}
code = 'great'
print(code)
\end{verbatim}
\end{samepage}
The codeillustration
I define above gives the following error:
! FancyVerb Error:
Extraneous input `{\end {Verbatim}} \end {samepage} {' between \begin{samepag
e}[<key=value>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}
That likely means I don't know how to program latex macros well.
I saw this question and read some of the fancyvrb docs, but I'm not seeing what to do. I can add samepage=true
to a custom verbatim environment, but it's not obvious how to get in the \ill
and keep it on the same page.
I'd also be fine with a lstlisting
example instead of verbatim. I just want to keep the \ill
(numbered headline) with the code all on the same page. These are short code examples in general.
EDIT:
I just saw this example in the lstlisting package to get a background color (different question, but shows customizing such a thing):
\begin{verbatimwrite}{temp.c}
/* the following code computes $\displaystyle\sum_{i=1}^{n}i$ */
for (i = 1; i <= limit; i++) {
sum += i;
}
\end{verbatimwrite}
\begin{mdframed}[backgroundcolor=yellow!10, rightline=false]
\lstinputlisting[language=C,mathescape,frame={}]{./temp.c}
\end{mdframed}
This makes me want to be able to wrap lstlisting
into a newenvironment
as well.
My point is, I just want my own codeillustration
environment, and to be able to play with the settings without changing the callers.
EDIT: Note \ill
is defined as
\newtheorem{ill}{Illustration}[chapter]