I'm having some problems with the column alignment in the lstlisting
environment provided by listings.sty
.
In fixed-width column alignment mode, whatever the typeface being used, listings are supposed to be presented with one character per column, where the columns have uniform widths (which may be set by the author). However, I find this not actually to be the case with certain input.
Minimal example:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[columns=fixed]
columnar alignment maligned multiple narrow characters?
1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234
\end{lstlisting}
\end{document}
Result:
Note that the second line is exactly one character shorter than the first, which is observed at the far right-hand side. However, the alignment in the middle is somewhat ruined at the occurance of every l
character (ambiguous alignment of u
in columnar
with 3
or 4
; ambiguous alignment of i
, g
, and n
in alignment
with 1
, 2
, 3
, and 4
; etc.). This problem is not helped by tweaking the alignment, by e.g. replacing fixed
with {[l]fixed}
for the column alignment.
Is this a known problem? Is there any fix for it?
Best Answer
This is the expected/default output for
columns=fixed
in anlstlisting
environment. From thelistings
package documentation:That is, the alignment is not meant to consistent across lines within the listing. Rather, the chunks of code is placed in boxes and the spacing is modified with no regard for anything else except the box width. Even the example presented in the documentation (page 19) exhibits this behaviour:
Notice the misaligned
i
inwrite
(line 1) andprint
(line 2) due to the widerw
.Even if you modified your minimal example to the following:
attempting to align the listing column-wise, the output looks better, but still has the same issues you mention:
Notice the misalignment between
mnar
and6789
(due tom
);tmal
and1234
(again due tom
);rowc
and6789
(due tow
), etc.The only way around this is to use a mono-spaced font for your entire listing, using (say)
basicstyle=\ttfamily
: