24

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}
Henri Menke
  • 109,596
Fritz
  • 6,273
  • 31
  • 55
  • 8
    I guess only the creator of the package can answer the question. – morbusg Sep 08 '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 08 '14 at 13:09
  • 1
    re the style, there's some history in the listings package documentation, in section 2.10. but it doesn't say "why". – barbara beeton Sep 08 '14 at 13:28
  • 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. – cgnieder Sep 08 '14 at 13:39
  • The same question I asked myself when I discovered listings. As @cgnieder says, it's easy to select the style you want, but I also wonder when would one ever prefer the default style you show in the image! – Manuel Sep 08 '14 at 13:53
  • 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 08 '14 at 13:58
  • 2
    Unfortunately the original author, Carsten Heinz, abandoned the work on listings some years ago. Does anybody know why? –  Sep 08 '14 at 14:00
  • 1
    @morbusg Well, someone might know what is what Listings try to do “intelligently” which is something that seems no one understand. I see a close-vote as primarily opinion based, I don't think it's. Because no one has ever said what is it that listings try to do. In any case I think it might be Off Topic here… – Manuel Sep 08 '14 at 14:03
  • Add [columns=fullflexible] to the listing environment to see the difference which also tells you why it is as such. This is something related to the terminal input emulation fun but I can't make an example now. The explanation is in the manual somewhere – percusse Sep 08 '14 at 14:12
  • 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 08 '14 at 15:17
  • 1
    I also think it should be reopened. While my argument is clearly an opinion, asking for a technical reason why this is so (if one exists) is not opinionated. If anyone who knows such a reason comes across this question, they should be allowed to answer. – Fritz Sep 08 '14 at 16:14
  • 1
    @FrankMittelbach I disagree in this case: the question says »There have been other questions about making lstlistings look better [...] so that is not really the topic here.«. If it were rewritten it'd probably be closed as duplicate of the mentioned other question. – cgnieder Sep 08 '14 at 16:29
  • @cgnieder can accept that position didn't really read to the bottom it seems. – Frank Mittelbach Sep 08 '14 at 16:40
  • As we talk about opinions here I just want to say that I for instance like the output of listings package. I am not an expert latex user so to have seen a lot of other packages but even without customization I don't find it ugly (even thought I personally make customizations while using it). I don't want someone to mistake what I say here and take it for more than a simple "rookie" opinion! – Adam Sep 13 '14 at 00:24

1 Answers1

17

(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.

jub0bs
  • 58,916
  • 13
    Better use \lstset{basicstyle=\ttfamily}, it's the publicly documented command AFAIK. – marczellm Sep 08 '14 at 13:37
  • 4
    @marczellm That was a joke, obviously... – jub0bs Sep 08 '14 at 14:43
  • Thanks for the very elaborate (and indeed helpful) answer. I guess this is as close as we can get, so I'll accept it. – Fritz Sep 08 '14 at 19:46
  • Altough it answers in a way the question, I still wonder what is it that listings does “intelligently” with those spaces, does anyone know what does it do? I'm talking only about columns=fixed. – Manuel Sep 08 '14 at 20:42
  • 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 08 '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
  • @Daniel I'll edit my answer to make it clear that I was joking. – jub0bs Sep 12 '14 at 19:00
  • @barbarabeeton You mean similar to elastic tabstops? Maybe my example is bad because it contains no spaces on which to align things. But whatever it does, I can't make sense of why it would typeset the two uses of the word use in my example so differently. – Fritz Sep 12 '14 at 20:09
  • Thanks, almost perfect :-) I would just add the "official solution", as suggested by @marczellm, somewhere below the joke, so that users just looking for a simple fix to the problem can find it. – Daniel Sep 12 '14 at 21:46
  • 1
    @Fritz -- you're correct, and thanks for calling me on it. what i see, now that i look more closely, is that the input is parsed into words, a horizontal space is allotted for each word (that appears ultimately to equate to the space needed for presentation of the monospace form), and within that space the word is letterspaced evenly. you're quite correct -- the two lines in the example you show don't have letter-by-letter alignment. – barbara beeton Sep 13 '14 at 12:34