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 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 )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}

\end{document}

which looks like:

enter image description here

Edit

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

share|improve this question
3  
columns=fullflexible,keepspaces=true –  egreg Apr 21 at 20:27
    
related: tex.stackexchange.com/questions/164993/… –  Jubobs Apr 21 at 20:28
    
@egreg That works perfectly; would you mind making it an answer so I can accept it? –  csl Apr 21 at 20:33

2 Answers 2

up vote 8 down vote accepted

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 )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}

\end{document}

enter image description here

share|improve this answer
    
So now my answer is completely redundant :( –  Gonzalo Medina Apr 21 at 20:46
    
So there are no visible differences, only that the engine rendering is more flexible with the second approach? –  csl Apr 21 at 20:48
    
@GonzaloMedina I removed the basewidth part. –  egreg Apr 21 at 20:49
3  
@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 at 20:51
    
No need for it. I've already deleted my answer. –  Gonzalo Medina Apr 21 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,
  fontadjust=true,
  basewidth=0.5em
}

\begin{document}

This is lstlisting:

\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}

This is verbatim (and what I want):

\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
      2 paxos.pack32          ( addr n v -- addr payload )
      paxos.eth_type.learn    ( addr payload -- addr payload ethtype )
      swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}

\end{document}

enter image description here

share|improve this answer
1  
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 at 20:34
1  
@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 at 20:37
    
I did, but do the two ways produce the exact same look? I couldn't notice any differences. –  csl Apr 21 at 20:39
1  
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 at 20:45

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.