4

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.

2

I've found a solution: turn on showspaces and replace visible space character with a regular space character:

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

Not pretty, but it works.

enter image description here

2

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

  • I confirm, that works too. It still seems like a work-around though. Thanks, Jon – Jon Dec 23 '16 at 11:57
1

As I cannot comment, let me suggest not a solution but an alternative to this issue: the minted package. It's a very powerful hightlighting pacakge with makes use of Pygments and fancyvrb. Here a 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}

enter image description here

The catch here is that minted has to run with -shell-escape and I'm not quite sure but maybe it needs to have python installed. Still, I truly recomend it as there's a bunch of predefined styles you can change just by preselecting it in the preamble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.