列表包:增加 == 中 = 之间的间距

列表包:增加 == 中 = 之间的间距

一切都在标题中。下面是最简单的例子

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{green}, % comment color
    keywordstyle=\color{blue}, % keyword color
    stringstyle=\color{blue!60} % string color
}


\begin{document}
\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
  int a = 1;
  if ( a == 1 )
    cout << "Hello world" << endl; 
  return 0;
}
\end{lstlisting}

\end{document}

产生以下输出

显示 == 输出的最小示例

我发现 == 运算符中的 = 之间的间距太小。因此我想增加它。我该怎么做?

谢谢!

答案1

您必须增加“基准宽度”,因为=它比默认的 0.6em 更宽。如果我们进行测量,我们会发现标准字体的 0.6em 大约为 6.57pt,而“=”字形的宽度大约为 8.52pt(但它有侧边距)。因此,基准宽度 0.8em 似乎是必要的。

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{green}, % comment color
    keywordstyle=\color{blue}, % keyword color
    stringstyle=\color{blue!60}, % string color
    basewidth=0.8em,
}


\begin{document}

\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
  int a = 1;
  if ( a == 1 )
    cout << "Hello world" << endl; 
  return 0;
}
\end{lstlisting}

\end{document}

在此处输入图片描述

但我更喜欢basicstyle=\ttfamily

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{green}, % comment color
    keywordstyle=\color{blue}, % keyword color
    stringstyle=\color{blue!60}, % string color
    columns=fullflexible,
    basicstyle=\ttfamily,
}


\begin{document}

\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
  int a = 1;
  if ( a == 1 )
    cout << "Hello world" << endl; 
  return 0;
}
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

一种可能的解决方案是使用literate如下示例:

清单

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{green}, % comment color
    keywordstyle=\color{blue}, % keyword color
    stringstyle=\color{blue!60}, % string color
    literate = { == }{{~=\,=~}}4
}


\begin{document}
\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
  int a = 1;
  if ( a == 1 )
    cout << "Hello world" << endl; 
  return 0;
}
\end{lstlisting}

\end{document}

相关内容