5

Is it possible to change the linespacing of a document except for inside verbatim blocks?

\usepackage{setspace}
\onehalfspacing

The above results in more readable paragraphs, but the code snippets are too large for my taste.

5

Just patch \verbatim@font or (even safer) \verbatim macro to add your code before verbatim blocks.

\documentclass{article}
\usepackage{setspace}
\setstretch{1.5}
\usepackage{etoolbox}
\appto\verbatim{\singlespacing}
\begin{document}

text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
\begin{verbatim}
#include <stdio.h>
int main()
{
    puts("Hello world.");
    return 0;
}
\end{verbatim}

\end{document}

Alternatively, if you use fancyvrb package for verbatim, you could use \fvset to set formatcom option for all Verbatim (not verbatim) environments:

\documentclass{article}
\usepackage{setspace}
\setstretch{1.5}
\usepackage{fancyvrb}
\fvset{formatcom=\singlespacing}
\begin{document}

text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
\begin{Verbatim}
#include <stdio.h>
int main()
{
    puts("Hello world.");
    return 0;
}
\end{Verbatim}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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