inlinelisting 在关键字后添加空格

inlinelisting 在关键字后添加空格

我在使用宏时遇到了问题lstinline。它一直在关键字“Module”后面添加空格 - 请参阅 MWE。我该如何说服 Latex 不要这样做?

我已经尝试在参数中包含“。” \koda,但是结果却出现了 2 个空格。

\documentclass{book}
\usepackage{listings} 

\newcommand{\koda}[1]{%
\lstinline[language=[Visual]Basic,keywordstyle=\ttfamily\color{blue}, %
basicstyle=\rmfamily\normalsize, prebreak=, %
morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}]
{#1}} %

\begin{document}
An example with \koda{Console, Sub et Module}. Continued text ...
\end{document}

提前致谢!

答案1

如果您通过在语言和加载周围添加花括号来修复示例xcolor,则间距不会出现任何问题代码。但间距仍然存在问题 :有一个多余的空格。我尝试过删除空格并放入%'s,但似乎没有办法解决这个问题。另外,它似乎特定于[Visual]Basic;如果您尝试[Visual]C++,多余的空格就会消失。如果您删除 中的语言设置\lstinline,并在定义\lstset{language=[Visual]Basic}之外使用\koda,空格也会消失。也许是 Visual Basic 定义中的某个地方有错误?

在此处输入图片描述

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings} 

\newcommand{\koda}{%
  \lstinline[language={[Visual]Basic},
    keywordstyle=\ttfamily\color{blue},
    basicstyle=\rmfamily\normalsize,
    prebreak=,
    morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}]}

\begin{document}

An example with LEFT\koda{Console, Sub et Module}RIGHT. Continued text ...

\end{document}

答案2

之前的空格是lstmisc.sty(2007/02/22 v1.4) 中的一个错误,出现在以下“方面”的第 849 行:

\lst@BeginAspect[keywords,comments]{keywordcomments}
...
\lst@ProvideFamily@@
    \lst@KCAkeywordsB@list\lst@KCAkeywordsB \lst@KC@also
    \lst@gKCAkeywordsB@list\lst@gKCAkeywordsB \lst@KCAWorkB
    {kcb}owo % prefix, other key, working procedure, Output hook
...
\lst@EndAspect

第 849 行应该删除注释前的空格:

    {kcb}owo% prefix, other key, working procedure, Output hook

(下一行没有注释,但仍然有效,因为行尾字符 13 ( ^^M) 的 catcode 为 9,表示忽略。)

解决方法

如果语言在序言中加载,则空白以垂直模式执行不会造成任何损害:

\lstloadlanguages{[Visual]Basic}

相关内容