# How to suppress extra vertical space before/after fancy Verbatim environments?

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}


Later: egreg's solution sometimes works:

but sometimes doesn't:

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.

-
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

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.

-

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}


-