使用或时,替换listings
后立即丢失空格的问题(使用 时正确显示)。下面是一个小例子,问题是替换运算符后的空格丢失了。literate
columns=spaceflexible
columns=fullflexible
columns=flexible
注意:此问题与今天早些时候发布过一篇,但这次问题涉及替换后的空格,并且在和模式literate
下该空格都会丢失(上一个问题涉及字符串替换,并且仅在模式下发生错误)。\lstinline
lstlisting
\lstinline
\documentclass{article}
\usepackage{listings}
\lstset{literate=*{{+}{*}{1}}, columns=fullflexible}
\begin{document}
\noindent
\begin{tabular}{ll}
flexible & \lstinline[columns=flexible]!1 + 1!\\
spaceflexible & \lstinline[columns=spaceflexible]!1 + 1!\\
fullflexible & \lstinline!1 + 1!\\
\end{tabular}
\bigskip
\noindent
Within lstlisting environment with columns=flexible
\begin{lstlisting}[columns=flexible]
1 + 1
\end{lstlisting}
\noindent
Within lstlisting environment with columns=spaceflexible
\begin{lstlisting}[columns=spaceflexible]
1 + 1
\end{lstlisting}
\noindent
Within lstlisting environment with columns=fullflexible
\begin{lstlisting}
1 + 1
\end{lstlisting}
\end{document}
答案1
您可以使用选项keepspaces
。有关更多详细信息,请参阅文档。
\documentclass{article}
\usepackage{listings}
\lstset{literate=*{+}{*}{1}, columns=fullflexible,keepspaces}
\begin{document}
\noindent
\begin{tabular}{ll}
flexible & \lstinline[columns=flexible]!1 + 1!\\
spaceflexible & \lstinline[columns=spaceflexible]!1 + 1!\\
fullflexible & \lstinline!1 + 1!\\
\end{tabular}
\bigskip
\noindent
Within lstlisting environment with columns=flexible
\begin{lstlisting}[columns=flexible]
1 + 1
\end{lstlisting}
\noindent
Within lstlisting environment with columns=spaceflexible
\begin{lstlisting}[columns=spaceflexible]
1 + 1
\end{lstlisting}
\noindent
Within lstlisting environment with columns=fullflexible
\begin{lstlisting}
1 + 1
\end{lstlisting}
\end{document}