lstlisting, tt fonts, and alignment.

I (mostly) love the listings package, which I have started migrated to from verbatim for code listings. I prefer to continue to use teletype for code, and so I changed my default listing configuration to:

\lstset{
keywordstyle=\color{blue}
, basicstyle=\ttfamily
, columns=flexible
, numbers=left
, showstringspaces=false
}


However, there is one small annoyance/tradeoff that I cannot seem to put how I would like. With columns=flexible, alignment between lines is not longer very good, so things that you expect and want to align vertically no longer do, whereas in verbatim anything that's aligned in the latex source is aligned in the output.

On the other hand, changing columns to a non-flexible option fixes the alignment issue, but at the expense of strange (in my opinion, and also ugly) spacing between characters (more space than verbatim). Is there any way to get both verbatim-like inter-character spacing and maintain vertical alignment?

Here is a complete example that exhibits the problem when I compile (texlive-core 2010.22154-1 on Arch). I want the "::" for f and f' to be aligned.

\documentclass{article}
\usepackage{color}
\usepackage{listings}

\lstset{
keywordstyle=\color{blue}
, basicstyle=\ttfamily
, columns=flexible
, numbers=left
, showstringspaces=false
}

\begin{document}

f  :: Int -> Int -> Int
f' :: Int -> Int
\end{lstlisting}

\begin{verbatim}
f  :: Int -> Int -> Int
f' :: Int -> Int
\end{verbatim}

\end{document}


Any help would be much appreciated.

-
Would you make an example, please? –  egreg Jun 11 '11 at 19:58

keepspaces=true,

in your \lstset command. You might consider also fullflexible instead of flexible for columns.