8

Here's a MWE:

\documentclass[12pt]{article}
\usepackage{fancyvrb}

%===

\begin{document}


\begin{Verbatim}[fontsize=\small, frame=single, xrightmargin=5cm]
Text.
\end{Verbatim}


\end{document}

I'm trying to center the box, but I couldn't find any way of doing it. Any comments? Thanks in advance!

1
  • It may be overkill, but tcolorbox can do this easily.
    – jon
    Commented May 31, 2014 at 19:42

2 Answers 2

9

You could use a minipage:

\documentclass[a4paper]{article}

\usepackage{fancyvrb}

\begin{document}

\begin{center}
\begin{minipage}{8cm}
\begin{Verbatim}[fontsize=\small, frame=single]
Text.
\end{Verbatim}
\end{minipage}
\end{center}

\end{document}
1
  • This needs one to hardcode the width of the verbatim content in the line \begin{minipage}{8cm}.
    – HappyFace
    Commented Sep 16, 2021 at 13:03
2

Use BVerbatim; for boxing, use \fbox:

\documentclass[12pt]{article}
\usepackage{fancyvrb}

\usepackage{lipsum} % just for the example

\newsavebox{\FVerbBox}
\newenvironment{FVerbatim}
 {\VerbatimEnvironment
  \begin{center}
  \begin{lrbox}{\FVerbBox}
  \begin{BVerbatim}}
 {\end{BVerbatim}
  \end{lrbox}
  \fbox{\usebox{\FVerbBox}}
  \end{center}}

\begin{document}
\lipsum*[2]
\begin{FVerbatim}[fontsize=\small]
Text.
\end{FVerbatim}
\lipsum[3]
\end{document}

enter image description here

2
  • How to remove the box border?
    – HappyFace
    Commented Sep 16, 2021 at 13:04
  • @HappyFace Remove \fbox and a pair of braces.
    – egreg
    Commented Sep 16, 2021 at 15:38

You must log in to answer this question.

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