I'd like to make a "codeillustration" environment that's something like:


An example:

\codeillustration{Some great code.}{
  code = 'great'

would act like

{\ill{Some great code.}}
  code = 'great'

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.


I just saw this example in the lstlisting package to get a background color (different question, but shows customizing such a thing):

/* the following code computes $\displaystyle\sum_{i=1}^{n}i$ */
for (i = 1; i <= limit; i++) {
sum += i;
\begin{mdframed}[backgroundcolor=yellow!10, rightline=false]

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


1 Answer 1


You're defining an environment and try to use a command. That's not how environments work.






\begin{codeillustration}{Some great code}
code = 'great'


enter image description here

  • Ah thanks. Yeah, \ill is defined as \newtheorem{ill}{Illustration}[chapter] in the book document class.
    – dfrankow
    May 30, 2021 at 19:55
  • @dfrankow If you define ill with \newtheorem, you should not use \ill, but \begin{ill} and \end{ill}. I changed the code to use it.
    – egreg
    May 30, 2021 at 20:03
  • Very helpful, thank you! I'm using code from someone else, and I don't know what I'm doing.
    – dfrankow
    May 30, 2021 at 20:16
  • @dfrankow It seems that your friend has very peculiar ideas of what environments are…
    – egreg
    May 30, 2021 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .