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 am trying to define an environment which requires a frame line as wide as \textwidth

\DefineVerbatimEnvironment%
 {code}{Verbatim}{...,frame=lines,%
               xleftmargin=7pt,fontsize=...}

I am not sure how I could constrain the width of the line?

share|improve this question
1  
Although I can guess you're using fancyvrb, it is better to provide a Minimal Working Example here. –  Leo Liu Aug 20 '12 at 7:03
add comment

1 Answer

One can modify this solution to a related question as follows:

\documentclass{article}

\usepackage{fancyvrb}

\newenvironment{code}
  {
    \VerbatimEnvironment
    \vskip\baselineskip\hrule
    \begin{Verbatim}[xleftmargin=7pt]%
  }
  {\end{Verbatim}\hrule\vskip\baselineskip}

\usepackage{lipsum}
\begin{document}

\lipsum[2]

\begin{code}
  Test the verbatim.  
  Here is some more.
\end{code}

\lipsum[4]

\end{document}

Sample output

\hrule on its own will produce a line of length \textwidth, however, it only adds a minimal amount of vertical space. This is the reason for putting in the \vskip's. You may well want to adjust the size of these spaces (or use \smallbreak, etc.) and you may also want to add some negative \vskip amount just after the first rule and just before the last. E.g. the following gives spacing closer to that obtained from \begin{Verbatim}[frame=lines]:

\newenvironment{code}
  {
    \VerbatimEnvironment
    \vskip18pt\hrule\vskip-10pt\hskip0pt
    \begin{Verbatim}[xleftmargin=7pt]%
  }
  {\end{Verbatim}\vskip-3pt\hrule\vskip18pt}

If you need the rules to be of different thickness you can write something like \hrule height 2pt instead.

share|improve this answer
add comment

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.