7

When I use columns=fixed to present my listings, the letters are spaced too far apart. Why is that?

\documentclass{article}

\usepackage{listings}
\lstset{columns=fixed, basicstyle=\ttfamily}}

\begin{document}
\begin{lstlisting}
Hello, world!
\end{lstlisting}
\end{document}

enter image description here

1 Answers1

12

The problem, as user egreg has previously pointed out, is that

the default setting of listings with "fixed columns" has a space 0.6em wide, while the characters in Computer Modern Typewriter are 0.5em wide. Fixed Width Font with LTXexample environment

One can use the basewidth key to fix this.

\documentclass{article}

\usepackage{listings}
\lstset{columns=fixed, basicstyle=\ttfamily, basewidth=0.5em}

\begin{document}
\begin{lstlisting}
Hello, world!
\end{lstlisting}
\end{document}

enter image description here

The answer linked above also provides a more flexible solution that works regardless of which monospaced font you use.