3

Consider the MWE

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{libertine}

\usepackage{listings}


\lstset{
  columns=[l]flexible,
  breaklines,
  % fontadjust,
  keepspaces,
  basicstyle={\sffamily}, % \ttfamily
}
\begin{document}

\begin{lstlisting}
bla(M) = f(Q, C)
{P}
\end{lstlisting}

\end{document}

I get the horribly spaced

bad

By commenting \usepackage[T1]{fontenc} the second line is fixed (the brackets are different)

second ok

Or, I can fix the kerning manually abusing the literate option

\begin{lstlisting}[
  literate={
    {Q}{{\kern -.25ex Q\kern .25ex}}1
    {M}{{\kern -.2ex M\kern .2ex}}1
    {C}{{\kern -.2ex C\kern .2ex}}1
    {\}}{{\kern .4ex\}}}1
    {\{}{{\{\kern -.4ex}}1
}]
bla(M) = f(Q, C)
{P}
\end{lstlisting}

obtaining

hack

How can I properly fix the kerning once for all? Where's the miscommunication between the packages happening?

7
  • The problem has nothing to do with fontenc or the T1 encoding. It is a problem in the font. The reason not loading T1 appears to avoid the issue is that LaTeX falls back to using Computer Modern for the curly brackets in that case. This is because, when T1 is loaded, it uses curly brackets from the active T1-encoded font. When OT1 is active, it takes curly brackets from the active OMS-encoded font, because OT1 doesn't provide them. But the only OMS-encoded font it has available is Computer Modern, the fallback option. So it uses those and the problem doesn't manifest.
    – cfr
    Mar 8, 2015 at 1:09
  • The proper fix for this is to fix the fonts. However, I'm not sure how much luck you'll have with that since I seem to remember that libertine is now only being actively developed for OTF support (i.e. Xe/LuaLaTeX which can use opentype directly). So I'm not sure any bug fix will make it into libertine support for the fonts you are using.... [But I'm not sure about any of this so it is worth asking - I might be misremembering altogether.]
    – cfr
    Mar 8, 2015 at 1:13
  • @cfr thanks for looking into this. Re the brackets I suspected as much. Libertine is such a promising font, it's a pity it is not as refined as could be when using it with pdflatex...
    – Bordaigorl
    Mar 8, 2015 at 1:17
  • 1
    Well, it ought to be corrected in the fonts themselves. It could be done in generating the TeX Font Metric files (.tfms) for TeX. That is, by default, the .tfm generation usually takes kerning information etc. from the .afm for the font. But you can change it when creating the .tfms. But either the kerning is wrong or the width of the character is wrong in the original font, I assume. For type1, this is in the .afm file. (Although the .tfm may be generated directly in this case since I think the type1 are conversions anyway. So the .otf or whatever should be corrected.)
    – cfr
    Mar 8, 2015 at 1:25
  • 1
    Right now, I see the same issue if I compile with XeLaTeX (thus using the opentype fonts) so the bug is not in the conversions to type1.
    – cfr
    Mar 8, 2015 at 2:13

1 Answer 1

4

Use another setting for columns.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{libertine}

\usepackage{listings}


\lstset{
  columns=[l]flexible,
  breaklines,
  % fontadjust,
  keepspaces,
  basicstyle={\sffamily}, % \ttfamily
}
\begin{document}

\begin{lstlisting}
bla(M) = f(Q, C)
{P}    = f
\end{lstlisting}

\lstset{columns=fullflexible}
\begin{lstlisting}
bla(M) = f(Q, C)
{P}    = f
\end{lstlisting}


\lstset{columns=spaceflexible}
\begin{lstlisting}
bla(M) = f(Q, C)
{P}    = f
\end{lstlisting}

\lstset{columns=fixed}
\begin{lstlisting}
bla(M) = f(Q, C)
{P}    = f
\end{lstlisting}

\end{document}

enter image description here

10
  • The placement of Q is still unsatisfactory but I guess that's because of the font's design. But what if I really want the flexible spacing?
    – Bordaigorl
    Mar 8, 2015 at 11:20
  • Well if you really want the flexible spacing, then you shouldn't complain about the space after the brace. From the documentation "The normal flexible format will insert make-up space to fix the alignment at spaces,". Beside this: listings should show code in a sensible way. They are not showcases for beautiful (math) typography. Mar 8, 2015 at 11:25
  • Showing code in a readable way is what I am trying to do. This spacing is disrupting readability. The fixed spacing is also showing that listing seems somewhat tailored to some fonts and not others. If there is a way to solve that it may even contribute to the development of the package. I don't see why my question is not reasonable.
    – Bordaigorl
    Mar 8, 2015 at 11:32
  • 2
    Why don't you use columns=fullflexible or =space-flexible? Mar 8, 2015 at 12:26
  • 1
    @Bordaigorl Then you can use a monospaced font, which, IMO, looks much better than the default spacing of listings.
    – Manuel
    Mar 8, 2015 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .