25

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.
22

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}
7

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?

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

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.

4

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

4

tcolorbox can also draw boxes around verbatim text

\documentclass{article}
\usepackage{tcolorbox}

\begin{document}

This is text.

\begin{tcolorbox}
This is text.
\begin{verbatim}
This is some more text.
     This is a second line #/&
\end{verbatim}
And this is even more text.
\end{tcolorbox}

And this is even more text.

\end{document}

enter image description here

4

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}
1

I've got another solution which might be not useful in most cases, but in my case it is:

\begin{tikzpicture}%
\draw [very thick] (0,-0.5) rectangle (2,0.5);
\node at (1,0) {\verb+\text+};
\end{tikzpicture}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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