I have included verbatim to show a small section of code, how can I add a comment/caption for this verbatim?

I am writing some documentation on a tool I am using at work and I have included some code that has a comment to explain what a particular line is there for, this comment runs off the page if I leave it in the verbatim (which I'm sure is bad practice in it's own right). My question is how can I include this close to my verbatim and make it obvious that it pertains to the verbatim if not the specific line?

\begin{verbatim}
*** Settings ***
Documentation     A test suite with a single test for valid login.

\end{verbatim}
\caption{The textit{Documentation} is the description that will be shown in the reports generated by RF and at run time when this test suite is running.}

• Normally a caption would have a header like "Figure 1" or "Table 2" which could be added to a list of figures/tables. Is there any similar requirement here? Would the caption say "Verbatim 1: blah-blah" or merely "Blah-blah"? Also, is the verbatim text to be full width, or a subset of \textwidth? – Steven B. Segletes Jul 29 '14 at 15:57
• I wouldn't bother with a heading, I only use the term caption so that people understand roughly what I want this to look like. The verbatim is inline with the main body of text, but runs to the edge (and over) the page. Can I make that wrap or should I post a second question for that? – Stormie Jul 29 '14 at 16:00

This may or may not be what you are looking for. Here I put the verbatim in a box structure (which means it can't break across a page boundary). I then inlay (with \stackinset) the "caption" over the box in the general area of relevance.

\documentclass{article}
\usepackage{verbatimbox,stackengine,xcolor}
\begin{document}
\begin{verbbox}
*** Settings ***
Documentation     A test suite with a single test for valid login.

\end{verbbox}
\stackinset{c}{}{t}{-.1cm}{\textcolor{red}{\sffamily\tiny\parbox{2.2in}{
The \textit{Documentation} is the description that will be shown in the
reports generated by RF and at run time when this test suite is running.}}}{\theverbbox}
\end{document}


And here is using \scriptsize inset, rather than \tiny:

\documentclass{article}
\usepackage{verbatimbox,stackengine,xcolor}
\begin{document}
\begin{verbbox}
*** Settings ***
Documentation     A test suite with a single test for valid login.

\end{verbbox}
\stackinset{r}{}{t}{-.2cm}{\textcolor{red}{\sffamily\scriptsize\parbox{3.5in}{
The \textit{Documentation} is the description that will be shown in the
reports generated by RF and at run time when this test suite is running.}}}{\theverbbox}
\end{document}


And, finally, here I present it to look something like a normal "caption".

\documentclass{article}
\usepackage{verbatimbox,stackengine,lipsum}
\begin{document}
\lipsum[4]

\bigskip\begin{verbbox}
*** Settings ***
Documentation     A test suite with a single test for valid login.

\end{verbbox}
{\centering\stackon[12pt]{\footnotesize\parbox{\textwidth}{
The \textit{Documentation} is the description that will be shown in the
reports generated by RF and at run time when this test suite is running.}}{\theverbbox}%
\par}\bigskip

\lipsum[4]
\end{document}


• I'm trying to download the package and I keep getting the error verbatimbox.sty not found then it emergency stops? Apart from that, your solution looks fantastic. – Stormie Jul 29 '14 at 16:15
• @Stormie You can go to ctan.org/tex-archive/macros/latex/contrib/verbatimbox, and download the .sty and .pdf files. Placing the sty in your working directory will allow compilation, until you figure out how to make a more permanent installation of the package. – Steven B. Segletes Jul 29 '14 at 16:17
• Ah! it's my company firewall blocking it, I can't get any of the packages. I'll just do this at home. Thank you very much for the answer though, fantastic and comprehensive. +1 from me – Stormie Jul 29 '14 at 16:19
• @Stormie You may also want to look at my numberedblock package at ctan.org/pkg/numberedblock, which allows you to number-label code snippets. Best wishes! – Steven B. Segletes Jul 29 '14 at 16:24