Example environment with numbering, caption, "verbatim" content
2,143
I've solved in this way.
\usepackage{listings}
\lstnewenvironment{example}[1][]{
\renewcommand*{\lstlistingname}{Code}
\lstset{fancyvrb=true,basicstyle=\footnotesize,captionpos=b,xleftmargin=2em,#1}
}{}
And then using this sample:
\begin{example}[caption=My Caption,label=mylabel]
My example text
\end{example}
Related videos on Youtube
Author by
auino
Updated on August 01, 2022Comments
-
auino over 1 year
I need to use an example environment like the following.
\begin{example}[My Caption, mylabel] My example text \end{example}
It should show the text inside a verbatim environment, the caption (like a figure)
Example 1: My Caption
and the labelmylabel
to refer to the example.Can anyone help me, please?
I tried with this, but it doesn't work with the verbatim environment.
\newcounter{example}[chapter] \newenvironment{example}[1]{ \refstepcounter{example} \vspace{0.3cm} {\bf{Example \thechapter.\arabic{example}} - #1} \\ \sf }{\vspace{0.3cm}} \renewcommand{\theexample}{\thechapter.\arabic{example}}
Thanks