[Tex/LaTex] New environment for minipage and colorbox

environmentsverbatim

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.

Best Answer

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