如何修复代码环境中产生奇怪符号的间距?

如何修复代码环境中产生奇怪符号的间距?

我想知道为什么会有像这样的奇怪的间距符号(在 GLSL 代码的左侧):

1

这是您可以检查并重现的代码:


\documentclass[twoside]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{enumerate}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{cite}
\usepackage{cancel}
\usepackage{ulem}
\usepackage{makecell}
\usepackage{newunicodechar}
\usepackage[utf8]{inputenc}

\usepackage{marginnote}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{float}
\usepackage{listings}
\usepackage{regexpatch}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9}
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  
\usepackage{color}
\newcounter{bash}
\makeatletter


\newcommand{\lstlistbashname}{List of Julia Codes}
\lst@UserCommand\lstlistofbash{\bgroup
    \let\contentsname\lstlistbashname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
    \renewcommand{\lstlistingname}{Julia Code}%
    \let\c@lstlisting=\c@bash
    \let\thelstlisting=\thebash
    %\xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
    \lstset{language=bash,
        keywordstyle=\sffamily\ttm,
        basicstyle=\sffamily\ttm,
        numbersep=5pt,
        frame=tb,
        columns=fullflexible,
        backgroundcolor=\color{yellow!20},
        linewidth=0.95\linewidth,
        xleftmargin=0.05\linewidth,
        breaklines=true,
        captionpos=b,
        #1}}
{}
\makeatother




%----------------------------------------------------------------------------------------

\begin{document}
    
    
\begin{bash}[caption={Bash}]
    sudo -i
    
\end{bash}

\begin{bash}[caption={GLSL}]
    const GLchar* vertexSource = R"glsl(
    #version 330 core
    in vec2 position;
    in vec3 color;
    in vec2 texcoord;
    out vec3 Color;
    out vec2 Texcoord;
    uniform mat4 trans;
    void main()
    {
        Color = color;
        Texcoord = texcoord;
        gl_Position = trans * vec4(position, 0.0, 1.0);
    }
    )glsl";
    
\end{bash}

\addcontentsline{toc}{section}{Listings}
\lstlistoflistings
\end{document}

如何修复它,使它看起来更清晰,不再有那些奇怪的间距符号?

答案1

我认为这些是空格,它们listings被视为字符串的一部分,因此在 中showstringspaces=true它们显示为空格。它们位于行首并没有什么不妥。

您可以用不可见的空格替换它们,但代价是将字符串中的其他可见空格替换为不可见的空格。

\begin{bash}[caption={Bash}]
    sudo -i
    
\end{bash}

\begin{bash}[caption={GLSL}]
    const GLchar* vertexSource = R"glsl(
    #version 330 core
    in vec2 position;
    in vec3 color;
    in vec2 texcoord;
    out vec3 Color;
    out vec2 Texcoord;
    uniform mat4 trans;
    void main()
    {
        Color = color;
        Texcoord = texcoord;
        gl_Position = trans * vec4(position, 0.0, 1.0);
    }
    )glsl";
    
\end{bash}

使代码清单中的空格不可见

或者,您至少可以删除多余的间距,但您仍然需要缩进,这样就不会完全消除可见的字符。

\begin{bash}[caption={GLSL},showstringspaces=true]
const GLchar* vertexSource = R"glsl(
#version 330 core
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 trans;
void main()
{
    Color = color;
    Texcoord = texcoord;
    gl_Position = trans * vec4(position, 0.0, 1.0);
}
)glsl";
    
\end{bash}

通过设置代码左对齐来减少空格

或者,您可以用制表符替换行首的空格,并以不同的方式处理制表符。

\begin{bash}[caption={GLSL},showstringspaces=true,showtabs=true,tab={    }]
    const GLchar* vertexSource = R"glsl(
    #version 330 core
    in vec2 position;
    in vec3 color;
    in vec2 texcoord;
    out vec3 Color;
    out vec2 Texcoord;
    uniform mat4 trans;
    void main()
    {
        Color = color;
        Texcoord = texcoord;
        gl_Position = trans * vec4(position, 0.0, 1.0);
    }
    )glsl";
    
\end{bash}

可见空格与不可见标签

完整代码:

\documentclass[twoside]{book}
\usepackage[utf8]{inputenc}

\usepackage[table, dvipsnames]{xcolor}
\usepackage{listings}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9}
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  
\newcounter{bash}
\makeatletter


\newcommand{\lstlistbashname}{List of Julia Codes}
\lst@UserCommand\lstlistofbash{\bgroup
  \let\contentsname\lstlistbashname
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
  \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
  \renewcommand{\lstlistingname}{Julia Code}%
  \let\c@lstlisting=\c@bash
  \let\thelstlisting=\thebash
  %\xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{language=bash,
    keywordstyle=\sffamily\ttm,
    basicstyle=\sffamily\ttm,
    numbersep=5pt,
    frame=tb,
    columns=fullflexible,
    backgroundcolor=\color{yellow!20},
    linewidth=0.95\linewidth,
    xleftmargin=0.05\linewidth,
    breaklines=true,
    captionpos=b,
    showstringspaces=false,
    #1}}
{}
\makeatother

\begin{document}
    
\begin{bash}[caption={Bash}]
    sudo -i
    
\end{bash}

\begin{bash}[caption={GLSL}]
    const GLchar* vertexSource = R"glsl(
    #version 330 core
    in vec2 position;
    in vec3 color;
    in vec2 texcoord;
    out vec3 Color;
    out vec2 Texcoord;
    uniform mat4 trans;
    void main()
    {
        Color = color;
        Texcoord = texcoord;
        gl_Position = trans * vec4(position, 0.0, 1.0);
    }
    )glsl";
    
\end{bash}

\begin{bash}[caption={GLSL},showstringspaces=true]
const GLchar* vertexSource = R"glsl(
#version 330 core
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 trans;
void main()
{
    Color = color;
    Texcoord = texcoord;
    gl_Position = trans * vec4(position, 0.0, 1.0);
}
)glsl";
    
\end{bash}
\clearpage

\begin{bash}[caption={GLSL},showstringspaces=true,showtabs=true,tab={    }]
    const GLchar* vertexSource = R"glsl(
    #version 330 core
    in vec2 position;
    in vec3 color;
    in vec2 texcoord;
    out vec3 Color;
    out vec2 Texcoord;
    uniform mat4 trans;
    void main()
    {
        Color = color;
        Texcoord = texcoord;
        gl_Position = trans * vec4(position, 0.0, 1.0);
    }
    )glsl";
    
\end{bash}

\addcontentsline{toc}{section}{Listings}
\lstlistoflistings
\end{document}

相关内容