我尝试在带有背景的列表上使用 Fira Code。但是,背景线在 、 和 上方断开(
。)
起初{
我}
以为这是自定义字体的普遍问题,但在使用系统中的随机等宽字体 (LiberationMono-Regular) 进行测试后,它似乎工作正常。这是一个 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}
注意:在此示例中,我使用了在这个答案。这是一个重要的细节,因为这意味着使用 tcolorbox 的简单解决方案还不够,因为它还会环绕标题,破坏此设计所追求的外观。
为什么会发生这种情况?如何解决?
答案1
您可以清楚地看到圆括号和大括号的作用。原因是它们比标准字体中相应的字符高得多。
您有多种选择。
1. 增加内部领先列表
\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}
2.减小字体大小
\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}
或者Scale=0.9