9

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.

David Carlisle
  • 757,742

3 Answers3

10

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.

enter image description here

David Carlisle
  • 757,742
A.Ellett
  • 50,533
  • 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 00:58
  • 1
    You can also say \fvset{baselinestretch=1} in the preamble, thus avoiding the need to type it every time. – egreg Dec 12 '12 at 09:04
6

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}

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
5

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

enter image description here

\documentclass{uwthesis}% http://ctan.org/pkg/uwthesis
\makeatletter
\def\verbatim@font{\linespread{1}\normalfont\ttfamily}
\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 TeX FAQ.

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

\makeatletter
\newcommand{\nextverbatimspread}[1]{%
  \def\verbatim@font{%
    \linespread{#1}\normalfont\ttfamily% Updated definition
    \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:

enter image description here

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

Double \ Spaced \ Lines

\nextverbatimspread{1} \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).

Werner
  • 603,163
  • 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 00: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
  • Is your first solution compatible with fontspec? For example, to reduce the size of the verbatim font, I found this to work: \setmonofont[Scale=0.8]{Droid Sans Mono}, but it's ignored in the verbatim environment when I use your \def\verbatim@font{...} bit above. I'm not sure how to combine the two if it's possible... – Hendy Jun 08 '16 at 21:23