1

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]

1 Answer 1

1

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

\documentclass{book}
\usepackage{fancyvrb}

\newenvironment{codeillustration}[1]
 {\VerbatimEnvironment
  \par\begin{samepage}
  \begin{ill}#1\end{ill}
  \begin{Verbatim}
 }
 {\end{Verbatim}\end{samepage}}

\newtheorem{ill}{Illustration}[chapter]

\begin{document}

\chapter{title}

\begin{codeillustration}{Some great code}
code = 'great'
print(code)
\end{codeillustration}

\end{document}

enter image description here

4
  • 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 .