5

I have a problem using the sourcecodepro and listings. I want to suppress ligatures and bold (Java) keywords. I already tried using the literate-option but it doesn't work as expected. That's my current code:

Code:

\documentclass{minimal}

\usepackage[default]{sourcecodepro}
\usepackage{listings}

\lstset{%
  language=Java,
  keywordstyle=\bfseries,
  literate={fl}{f{}l}{2},
  morekeywords={float}
}

\begin{document}

\begin{lstlisting}
  float f;
  int i;
\end{lstlisting}

\end{document}

Result:

Result

dawu
  • 1,648

2 Answers2

8

Using an additional group around the replacement solves the problem.

source code

Btw: You should not use minimal.

\documentclass{article}

\usepackage[default]{sourcecodepro}
\usepackage{listings}

\lstset{%
  language=Java,
  keywordstyle=\bfseries,
  literate={fl}{{f{}l}}2,
  morekeywords={float}
}

\begin{document}

\begin{lstlisting}
  float f;
  int i;
\end{lstlisting}

\end{document}

Update: Disabling ligatures via microtype allows also the use of the keyword.

f ligatures

\documentclass{article}

\usepackage[default]{sourcecodepro}
\usepackage{listings}
\usepackage{microtype}
\DisableLigatures[f]{encoding = *, family = tt }

\lstset{%
  language=Java,
  keywordstyle=\bfseries,
  morekeywords={float}
}

\begin{document}

\begin{lstlisting}
  float f;
  int i;
\end{lstlisting}

floating away

\end{document}
TeXnician
  • 33,589
  • Thanks! But I also want float to be typeset bold. How can I now achieve this? PS: Good to know about minimal! :-) – dawu Jan 04 '18 at 17:23
  • @dawu Added another approach. – TeXnician Jan 04 '18 at 17:48
  • Finally, it's time for me to deal with microtype. – dawu Jan 04 '18 at 18:04
  • One last question: Why looks the 'fl' outside of lstlisting so 'strange'? Is it a lack of ligatures in sourcecodepro? (I'm new to such font details.) – dawu Jan 04 '18 at 18:38
  • 1
    @dawu What do you mean with "so strange"? It's probably how the font designer wanted it to be. Not every font has a ligature that really connects both letters. – TeXnician Jan 04 '18 at 18:43
4

The fi and fl ligatures are only produced outside listings (which shouldn't happen anyway, but it's a different problem).

\documentclass{article}

\usepackage[default]{sourcecodepro}
\usepackage{listings}

\lstset{%
  language=Java,
  keywordstyle=\bfseries,
  morekeywords={float}
}

\begin{document}

\texttt{ff fi fl ffi ffl} % bad

\begin{lstlisting}
  float f;
  int i;
\end{lstlisting}

\end{document}

enter image description here

egreg
  • 1,121,712
  • You're right. That's curious, because some time ago, I had to add literate={fl}{f{}l}{2} to suppress ligatures inside listings. Seems that this is not necessary anymore. – dawu Jan 04 '18 at 19:25