# remove double spacing inside {verbatim}

I'm using the uwthesis class which generally typesets with a lot of space between lines. This is generally fine, however, I want to override the spacing in certain examples which appear in the verbatim environment.

An MWE looks something like this:

\documentclass{uwthesis}
\begin{document}

Double \\
Spaced \\
Lines

\begin{verbatim}
These
lines
are
also
doublespaced
\end{verbatim}
\end{document}


Ideally, I'd like to define a custom verbatim for use in examples which presents text without the extra space between the lines, but I'm not sure what I need to write.

-

You can update the verbatim font to use a different \linespread. More specifically, include \linespread{1} as part of \verbatim@font:

\documentclass{uwthesis}% http://ctan.org/pkg/uwthesis
\makeatletter
\makeatother
\begin{document}

Double \\
Spaced \\
Lines

\begin{verbatim}
These
lines
are
not
doublespaced
\end{verbatim}

Double \\
Spaced \\
Lines
\end{document}


The uwthesis class relies on the default LaTeX kernel verbatim environment, hence the ease of not having to add extra packages to obtain a desired output. For a reference to \linespread, see Why doesn’t \linespread work? on the UK TeX FAQ.

You could contain the redefinition in a macro that would act as a switch:

\makeatletter
\def\verbatim@font{%
\gdef\verbatim@font{\normalfont\ttfamily}}% Revert to old definition
}
\makeatother


which you could use in the form

\nextverbatimspread{1}
\begin{verbatim}
% ...single-spaced verbatim
\end{verbatim}
% ...some other content
\begin{verbatim}
% ...double-spaced verbatim
\end{verbatim}


For creating your own verbatim-like environment is a little more tricky. The easiest would be to include the verbatim package and use David's suggestion of wrapping the verbatim environment in command-form:

\documentclass{uwthesis}% http://ctan.org/pkg/uwthesis
\usepackage{verbatim}% http://ctan.org/pkg/verbatim
\makeatletter
\def\verbatim@font{%
\gdef\verbatim@font{\normalfont\ttfamily}}% Revert to old definition
}
\newenvironment{myverbatim}[1][1.5]
{\def\verbatim@font{%
\gdef\verbatim@font{\normalfont\ttfamily}}% Revert to old definition
\verbatim}
{\endverbatim}
\makeatother
\begin{document}

Double \\
Spaced \\
Lines

\begin{verbatim}
These
lines
are
not
doublespaced
\end{verbatim}

Double \\
Spaced \\
Lines

\begin{myverbatim}
These
lines
are
doublespaced
\end{myverbatim}

Double \\
Spaced \\
Lines

\begin{myverbatim}[1]
These
lines
are
not
doublespaced
\end{myverbatim}
\end{document}


The myverbatim environment takes an option argument to specify a modified \linespread. The default is 1.5, as specified by uwthesis ("not quite doublespaced", according to uwthesis.cls, line 158).

-
I like this answer as it avoids loading more packages. I'd like to use this technique to define a customized {verbatim}, say {verbatim1}, that has this property. I have some other text in my document that I think I'd like to keep as normal verbatim. To do that, I'd need some sort of \newcommand, but I'm not sure exactly what to write. –  apexofservice Dec 12 '12 at 0:51
@apexofservice: verbatim is notoriously picky. I've added an update to my answer that achieves what you're after, but requires the verbatim package. –  Werner Dec 12 '12 at 19:31

Look at using fancyvrb. It has a lot of nice tricks. But in particular, you can set the baselineskip within the verbatim environment:

\documentclass{uwthesis}
\usepackage{fancyvrb}
\begin{document}

Double \\
Spaced \\
Lines

\begin{Verbatim}[baselinestretch=0.75]
These
lines
are
also
doublespaced
\end{Verbatim}
\end{document}


0.75 might be a bit tight for you.

-
I'll try this solution as it seems to avoid redefining the old {verbatim} (so that I can have some examples which still have doublespacing). –  apexofservice Dec 12 '12 at 0:58
You can also say \fvset{baselinestretch=1} in the preamble, thus avoiding the need to type it every time. –  egreg Dec 12 '12 at 9:04

You can use the etoolbox package to change \baselinestretch from 1.5 (the default in uwthesis.cls) to 1:

\documentclass{uwthesis}
\usepackage{etoolbox}

\BeforeBeginEnvironment{verbatim}{\def\baselinestretch{1}}

\begin{document}

Double \\
Spaced \\
Lines

\begin{verbatim}
These
lines
are
not
doublespaced
\end{verbatim}

Double \\
Spaced \\
Lines

\end{document}


-