Weird Vertical Spacing of '*' with Fira Code in Listings
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,
commentstyle=\color{red}
}
\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,
commentstyle=\color{red}
}
\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,
commentstyle=\color{red}
}
\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,
commentstyle=\color{red}
}
\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