[Tex/LaTex] Listings package lstinline command has strange spacing behaviour after double quotation mark

listingsspacing

I'm having problems with spacing after double quotation mark in lstinline. I've identified combinations of morestring={[b]"} and basicstyle=\ttfamily, which all behaves in manners, I cannot understand. Can anyone tell me, if this is a bug in listings or in my understanding of how to use it? E.g., the latex code,

\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}

gives all different output, where the begin-end-lstlisting last, is what I would have expected:

enter image description here

Thank you.

Best Answer

What about using the keepspaces key?

keepspaces=true tells the package not to drop spaces to fix column alignment and always converts tabulators to spaces.

You just have to add this line:

\lstset{keepspaces=true}

Here is the full code:

\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}

Here is the result:

enter image description here

Related Question