Have a look at §2.10 Fixed and flexible columns of the listings
documentation.
In the MWE you are using columns=fixed
with a variable-width font. That means that the different glyphs/characters of the font have a different width. If you were to write a line of l
s and a line of M
s, the 'l's and 'M's would not line up. So to maintain the proper indentation and alignment of the code listings
has to do some tricks.
[T]he fixed format puts n characters into a box of width n × ‘base width’, where the base width is 0.6em in the example. The format shrinks and stretches the space between the characters to make them fit the box. [...]
If you don’t need or like this, you should use a flexible format. All characters are typeset at their natural width. In particular, they never overlap. If a word requires more space than reserved, the rest of the line simply moves to the right. The difference between the three formats is that the full flexible format cares about nothing else, while the normal flexible and space-flexible formats try to fix the column alignment if a character string needs less space than ‘reserved’.
The documentation explains
In the abstract one can say: The fixed column format ruins the spacing intended by the font designer, while the flexible formats ruin the column alignment (possibly) intended by the programmer.
Since the equal signs are quite long it just so happens that the space between them is shrunken to such as degree that the two equal signs merge together.
So what can you do to solve this problem?
Use a fixed-width typewriter font. In those fonts all characters have the same width making it unnecessary to stretch and shrink spaces a lot. Everything aligns naturally.
basicstyle=\ttfamily
If you want to keep bold and italic/slanted markup you will have to find typewriter font that supports bold and slanted. The default Computer Modern \ttfamily
has no bold version (Using \ttfamily with \bfseries (or how to enable bold in fixed-width font)). Latin Modern (\usepackage{lmodern}
) has a bold font, which is quite similar to the regular variant, so you may want to try lighttt
(in which case you need to be aware of italic font in lmodern-lighttt).
You can increase the basewidth
parameter. That makes the box into which the glyphs need to fit wider allowing for a bit of space around the equal signs. Of course all other words will suffer from increased letter spacing.
The default value for fixed mode is 0.6em, for flexible mode 0.45em. In the example here it has been increased to 0.7em. The space is still quite faint, so one might be tempted to increase the width further, but of course that drags the other letters even further apart from each other.
Use columns=flexible
or columns=fullflexible
While it may not be easy to count the exact number of spaces with here, the fact that the second line is indented is still very clearly visible. Indentation levels don't get lost with these settings only the exact number of spaces can be hard to figure out.
\documentclass{article}
\usepackage[lighttt]{lmodern}
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily,
language=python,
}
\begin{document}
\begin{lstlisting}
if something == something:
somethingelse(something)
\end{lstlisting}
\begin{lstlisting}[basicstyle=\normalfont, basewidth=0.7em]
if something == something:
somethingelse(something)
\end{lstlisting}
\begin{lstlisting}[basicstyle=\normalfont, columns=flexible]
if something == something:
somethingelse(something)
\end{lstlisting}
\begin{lstlisting}[basicstyle=\normalfont, columns=fullflexible]
if something == something:
somethingelse(something)
\end{lstlisting}
\end{document}
basicstyle=\ttfamily
, that changes the font to typewriter/fixed-width. Or you could have a look at §2.10 Fixed and flexible columns of thelistings
documentation. In particularcolumns=flexible
orcolumns=fullflexible
may be of interest. To quote the manual: "In the abstract one can say: The fixed column format ruins the spacing intended by the font designer, while the flexible formats ruin the column alignment (possibly) intended by the programmer." If you are a Python person you may want to look atminted
basicstyle=\ttfamily
.basicstyle=\ttfamily
is it removes the language specific formatting - is there any way to remedy this? Other than that it seems to work well.columns=flexible
andcolumns=fullflexible
do preserved some amount of indentation, but I'm not sure if that is what you need. It seems to me you are between a rock and a hard place:columns=flexible
gives bad results with fonts that have no fixed character widths, the simple solution would be to usebasictstyle=\ttfamily
in that case (all characters have the same width, everything is tickety boo). If you don't want\ttfamily
you need to switch to a differentcolumns
format to avoid the letters running into each other.