2

I'm trying to use Fira Code on listings with a background. However, the background lines break above (, ), { and }. At first I thought this was a problem with custom fonts in general, but after testing with a random monospaced font I have in my system (LiberationMono-Regular), it seems to work fine. Here's a MWE:

\documentclass{article}

\usepackage{fontspec, listings, caption, xcolor, tcolorbox}

\DeclareCaptionFont{white}{\color{white}\bfseries}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{labelformat=simple,format=listing,labelfont=white,textfont=white}

\lstset{
    basicstyle=\ttfamily,
    backgroundcolor=\color[rgb]{0.9,0.9,0.9},
}

\begin{document}
\begin{lstlisting}[caption=Useful Code (Regular)]
// Some very useful code
// It truly does some amazing stuff
for (int i = 0; i <= matrix->width; i++) {
    for (int j = 0; j < matrix->height; j++) {
        matrix[i][j] = matrix[i][j];
    }
}
\end{lstlisting}
\setmonofont{LiberationMono-Regular.ttf}
\begin{lstlisting}[caption=Useful Code (Liberation)]
// Some very useful code
// It truly does some amazing stuff
for (int i = 0; i <= matrix->width; i++) {
    for (int j = 0; j < matrix->height; j++) {
        matrix[i][j] = matrix[i][j];
    }
}
\end{lstlisting}
\setmonofont{Fira Code Regular}
\begin{lstlisting}[caption=Useful Code (Fira Code)]
// Some very useful code
// It truly does some amazing stuff
for (int i = 0; i <= matrix->width; i++) {
    for (int j = 0; j < matrix->height; j++) {
        matrix[i][j] = matrix[i][j];
    }
}
\end{lstlisting}
\end{document}

enter image description here

Note: In this example I'm using a caption design developed in this answer. This is an important detail, since it means that simple solutions with tcolorbox won't suffice, as it also wraps around the caption, breaking the look that this design aims for.

Why does this happen, and how can it be fixed?

3
  • Using tcblisting is certainly the best approach. You just need to declare the caption with some approach, e.g. \captionof command.
    – Alan Xiang
    Dec 16 '20 at 4:44
  • Fira is larger, you should adapt the line spacing. But using tcolorbox is certainly recommended. It has all the means to create your layout. Dec 16 '20 at 7:32
  • @UlrikeFischer @AlanXiang Feel free to post an answer. I have nothing against the use of tcolorbox, it just happens that most solutions that I've seen using it don't go well with this design, and I don't have the knowledge to adapt it. However, if you know how to, feel free to leave your take using tcolorbox or tcblistings. Dec 16 '20 at 12:35
3

You can clearly see that parentheses and braces are doing that. The reason is that they are quite higher than the corresponding characters in the standard font.

You have a couple of choices available.

1. Increase the leading inside listings

\documentclass{article}

\usepackage{fontspec, listings, caption, xcolor, tcolorbox}

\setmonofont{Fira Code Regular}

\DeclareCaptionFont{white}{\color{white}\bfseries}
\DeclareCaptionFormat{listing}{%
  \colorbox{gray}{%
    \parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}%
  }%
}
\captionsetup[lstlisting]{
  labelformat=simple,
  format=listing,
  labelfont=white,
  textfont=white
}

\lstset{
    basicstyle=\linespread{1.1}\ttfamily,
    backgroundcolor=\color[rgb]{0.9,0.9,0.9},
}

\begin{document}

Text \texttt{Text} \{x\}\texttt{\{x\}}

\begin{lstlisting}[caption=Useful Code (Regular)]
// Some very useful code
// It truly does some amazing stuff
for (int i = 0; i <= matrix->width; i++) {
    for (int j = 0; j < matrix->height; j++) {
        matrix[i][j] = matrix[i][j];
    }
}
\end{lstlisting}

\end{document}

enter image description here

2. Reduce the size of the font

\documentclass{article}

\usepackage{fontspec, listings, caption, xcolor, tcolorbox}

\setmonofont{Fira Code Regular}[Scale=MatchLowercase]

\DeclareCaptionFont{white}{\color{white}\bfseries}
\DeclareCaptionFormat{listing}{%
  \colorbox{gray}{%
    \parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}%
  }%
}
\captionsetup[lstlisting]{
  labelformat=simple,
  format=listing,
  labelfont=white,
  textfont=white
}

\lstset{
    basicstyle=\ttfamily,
    backgroundcolor=\color[rgb]{0.9,0.9,0.9},
}

\begin{document}

Text \texttt{Text} \{x\}\texttt{\{x\}}

\begin{lstlisting}[caption=Useful Code (Regular)]
// Some very useful code
// It truly does some amazing stuff
for (int i = 0; i <= matrix->width; i++) {
    for (int j = 0; j < matrix->height; j++) {
        matrix[i][j] = matrix[i][j];
    }
}
\end{lstlisting}

\end{document}

enter image description here

Or with Scale=0.9

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.