Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

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 UK 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).

share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
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
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 9:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.