列表包 lstinline 命令在双引号后有奇怪的间距行为

列表包 lstinline 命令在双引号后有奇怪的间距行为

我在 lstinline 中双引号后的空格方面遇到了问题。我已经识别了morestring={[b]"}和的组合basicstyle=\ttfamily,它们的行为方式都让我无法理解。有人能告诉我,这是列表中的错误还是我对如何使用它的理解有误?例如,latex 代码,

\documentclass{article}
\usepackage{listings}

\begin{document}
\noindent Here is an example of gobbling spaces after quotes in lstinline. I am testing the combinations of with and without '[b]"' and ttfamily fonts:

\begin{description}
\item[No arguments: 2nd space is removed]~\\
  \begin{description}
  \item[0:] \lstinline !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[{[b]"}: 1st space is removed]~\\
  \begin{description}
  \item[0:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[ttfamily: more than 1 space removed]~\\
   \begin{description}
  \item[0:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[{[b]"} and ttfamily - first 2 spaces are removed]~\\
   \begin{description}
  \item[0:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"   3!
  \end{description}
\end{description}
Which is different when using begin-end-lstlisting, no arguments:
\begin{lstlisting}
printfn "Test 1 2 \%d"3
printfn "Test 1 2 \%d" 3
printfn "Test 1 2 \%d"  3 
printfn "Test 1 2 \%d"   3 
\end{lstlisting}
\end{document}

给出所有不同的输出,其中最后的 begin-end-lstlisting 是我所期望的:

在此处输入图片描述

谢谢。

答案1

那么使用keepspaces钥匙怎么样?

keepspaces=true 告诉包不要删除空格来修复列对齐,并始终将制表符转换为空格。

您只需添加这一行:

\lstset{keepspaces=true}

完整代码如下:

\documentclass{article}
\usepackage{listings}
\lstset{keepspaces=true} % add this line
\begin{document}
\noindent Here is an example of gobbling spaces after quotes in lstinline. I am testing the combinations of with and without '[b]"' and ttfamily fonts:

\begin{description}
\item[No arguments: 2nd space is removed]~\\
  \begin{description}
  \item[0:] \lstinline !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[{[b]"}: 1st space is removed]~\\
  \begin{description}
  \item[0:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[morestring={[b]"}] !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[ttfamily: more than 1 space removed]~\\
   \begin{description}
  \item[0:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[basicstyle=\ttfamily] !printfn "Test 1 2 \%d"   3!
  \end{description}
\item[{[b]"} and ttfamily - first 2 spaces are removed]~\\
   \begin{description}
  \item[0:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"3!
  \item[1:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d" 3!
  \item[2:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"  3!
  \item[3:] \lstinline[morestring={[b]"},basicstyle=\ttfamily] !printfn "Test 1 2 \%d"   3!
  \end{description}
\end{description}
Which is different when using begin-end-lstlisting, no arguments:
\begin{lstlisting}
printfn "Test 1 2 \%d"3
printfn "Test 1 2 \%d" 3
printfn "Test 1 2 \%d"  3 
printfn "Test 1 2 \%d"   3 
\end{lstlisting}
\end{document}

结果如下:

在此处输入图片描述

答案2

我找到了一个解决方案:打开 showspaces 并用常规空格字符替换可见空格字符:

\lstset{showspaces=true}
\makeatletter
\def\lst@visiblespace{ }
\makeatother

虽然不漂亮,但是确实管用。

在此处输入图片描述

答案3

由于我无法发表评论,因此我建议的不是解决方案,而是针对此问题提出的替代方案:minted包。这是一个非常强大的高亮包,利用了 Pygments 和fancyvrb。这里是 MWE:

\documentclass{article}
\usepackage{minted}

\begin{document}
\noindent Here is an example of gobbling spaces after quotes in lstinline. I am testing the combinations of with and without '[b]"' and ttfamily fonts:

\begin{description}
    \item[No arguments: 2nd space is removed]~\\
    \begin{description}
        \item[0:] \mint{fsharp}{!printfn "Test 1 2 \%d"3!}
        \item[1:] \mint{fsharp}{!printfn "Test 1 2 \%d" 3!}
        \item[2:] \mint{fsharp}{!printfn "Test 1 2 \%d"  3!}
        \item[3:] \mint{fsharp}{!printfn "Test 1 2 \%d"   3!}
    \end{description}
\end{description}
Which is different when using begin-end-lstlisting, no arguments:
\begin{minted}{fsharp}
printfn "Test 1 2 \%d"3
printfn "Test 1 2 \%d" 3
printfn "Test 1 2 \%d"  3 
printfn "Test 1 2 \%d"   3 
\end{minted}
\end{document}

在此处输入图片描述

这里的问题是它minted必须与 Python 一起运行-shell-escape,我不太确定,但也许需要安装 Python。不过,我还是强烈推荐它,因为有很多预定义的样式,您只需在序言中预先选择它即可更改。

相关内容