# Weird Vertical Spacing of '*' with Fira Code in Listings

I am recently getting started with LuaLaTeX and I was trying to set up a custom listings style for C++. Consider the following MWE:

\documentclass{scrartcl}

\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{listings}

\newfontfamily{\firaseries}{Fira Code}

\lstdefinestyle{mStyle}{
language=C++,
basicstyle=\footnotesize\firaseries,
}

\lstset{style=mStyle}

\begin{document}
Default font text with *.

\firaseries This is text in Fira Code with *.

\begin{lstlisting}
Listing text with *.
/* This is a comment. */
This should not be red.
\end{lstlisting}
\end{document}

This produces the following output:

As you can see, the * characters are way too low when used in the listing.

In an attempt to fix this issue, I added this line to my style definition:

literate={*}{{\firaseries*}}1

This yields the desired vertical position but unfortunately, it creates a new problem: Now the text after a multi-line comment is also red.

I would very much appreciate any pointers on how to fix this problem.

• Welcome to TeX.SX! If I remembered correctly, the /* and */ are two pairs of characters which have ligatures in Fira Code. I suspect the problem comes from how listings treats the special * character. – Ruixi Zhang Dec 12 '18 at 15:43
• BTW, simply using literate={*}{*}1 suffices to get the * up. But still no ligature :( – Ruixi Zhang Dec 12 '18 at 16:01
• @RuixiZhang Oh, that's good to know as well. I do not care about ligatures in this case anyway so it's not too much of an issue for me. :) – Leon Dec 12 '18 at 16:04
• Still, I am searching for a solution that enables Fira Code's ligatures. Here is a comparison between Ulrike Fischer's nice answer and what I intended to work on: i.stack.imgur.com/4CzUM.png – Ruixi Zhang Dec 12 '18 at 16:12

Although @UlrikeFischer’s answer solves the OP, it is still worth mentioning that there are hidden traps. Consider the following “enhanced” example from the OP:

\documentclass{scrartcl}
\usepackage{fontspec,xcolor,listings}
\newfontfamily\firaseries{Fira Code}
\lstdefinestyle{LeonStyle}{
language=C++,
basicstyle=\footnotesize\firaseries,
}
\lstset{style=LeonStyle}
\begin{document}
\begin{lstlisting}
Listing text with *.
C++  a<=b  c=2-1
/* This is a comment. */
This should not be red.
\end{lstlisting}
\end{document}

The hyphen-minus symbol is wrong, too. In fact, it is replaced with a math mode minus $-$ by the listings package.

# The easiest solution

Don’t load Fira Code via \newfontfamily. Instead, load the font via \setmonofont and use basicstyle=\ttfamily. This signifies Fira Code as a monospaced typeface and prevents listings from replacing certain symbols.

\documentclass{scrartcl}
\usepackage{fontspec,xcolor,listings}
\setmonofont{Fira Code}
\lstdefinestyle{LeonStyle}{
language=C++,
basicstyle=\footnotesize\ttfamily,
}
\lstset{style=LeonStyle}
\begin{document}
\begin{lstlisting}
Listing text with *.
C++  a<=b  c=2-1
/* This is a comment. */
This should not be red.
\end{lstlisting}
\end{document}

# More elaborate solution

Over the past few days, I have written a small package lstfiracode (shameless plug). It is mainly designed to utilize the Fira Code programming ligatures in the lstlisting environment. If you are using Fira Code, then you probably want the ligatures (otherwise you could just use Fira Mono). Here is how you would implement lstfiracode:

\documentclass{scrartcl}
\usepackage{fontspec,xcolor,listings}
\usepackage{lstfiracode} % https://ctan.org/pkg/lstfiracode
\setmonofont{Fira Code}[Ligatures=Common,Contextuals=Alternate]
\lstset{
language=C++,
style=FiraCodeStyle,
basicstyle=\footnotesize\ttfamily,
}
\begin{document}
\begin{lstlisting}
Listing text with *.
C++  a<=b  c=2-1
/* This is a comment. */
This should not be red.
\end{lstlisting}
\end{document}

The lstfiracode package is now on CTAN (2018/12/17). It will be available on all the mirrors and the major TeX distributions in the next few days. Enjoy! :)

You can raise the asterix like this:

\documentclass{scrartcl}

\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{listings}

\newfontfamily{\firaseries}{Fira Code}

\lstdefinestyle{mStyle}{
language=C++,
basicstyle=\footnotesize\firaseries,
}

\lstset{style=mStyle}
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2A}{%
\lst@ttfamily
{*}% used with ttfamily
{\raisebox{1ex}{\textasteriskcentered}}}% used with other fonts
\@empty\z@\@empty
\makeatother

\begin{document}
Default font text with *.

\firaseries This is text in Fira Code with *.

\begin{lstlisting}
Listing text with *.
/* This is a comment. */
This should not be red.
\end{lstlisting}
\end{document}

Something like this would work too (or you combine it with a \raisebox):

\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2A}{%
\lst@ttfamily
{*}% used with ttfamily
{*}}% used with other fonts
\@empty\z@\@empty
\makeatother
• This is great, thank you! Although I have no idea why or how this works ... :) – Leon Dec 12 '18 at 16:02