# How to draw box around text that contains a verbatim block?

Typically, to box in some text, I use the following:

\fbox {
\parbox{\linewidth}{
This is some text! Blah blah blah...
}
}


However, say I have a verbatim block within that. It will never compile. Anyone know how I could draw a box around text which contains a verbatim block? I should note that I want to be able to draw a box around not just a verbatim block. For example, I would want to draw a box around something like:

This is text.
\begin{verbatim}
This is some more text.
\end{verbatim}
And this is even more text.

-

If you just want to frame a verbatim block, you may consider using the fancyvrb package.

\documentclass[10pt]{article}
\usepackage{fancyvrb}
\begin{document}
\begin{Verbatim}[frame=single]
abc
def
\end{Verbatim}
\end{document}

-

The framed package defines a framed environment that can contain verbatim text. This is a nice option if you want text other than the verbatim environment to be in the box.

-
There is also the (IMHO better) mdframed package. –  Martin Scharrer Nov 5 '11 at 19:48

Willie's answer using the fancyvrb is a common way out of this. If you are inserting code you could also list at the listings package. You can also use the verbdef package that allows you to define commands that contain verbatim text.

\documentclass[11pt]{article}
\usepackage{verbdef}
\begin{document}
\verbdef\demo{demonstration text}
\framebox{
\begin{minipage}{4cm}
This some normal text and
this is verbatim
\demo\\
\demo\\
\end{minipage}
}
\end{document}

-

Yet another solution :) is to use the cprotect package (on CTAN)

\documentclass[11pt]{article}
\usepackage{cprotect}
\begin{document}

\cprotect\fbox{
\begin{minipage}{4cm}
This some normal text and
this is verbatim
\verb|demonstration text|\\
\verb|demonstration text|
\end{minipage}
}

\end{document}


Note that the minipage is not part of the answer: it allows several lines in the \fbox.

-

The verbbox environment of the verbatimbox package puts verbatim into a box that can then be recalled elsewhere, in places where verbatim environment is not allowed (such as tabular, footnotes, fboxes, etc. [A new version of verbatimbox should be coming out in the coming days]

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\begin{verbbox}
This is some more text.
\end{verbbox}
\fbox{
This is text.
\theverbbox
And this is even more text.
}
\end{document}


-