Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Are you using the verbatim package? Or listings? –  Seamus Jul 31 '12 at 9:25
    
regarding question 2a, if you are intending to submit your document to a publisher, you should check with the publisher regarding allowable margins; it often requires extra attention to accommodate wider-than-normal material on a press, and if it's too wide, it may actually be cut off or obscured in the binding process. better to check the guidelines rather than be asked to change something later -- or have it changed without your knowledge or consent. –  barbara beeton Jul 31 '12 at 12:38
    
I want to self-publish, thanks for the tip, I definitely should check the minimal margins required by the self-publishing editor. –  user17106 Jul 31 '12 at 15:28

1 Answer 1

up vote 9 down vote accepted

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}

Result

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

share|improve this answer
    
Thank you very much, thats an (almost)n perfect solution for my needs! I say almost, because I noticed that, if a wideverbatim environment expands over 2 pages, its content is cut off at the end of the first page and not continued on the second page. But these are rare cases and can, just like the cases when the verbatim text is to wide for the page, be fixed by hand. Thanks again - very helpfull. –  user17106 Jul 31 '12 at 15:26
    
An hour of attempting to rip my hair out and FINALLY i find this post. Thank you! –  Rick Dec 10 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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