4

I would like to create a new environment that would automate the following code:

\setbox0=\hbox{\begin{minipage}{5.8in}
\color{black}
\begin{verbatim}

<Some verbatim text here>

\end{verbatim}
\end{minipage}}
\fcolorbox{blue!30}{gray!7}{\box0}

I'm pretty new to Latex, so I apologize if this question is not clear or if there is some obvious mistake. I also want to say that this site is fantastic and extremely helpful for new Latex users like myself.

1

1 Answer 1

5

Adapting from code in this answer

\documentclass[12pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{fancyvrb,xcolor}

\usepackage{lipsum} % just for the example

\newsavebox{\FVerbBox}
\newenvironment{FVerbatim}
 {\VerbatimEnvironment
  \begin{center}
  \begin{lrbox}{\FVerbBox}
  \begin{minipage}{5.8in}
  \begin{Verbatim}}
 {\end{Verbatim}
  \end{minipage}
  \end{lrbox}
  \fcolorbox{blue!30}{gray!7}{\usebox{\FVerbBox}}
  \end{center}}

\begin{document}
\lipsum*[2]
\begin{FVerbatim}[fontsize=\small]
Text.
With { \ strange /##& characters
\end{FVerbatim}
\lipsum[3]
\end{document}

enter image description here

Here's a version with additional vertical spaces. The interaction with Verbatim makes it somewhat difficult to guess at the right proportions, just experiment.

\documentclass[12pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{fancyvrb,xcolor}

\usepackage{lipsum} % just for the example

\newsavebox{\FVerbBox}
\newenvironment{FVerbatim}
 {\VerbatimEnvironment
  \begin{center}
  \vspace{1ex} % additional space above the frame
  \begin{lrbox}{\FVerbBox}
  \begin{minipage}{5.8in}
  \vspace*{4ex} % additional space before the verbatim material
  \begin{Verbatim}}
 {\end{Verbatim}
  \vspace{1ex} % additional space after the verbatim material
  \end{minipage}
  \end{lrbox}
  \fcolorbox{blue!30}{gray!7}{\usebox{\FVerbBox}}
  \vspace{1ex} % additional space after the frame
  \end{center}}

\begin{document}
\lipsum*[2]
\begin{FVerbatim}[fontsize=\small]
Text.
With { \ strange /##& characters
\end{FVerbatim}
\lipsum[3]
\end{document}

enter image description here

For fancier boxes, you can have a look at tcolorbox

2
  • Thank you so much! This is extremely helpful. One more thing though: is there some way I could control the amount of space between the top of the frame of the text box and the start of the text? It would be ideal if I could also control the spacing between the bottom of the frame and the text as well. I'm starting to really like Latex :)
    – user56010
    Jul 23, 2014 at 16:58
  • Thanks! This is good, but I should have described things better in my question: I was wondering about the amount of space between the frame of the box and the text inside the box instead of the amount of space between the box and the text outside the box. I really appreciate the help.
    – user56010
    Jul 23, 2014 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .