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 using fancyvrb's DefineVerbatimEnvironment to create differently-styled verbatim environments for user input and output. When two or more of these follow each other, there is extra vertical space between them. According to control vertical space before and after verbatim environment?, that extra space can be suppressed by re-setting \topsep or \partopsetp for a regular verbatim. How can/should I suppress it for an environment defined using DefineVerbatimEnvironment?

Example showing the extra inter-block spacing I want to suppress:

\documentclass{book}

\usepackage{fancyvrb}
\usepackage{relsize}
\DefineVerbatimEnvironment{VerbIn}{Verbatim}{fontsize=\relsize{-1}}
\DefineVerbatimEnvironment{VerbOut}{Verbatim}{fontshape=it,fontsize=\relsize{-1}}

\begin{document}

\begin{VerbIn}
This is user input.
\end{VerbIn}
\begin{VerbOut}
This is user output.
\end{VerbOut}

\end{document}

screenshot showing extra inter-block spacing

Later: egreg's solution sometimes works:

VerbInX solution working

but sometimes doesn't:

VerbInX solution not working

Steven's verbatimbox solution works, but will be clunky in practice: there are often sequences of immediately consecutive in-out-in-out-in-out, all of which should have spaces suppressed, and having to give each one a unique token for later recall will be error-prone to edit.

Later: if you are comfortable using Git, I have committed egreg's solution in a branch:

$ git clone git@github.com:gvwilson/bc.git
$ cd bc
$ git checkout book
$ cd book
$ make # or 'pdflatex book' a couple of times
$ open book.pdf # and go to the bottom of pg 8 or the top of pg 10

Packages are loaded in packages.tex, and commands are defined in commands.tex.

share|improve this question
    
I imagine you want to suppress the spacing only when VerbOut immediately follows VerbIn. Does a VerbOut always follow a VerbIn? –  egreg Jun 9 at 22:44
    
No: sometimes there's plain text between the input and output. And sometimes there's a sequence of in-out-in-out, all of which should have the spaces suppressed. –  Greg Wilson Jun 10 at 21:23
    
Can you add the code for the non working case and clearly specify the use cases? –  egreg Jun 10 at 21:23
    
Instructions added to main body (I can't seem to put <pre> blocks in comments). –  Greg Wilson Jun 11 at 12:35
    
For consecutive in-out constructions, one may use the verbbox environment, rather than myverbbox. In the former case, no unique token name is specified. Rather, the "generic" verbbox is recalled with \theverbbox. I have updated my answer. –  Steven B. Segletes Jun 11 at 23:45

2 Answers 2

If you want to suppress the vertical space when a VerbOut environment immediately follows a VerbIn, this should work:

\documentclass{book}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{lipsum} % just for the example

\DefineVerbatimEnvironment{VerbInX}{Verbatim}{fontsize=\relsize{-1}}
\DefineVerbatimEnvironment{VerbOutX}{Verbatim}{fontshape=it,fontsize=\relsize{-1}}
\newenvironment{VerbIn}
  {\VerbatimEnvironment
   \par\VerbInX}
  {\endVerbInX\vskip1sp }
\newenvironment{VerbOut}
  {\VerbatimEnvironment
   \par\ifdim\lastskip=1sp\vspace{\dimexpr-2\topsep-2\partopsep\relax}\fi
   \VerbOutX}
  {\endVerbOutX}


\begin{document}

\lipsum[2]
\begin{VerbIn}
This is user input.
\end{VerbIn}
\begin{VerbOut}
This is user output.
\end{VerbOut}
\lipsum[3]
\begin{VerbIn}
This is user input.
\end{VerbIn}
\lipsum[4]

\end{document}

At the end of VerbIn I add a special invisible vertical skip of just 1sp (the least positive length in TeX). At the beginning of VerbOut I check if such vertical skip is present, which should only be the case when VerbIn has just ended.

enter image description here

share|improve this answer

You could use the verbatimbox package as an alternative. By placing the verbatim content in boxes, and then outputting the boxes, the extra line can be avoided.

I have EDITED my answer to show two ways to achieve the result with the package. In the first case the myverbbox environment requires a token name to be associated with the created box. In this way, the verbatim material can be created well in advance of its actual use (unless the token is reutilized).

In the second case, the verbbox environment does not use a unique token name in which to store the verbatim box; rather, the content is stored in the generic box that is recalled with \theverbbox. This technique is more efficient if the box can be used as soon as it is created.

\documentclass{book}
\usepackage{verbatimbox}
\usepackage{relsize}
\begin{document}
\begin{myverbbox}[\relsize{-1}]{\VerbIn}
This is user input.
\end{myverbbox}
\begin{myverbbox}[\itshape\relsize{-1}]{\VerbOut}
This is user output.
\end{myverbbox}
\noindent\VerbIn\\\VerbOut

\begin{verbbox}[\relsize{-1}]
This is user input.
\end{verbbox}
\noindent\theverbbox\\
\begin{verbbox}[\itshape\relsize{-1}]
This is user output.
\end{verbbox}
\theverbbox
\end{document}

enter image description here

share|improve this answer

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.