3

Fira Code (available via its GitHub Release page) not only provides fancy programming ligatures (see Use Fira Code font with ligatures in code listings), but also contains contextual substitutions. For example, the colon character : is usually vertically aligned with respect to lowercase letters, so it will appear misaligned when it is placed between uppercase letters and/or lining numerals. But Fira Code, when Contextual Alternates is enabled, can substitute the lowercase-friendly : with an uppercase-friendly one.

The Contextual Alternates feature does not work in a lstlisting environment. For the ligatures, there is lstfiracode (shameless plug), but how can one get the rest of contextual substitutions as mentioned above?

\documentclass{article}
\usepackage{fontspec}
\setmonofont{FiraCode-Regular.otf}[
  Contextuals=Alternate
]
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\makeatletter
\renewcommand*\verbatim@nolig@list{}
\makeatother
\begin{document}
\noindent
With \verb|verbatim|:
\begin{verbatim}
0xff 0xFF % x is contextually substituted
a-c  A-C  % - is contextually substituted
e+g  1+2  % + is contextually substituted
i*j  3*4  % * is contextually substituted
m:n  5:6  % : is contextually substituted
\end{verbatim}
With \verb|lstlisting|:
\begin{lstlisting}
0xff 0xFF
a-c  A-C
e+g  1+2
i*j  3*4
m:n  5:6
\end{lstlisting}
\end{document}

Contextual Alternates

1 Answer 1

6

If you inspect the output with \showoutput you can see that listings inserts stuff between the glyphs: space (glue) and \hbox. Both interfere with the contextual substituation.

Possible ways to avoid it are:

  • literate as in your sty
  • columns=fullflexible to avoid that glue is inserted
  • declaring some input as a letter to avoid that listings separate it.

Every one can have side effects on the processing of listings of keywords and so on.

\documentclass{article}
\usepackage{fontspec}
\setmonofont{FiraCode-Regular.otf}[
  Contextuals=Alternate
]
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\makeatletter
\renewcommand*\verbatim@nolig@list{}
\makeatother
\begin{document}

\showoutput 
\begin{minipage}{0.3\linewidth}
\begin{lstlisting}
0xff 0xFF
a-c  A-C
i*j  3*4
m:n  5:6  
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.3\linewidth}
\lstset{
  literate={0xff}{{0xff}}4,
  }
\begin{lstlisting}
0xff 0xFF
a-c  A-C
i*j  3*4
m:n  5:6
\end{lstlisting}
\end{minipage}
\begin{minipage}{0.3\linewidth}
\lstset{
  columns=flexible,
  alsoletter={0,*,-}
  }
\begin{lstlisting}
0xff 0xFF
a-c  A-C
i*j  3*4
m:n  5:6
\end{lstlisting}
\end{minipage}

\end{document}

enter image description here

1
  • Thank you for providing such comprehensive solutions! I will need to decide which one(s) to implement. Side note: I noticed in your picture the default zeros are dotted zeros, which was changed since Release 1.205 more than a year ago. The default zero now is slashed zero. Commented Apr 22, 2019 at 16:18

You must log in to answer this question.

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