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.

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.
share|improve this question
add comment

5 Answers 5

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}
share|improve this answer
add comment

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.

See also answers to How to use the quote environment inside a \framebox?

share|improve this answer
    
There is also the (IMHO better) mdframed package. –  Martin Scharrer Nov 5 '11 at 19:48
add comment

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}
share|improve this answer
add comment

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.

share|improve this answer
add comment

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}

enter image description here

share|improve this answer
add comment

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.