[Tex/LaTex] Is it possible to use a non-monospace font in verbatim environments

fontslistingsverbatim

I know it runs contrary to a lot of the point, but I'd like to experiment with different fonts to see which I like better. I can't seem to find any reference to a way to do this, if it's at all possible.


Edit:

I ended up using the following. It's better, but not quite to the point of what I'd like

\RequirePackage[T1]{fontenc}
\RequirePackage[scaled]{beramono}

The big problem I have is that I find the monospace fonts to be too spaced out, making text take up a lot more space on the line. If anybody has any other suggestions that would be great.

Best Answer

In the case of the lstlisting environment from the listings package, you can use the basicstyle key to control the font attributes for normal text in the code; some examples:

\documentclass{article}
\usepackage{listings}

\begin{document}

\begin{lstlisting}[basicstyle=\sffamily]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\itshape]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\small\ttfamily]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\Large\normalfont]
Some text here
\end{lstlisting}

\end{document}

enter image description here

Of course, these modifications are better made globally using \lstset in the preamble:

\lstset{
  basicstyle=\small\sffamily}

You can also use the columns key to control spacing; an example showing the four available values:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage{listings}

\lstset{columns=fullflexible}

\begin{document}

\begin{lstlisting}[basicstyle=\sffamily,columns=fixed]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\sffamily,columns=flexible]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\sffamily,columns=fullflexible]
Some text here
\end{lstlisting}

\begin{lstlisting}[basicstyle=\sffamily,columns=spaceflexible]
Some text here
\end{lstlisting}

\end{document}

enter image description here

Again, a global setting is preferable; for example:

\lstset{
  basicstyle=\small\sffamily,
  columns=fullflexible
}