2

Below are two examples of fonts. The first is from the verbatim environment (which I read has default font ttfamily). The second is from a lstlisting with basicstyle=\ttfamily. They look different. It looks like the second has more space between the letters. What is happening? And how to I get the lstlisting example to look like the verbatim example?

two code fonts

2
  • 1
    Look for basewidth in the listings documentation to adjust the width for every character. If you want a more detailed answer, you should add a MWE. Commented May 30, 2021 at 17:31
  • 1
    Please consider to upvote and/or accept the provided answer so that the process comes to an end :). Commented May 31, 2021 at 2:02

1 Answer 1

5

Like hinted by @MarcelKrüger the difference has to do with the basewidth key of listings.

listings prints its output to a fixed width (that can be specified with \lstset{basewidth=<width>}, or in the optional argument of lstlisting), this way also proportional fonts can be used for code listings.

You can turn this column format off by using columns=fullflexible:

\documentclass[]{article}

\usepackage{listings}

\begin{document}
\begin{verbatim}
some code about stuff
other code about stuff
\end{verbatim}

\begin{lstlisting}[basicstyle=\ttfamily,columns=fullflexible]
some code about stuff
other code about stuff
\end{lstlisting}
\end{document}

enter image description here

2
  • Amazing, thank you!
    – dfrankow
    Commented May 30, 2021 at 18:45
  • 1
    @dfrankow usually we don't say thank you via comments but via votes. But I'm glad I could help.
    – Skillmon
    Commented May 30, 2021 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .