23

I always thought the purpose of LaTeX was to make it easy to create beautiful documents. Yet every time I see one of those lstlistings I can't help but wince at the mutilated kerning. Take for example this simple snippet taken from the \tikzmark documentation:

What's happening here?

Why are the two highlighted uses of the word use typeset so differently? It's not monospace and not proportional but something... else. I added vertical lines between the top characters to showcase the wacky alignment.

This is also the reason I normally don't use that package at all and resort to tabulars with \ttfamily or verbatim environments.

So, why does it do this? Why make ugly the default and make people search for a way to make it look good (which nobody seems to be bothered with, judging from the amount of papers and lecture notes containing ugly listings), instead of making it look nice by default? Is there a good technical or typographic reason to do it this way instead of using a proper monospace font—which should always be present—or at least properly using the proportional font?

Are there other packages that do a better job at listing actual program code (i.e. not algorithms in pseudocode) by default?

There have been other questions about making lstlistings look better, like How can I make lstlisting look exactly like verbatim? or lstlisting, tt fonts, and alignment., so that is not really the topic here.

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\end{lstlisting}
\end{document}
  • 8
    I guess only the creator of the package can answer the question. – morbusg Sep 8 '14 at 13:05
  • 2
    To answer what "other packages" there might be, it might help if you also provide what features are essential for your application. For example, keyword highlighting, etc. – Steven B. Segletes Sep 8 '14 at 13:09
  • 2
    adding \lstset{basicstyle=\ttfamily,columns=fullflexible} is easy enough. Why the default is as it is is pure speculation as long as we can't ask the original author. IMHO there are no better packages than listings when it comes to typesetting code (even with all the flaws it might have) (well, minted maybe...) but this is again speculation and opinion-based. There is no good answer to this question so IMHO it should be closed. – clemens Sep 8 '14 at 13:39
  • 4
    No, there's no good reason other than “the original author of listings liked it”. Whether it's good or bad is a matter of opinion; the package easily allows to change the setup. – egreg Sep 8 '14 at 13:58
  • 4
    I think that this question is quite pertinent even if it is written as an opinionated question on "look and feel". It could be easily rewritten as "how can one get monospace typesetting in listings ... and that is an important question. (personally I think you get too hooked up on throwing out questions because they use the words "ugly" etc. :-) – Frank Mittelbach Sep 8 '14 at 15:17
16

(Actual answer: use \lstset{basicstyle=\ttfamily})


Joke answer (Do not do that!)

I'll fix that for you. Here is a custom package called fritzlistings, built on top of listings, that uses a typewriter font by default.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fritzlistings}%
 [2014/09/08 v0.1 Fritz's listings]
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{listings}}
\ProcessOptions\relax
\RequirePackage{listings}
\lst@Key{basicstyle}\ttfamily{\def\lst@basicstyle{#1}}
\endinput

There. Happy now? :p

enter image description here

\documentclass{article}

\usepackage{fritzlistings}

\begin{document}
\begin{lstlisting}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\end{lstlisting}
\end{document}

More seriously...

I can't help but wince at the mutilated kerning. [...] This is also the reason I normally don't use that package at all and resort to tabulars with \ttfamily or verbatim environments.

If you're typesetting source code, what would be the advantage of using a tabular environment with \ttfamily versus using an lstlisting environment? I can't think of any. Besides, you can configure listings to replicate the look of a verbatim environment (as shown in How can I make lstlisting look exactly like verbatim?), but with the nonnegligible benefit of automatic line breaking.

Why make ugly the default and make people search for a way to make it look good (which nobody seems to be bothered with, judging from the amount of papers and lecture notes containing ugly listings), instead of making it look by default?

I agree with you that the output of listings is basic and arguably ugly, but beauty is in the eye of the beholder. What you may find beautiful, others may find ugly as f**k. I think that Carsten Heinz, the author of listings, simply didn't want to impose any particular style on the user. In particular, you'll notice that language definitions in lstdvrs.dtx are just that: language definitions; keys such as keywordstyle are not used in those language definitions.

The package provides many ways of customizing the look of the output. Use them to suit your needs (I typically save my favourite styles in a custom LaTeX package), and spread the word; that will do more to improve the current state of affair than a rant.

Is there a good, technical or typographic reason to do it this way instead of using a proper monospace font (that should always be present) or at least properly using the proportional font?

I agree with you that the default columns=fixed and cmr don't go well together. Either

  • either columns=fixed and cmtt,
  • or columns=flexible (or fullflexible) and cmr,

would have made more sense to me, although others would disagree about that. So why is that the default? Who knows... All I can gather from listings.dtx is that the basicstyle key (introduced in v0.18) predates the columns key (v1.0). I don't have access to the full development history of the package, so I can only speculate.

My theory is that listings' default is what it is for historical reasons. I'm guessing that listings originally didn't tamper with the kerning at all before the inception of the columns key in v1.0; before that, cmr would have been an acceptable choice of font for listings. Then v1.0 came, but perhaps Carsten thought changing the default font to cmtt at that stage would have upset existing listings users, and he decided to stick to cmr.

Of course, only Carsten Heinz (or perhaps one of the maintainers, active or retired) could provide a conclusive rationale for this choice.

Are there other packages that do a better job at listing actual program code (not algorithms in pseudocode) by default?

Pygments-based packages, such as minted, verbments, and pythontex, have some advantages over listings, thanks to Python; in particular,

  • superior lexing/scanning capabilities,
  • full Unicode support,

come to mind. However, in terms of customisation from within LaTeX (& friends) documents, listings wins; no question about that. At least, that's the case at the moment; minted may end up catching up with listings, in this respect.

  • 12
    Better use \lstset{basicstyle=\ttfamily}, it's the publicly documented command AFAIK. – marczellm Sep 8 '14 at 13:37
  • 3
    @marczellm That was a joke, obviously... – jubobs Sep 8 '14 at 14:43
  • 1
    Maybe it is trying to equalize the space between each pair of letters on each individual line. Thus, a sequence of narrow letters like l or i will occupy less space than a sequence of wide letters like W and G – Fritz Sep 8 '14 at 21:00
  • 2
    @Fritz -- as i see it, listings is trying to make letters in a proportional font align so that a code listing will have reliable columnar alignment; this can be important in viewing code that is supposed to be aligned from line to line where horizontal position matters. (but it does look unattractive, and i personally prefer a reasonably designed monospace font for the purpose.) – barbara beeton Sep 12 '14 at 17:50
  • 3
    "That was a joke, obviously... " sorry, but I did not find that too obvious, despite the following "More seriously..." line. Given that this is a long-term Q&A site and your answer is the accepted answer, I foresee new listings users adopting your solution. – Daniel Sep 12 '14 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.