# [Tex/LaTex] I need a customized verbatim environment

environmentsverbatimwidth

I would need a verbatim environment with the following behaviour:

1. It is centered (left and right distance to the page edge should be equal).
2. It can be much wider than the regular text, almost using the whole page width.

I know how to define my own environment, but not how to achieve these goals. And I have an additional (sub)question:

2a. What is the minimal margin I have to leave at the page edges?

Using fancybox package you have tools to define customized verbatim environments, such as the one you are requesting. In this case, the trick is to save the whole verbatim into a box whose width is the width of its longest line (BVerbatim environment does this), and then put that box as the argument of a \centerline command, which basically centers the content between the margins, even if the contents are larger than \textwidth (allowing these contents protruding both margins the same amount).

The following MWE defines a new enviroment wideverb which does all this:

\documentclass{article}
\usepackage[margin=4cm]{geometry}
\usepackage{fancybox}

\newenvironment{wideverbatim}%
{\vskip\baselineskip\VerbatimEnvironment
\begin{Sbox}\begin{BVerbatim}}
{\end{BVerbatim}%
\end{Sbox}\noindent\centerline{\TheSbox}\vskip\baselineskip}

\begin{document}
\noindent Normal text\hrulefill X\par
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam placerat, neque
a gravida semper, ipsum neque tincidunt ipsum, a tincidunt mauris erat id enim.
\begin{wideverbatim}
A verbatim environment which contains
only
short lines
\end{wideverbatim}

\noindent Normal text again\hrulefill X\par
\begin{wideverbatim}
Another verbatim with longer lines
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam placerat, neque
a gravida semper, ipsum neque tincidunt ipsum, a tincidunt mauris erat id enim.
\end{wideverbatim}

\noindent Normal text again\hrulefill X\par
\begin{wideverbatim}
Another verbatim with EVEN LONGER lines
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam placerat, neque a gravida semper, ipsum neque
tincidunt ipsum, a tincidunt mauris erat id enim.
\end{wideverbatim}
\end{document}


Note that the contents of wideverbatim are not restricted in length. You can have a line which is longer than the page width, and in this case part of its contents would not be visible (but the line will be centered anyway).