# Ligatures and highlighting using sourcecodepro and listings

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:

• I get no ligatures by default. How are you compiling? And which texsystem do you use? – Ulrike Fischer Jan 4 '18 at 18:14
• @UlrikeFischer I get them by default with pdflatex on TL2017 (Linux). With LuaLaTeX I don't get them. – TeXnician Jan 4 '18 at 18:23
• @UlrikeFischer I am using TexLive 2016 (Linux Mint). – dawu Jan 4 '18 at 18:33
• @TeXnician I don't get them (inside listings!), I would find it very odd if it happens there due to the way listings work. – Ulrike Fischer Jan 4 '18 at 18:44
• I sent email to the author of the sourcecodepro package. On the other hand, I only get the ligature outside listings (using no literate option in lstlisting). – egreg Jan 4 '18 at 19:07

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}


• 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 4 '18 at 19:25

Using an additional group around the replacement solves the problem.

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

\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}

• Thanks! But I also want float to be typeset bold. How can I now achieve this? PS: Good to know about minimal! :-) – dawu Jan 4 '18 at 17:23
• @dawu Added another approach. – TeXnician Jan 4 '18 at 17:48
• Finally, it's time for me to deal with microtype. – dawu Jan 4 '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 4 '18 at 18:38
• @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 4 '18 at 18:43