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 (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
  , commentstyle={}
  , 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
  , commentstyle={}
  , columns=flexible
  , numbers=left
  , showstringspaces=false
  } 

\begin{document}

\begin{lstlisting}[language=Haskell,numbers=none]
  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.

share|improve this question
1  
Would you make an example, please? –  egreg Jun 11 '11 at 19:58

1 Answer 1

up vote 8 down vote accepted

Say

keepspaces=true,

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

share|improve this answer

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.