remove spacing before and after Verbatim

2,199 views
Skip to first unread message

yliow

unread,
Jul 12, 2011, 12:35:06 PM7/12/11
to LaTeX Users Group
Hi everyone,

Is there a way to control the spacing before and after the Verbatim?
I've looked high and low and can't find a good solution.

In the following, the verbatim frame has some vertical spacing before
and after the environment:

\documentclass[a4paper,12pt]{scrartcl}
\usepackage{lipsum}
\usepackage{fancyvrb}
\usepackage{parskip}
\setlength{\parskip}{12pt}
\setlength{\parindent}{0pt}

\begin{document}
\lipsum[1]
\begin{Verbatim}[frame=single]
hello world
\end{Verbatim}
\lipsum[2]

\end{document}

What I want is achieve this (with the vspace hack):

\documentclass[a4paper,12pt]{scrartcl}
\usepackage{lipsum}
\usepackage{fancyvrb}
\usepackage{parskip}
\setlength{\parskip}{12pt}
\setlength{\parindent}{0pt}

\begin{document}
\lipsum[1]
\vspace{-12pt}
\begin{Verbatim}[frame=single]
hello world
\end{Verbatim}
\vspace{-16pt}
\lipsum[2]

Peter Flynn

unread,
Jul 12, 2011, 5:53:37 PM7/12/11
to latexus...@googlegroups.com
On Tue, Jul 12, 2011 at 6:35 PM, yliow <yl...@ccis.edu> wrote:
Hi everyone,

Is there a way to control the spacing before and after the Verbatim?

Only manually. The package does not provide any special control for this unless you define a custom verbatim format (see the documentation).
 
I've looked high and low and can't find a good solution.

In the following, the verbatim frame has some vertical spacing before
and after the environment:

\documentclass[a4paper,12pt]{scrartcl}
\usepackage{lipsum}
\usepackage{fancyvrb}
\usepackage{parskip}
%\setlength{\parskip}{12pt}
%\setlength{\parindent}{0pt}

If you are using the parskip package, you do not need to set \parskip and \parindent like that: it happens automatically. I commented them out
 
[snip]

\begin{document}
\lipsum[1]
\vspace{-6pt}

\begin{Verbatim}[frame=single]
hello world
\end{Verbatim}
\vspace{-9pt}
\lipsum[2] 
\end{document}
 
I don't know an easier way to do this. Why did you want to remove the space?

///Peter

yliow

unread,
Jul 12, 2011, 8:05:01 PM7/12/11
to LaTeX Users Group
The extra spacing before and after verbatim makes my framed (single
line) verbatim boxes look like whole new paragraphs. In some of my
documents, I have *many* such small framed verbatim boxes so that the
visual sense of paragraphs is lost/confusing because the verbatim
spacing is about the same as my parskip.

I do have a solution that works well 99% of the time. The way I solved
this is to (1) put the verbatim inside a minipage and *then* (2)
manipulate the vspace. Since you cannot put verbatim inside a
newenvironment, I have to use atbeginend to do it for me.

It's not a pretty solution. But at least the hacks are localized while
I wait for someone to come up with a better solution. I've been
searching for a solution once every few months for a long time. So
I'll post it below since I know some people are looking for such a
solution.

% console = framed verbatim
\DefineVerbatimEnvironment%
{console}{Verbatim}
[frame=single]

% hack the vspace in console
\BeforeBegin{console}{\mbox{}\\ \begin{minipage}{\textwidth}
\vspace{3pt}}
\AfterEnd{console}{\vspace{4pt} \end{minipage} \\ }

After this you can use the "console" environment. Obviously you might
need to modify the vspace. I've already tested it at the beginning/
middle/end of paragraph for my documents (12pt, parskip 12pt).

-yihsiang
Reply all
Reply to author
Forward
0 new messages