Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

As with this question, I want to use a verbatim environment from within an environment definition, but I want the listings to be numbered, so I think I need to use fancyvrb. It appears that fancyvrb has no equivalent to \verbatim and \endverbatim. I can't see how to use it within an environment definition. Here's what I have (based on verbatim package):

\newenvironment{example}[2]{%
    \program%
    \caption{#2}%
    \label{#1}%
    \vskip.7\baselineskip
    \scriptsize
    \verbatim%
}{%
    \endverbatim\normalsize
    \vskip-.5\baselineskip
    \endprogram
}

I'd like to use a fancyvrb environment, defined like so:

\DefineVerbatimEnvironment%
{Example}{Verbatim}
{numbers=left,fontsize=\scriptsize, stepnumber=3,
frame=lines,framerule=0.8mm}

to do this:

\newenvironment{example}[2]{%
    \program%
    \caption{#2}%
    \label{#1}%
    \vskip.7\baselineskip
    \scriptsize
    \begin{Example}%
}{%
    \end{Example}\normalsize
    \vskip-.5\baselineskip
    \endprogram
}

I get the following error:

Runaway argument?
  ! File ended while scanning use of \FancyVerbGetLine.

I'm probably doing something silly, but would some patient person point out to me what it is?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

the same is possible with the listing environment

\documentclass{article}
\usepackage{fancyvrb,caption,floatrow}
\DeclareNewFloatType{example}{placement=t,%within=section,
   fileext=exa,name=Example}
\captionsetup[example]{font=sf,labelfont=bf,skip=\smallskipamount}

\newenvironment{Example}[2]
{\VerbatimEnvironment
 \captionof{example}{#2}\ifx\relax#1\relax\else\label{#1}\fi%
\begin{Verbatim}[numbers=left,fontsize=\scriptsize, stepnumber=3,
                 frame=lines,framerule=0.4mm]}
{\end{Verbatim}}

\begin{document}

\begin{Example}{TheLabel}{The caption}
foo
bar
\end{Example}

\begin{Example}{}{Another caption}
foo
bar
baz
\end{Example}

See Example~\ref{TheLabel}

\end{document}

enter image description here

share|improve this answer
    
Thanks! That works perfectly, and has the added advantage that the examples can split nicely across pages. –  Andrew Matthews May 18 '11 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.