1

I want to control the vertical space after a Verbatim environment from fancyvrb. Even setting \topsep and \partopsep to 0pt, as suggested in muzimuzhi's answer, gives me some extra spacing after the boxed code.

\documentclass{article}\setlength{\textwidth}{200pt}
\usepackage{lipsum}
\usepackage{fancyvrb}
\fvset{listparameters=\setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}}

\begin{document}
\lipsum[1][1]

\begin{Verbatim}[frame=single]
  Some content
  here, with its
  own box
\end{Verbatim}

\lipsum[2][1]
\end{document}

enter image description here

In the worse case, I can simply add a negative space in my custom environment definition. But I'd rather understand what's going on here and avoid creating patches due to my ignorance of the current spacing situation.

P.S. It would be nice asking pdftex to print out the boxes and glue used here to figure out what's going on.

Debugging

  • Using \setlength{\parskip}{0pt} globally does nothing.
  • Adding \nointerlineskip right after the Verbatim environment fixes the issue, although I'm not sure why (see comments to the accepted answer for an explanation).

enter image description here

1
  • Well the sep below is actually caused by the frame setting. With frame=lines (frame=topline) you can even see extra separation at top. Jun 7, 2023 at 2:39

1 Answer 1

2

You can add \prevdepth\dp\strutbox after the environment

\documentclass{article}\setlength{\textwidth}{200pt}
\usepackage{lipsum}
\usepackage{fancyvrb}
\fvset{listparameters=\setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}}

\begin{document}
\lipsum[1][1]

\begin{Verbatim}[frame=single]
  Some content
  here, with its
  own box
\end{Verbatim}
\prevdepth\dp\strutbox

\indent\smash{\rule{1pt}{\ht\strutbox}}\lipsum[2][1]
\end{document}

(the \rule for demo only; \indent tacitly implied by \smash in recent LaTeX at least)

output

Or with modern LaTeX:

\documentclass{article}\setlength{\textwidth}{200pt}
\usepackage{lipsum}
\usepackage{fancyvrb}
\fvset{listparameters=\setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}}

\AddToHook{env/Verbatim/after}{\prevdepth\dp\strutbox}
\begin{document}
\lipsum[1][1]

\begin{Verbatim}[frame=single]
  Some content
  here, with its
  own box
\end{Verbatim}

\lipsum[2][1]
\end{document}

Adding some more smashed rules to illustrate exact separation achieved:
\documentclass{article}\setlength{\textwidth}{200pt}
\usepackage{lipsum}
\usepackage{fancyvrb}
\fvset{listparameters=\setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}}

\begin{document}
\lipsum[1][1]\strut\smash{\rule[-\dp\strutbox]{1pt}{\dp\strutbox}}

\begin{Verbatim}[frame=single]
  Some content
  here, with its
  own box
\end{Verbatim}
\prevdepth\dp\strutbox

\noindent\kern-1.5pt\smash{\rule{3pt}{\ht\strutbox}}\kern-1.5pt\kern\parindent\smash{\rule{1pt}{\ht\strutbox}}\lipsum[2][1]
\end{document}

We need a \strut in last line above frame. We have set all extra space to zero. This document has not modified \parskip because it defaults to 0pt plus 1pt. Achieving "exact smae space before and after" would require to say "space between what and what". It is clear where we start measuring space either at top or bottom frame, it is not clear where we stop measuring space? is it distance to nearest ink, so depends whether lines contains descenders (for top) or ascenders (for bottom), or are we measuring distance to baselines. If latter case we should probably not insist on same space above and below, because the situation with letters is not symmetric: they rise higher than they descend.

rules

18
  • the testing small vertical rule really abuts precisely to bottom of frame, although for example on my PDF viewer anti-aliasing or other effect at some point made me think there was overlap, but there is none, adding a horizontal displacement (not indicated here) allowed me to verity it (I could have gone the \showoutput way)
    – user691586
    Jun 5, 2023 at 17:02
  • Thanks! Can you explain in your answer what \prevdepth\dp\strutbox does? Also, there is still some space on top of the N (of the following paragraph) while there is no space under the g (of the preceding paragraph). Finally, do you know what is causing the additional vertical spacing in my original question?
    – Atcold
    Jun 5, 2023 at 18:05
  • Oh, Googling your command I found out that \nointerlineskip suppresses parskip. I think this command should be used instead.
    – Atcold
    Jun 5, 2023 at 18:10
  • \nointerlineskip expands to \prevdepth -\@m \p@ i.e. to setting the \prevdepth to -1000pt. This is completely unrelated to the \parskip value. Indeed my advice has no relation to effect of the \parskip in the document as you can see by setting it to some large value for example 50pt in the document (I am not talking here about the parameters you modify via \fvset). Then you will see the effect of the parskip glue insertion after the framed box. And it turns out fancyvrb code induces it also above the framed box. For \prevdepth see TeX by Topic.
    – user691586
    Jun 6, 2023 at 7:01
  • 1
    tart next paragraph with \smash{\rule{1pt}{\baselineskip}}A you will see that TeX locates the baseline of next paragraph exactly baselineskip away from bottom of bottom rule. By faking the \prevdepth to be the depth of the strutbox, we are cheating and telling TeX to consider the last "baseline" was higher up, so it will position the baseline of new paragraph higher up.
    – user691586
    Jun 6, 2023 at 20:49

You must log in to answer this question.

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