# How can I make lstlisting look exactly like verbatim?

I like the way the verbatim environment looks, and I want to make my lstlistings look like it.

I've tried setting the basic style to \ttfamily, but the letter kerning and line spacing seem to be different. Is there any way in getting it look like verbatim?

Here is a complete example:

\documentclass[a4paper]{report}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}

This is lstlisting:

\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
\end{verbatim}

\end{document}


which looks like:

Edit

I tried setting the option columns=flexible, but then listings will ignore important spaces in the source code.

-
columns=fullflexible,keepspaces=true – egreg Apr 21 '14 at 20:27
– Jubobs Apr 21 '14 at 20:28
@egreg That works perfectly; would you mind making it an answer so I can accept it? – csl Apr 21 '14 at 20:33

Here is a way; I vastly prefer this to setting basewidth, because it doesn't box each character and doesn't need to guess whether the monospaced font uses half em character widths.

\documentclass[a4paper]{report}
\usepackage{listings}

\begin{document}

\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces=true,
}
\verb|basicstyle=\ttfamily, columns=fullflexible, keepspaces=true|
\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
\end{verbatim}

\end{document}


-
So there are no visible differences, only that the engine rendering is more flexible with the second approach? – csl Apr 21 '14 at 20:48
@csl The difference between flexible, spaceflexible and fullflexible is something I've never really understood. I can't stand the fixed style that is horrible with monospaced characters and preposterously awful with variable width fonts. – egreg Apr 21 '14 at 20:51

With the default setting fixed for columns, listings reserves 0.6em for characters; you need to adjust this to 0.5em using basewidth:

\documentclass[a4paper]{report}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fixed,
basewidth=0.5em
}

\begin{document}

This is lstlisting:

\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
\end{verbatim}

\end{document}


-
What is the difference between this and what @egreg commented to my question? (columns=fullflexible,keepspaces=true). This also works, but I would like to know why. – csl Apr 21 '14 at 20:34
@csl it all depends on how you want spacing to be handled; the explanation is too long for a comment, but you can read Section 2.10 Fixed and flexible columns of the listings documentation to see the difference between fullflexible, flexible and fixed. – Gonzalo Medina Apr 21 '14 at 20:37
I did, but do the two ways produce the exact same look? I couldn't notice any differences. – csl Apr 21 '14 at 20:39
fullflexible and fixed used with no additional keys for the spacing will produce different results; however, used as in my answer or in egreg's suggestion they seem to produce the same effect. In any case, since egreg's answer now makes mine completely useless, I'll delete this. – Gonzalo Medina Apr 21 '14 at 20:45