2

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?

1

1 Answer 1

4

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.

You must log in to answer this question.

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