# Why does lstlisting removes a space after morestring?

I have troubles with a specific code in lstlistings that does not layout correctly.

\documentclass{article}
\usepackage{listings}
\lstset{morestring=[b]",basicstyle=\ttfamily,}

\begin{document}
\lstinline^x: "" x^
\end{document}


produces:

in which a space after "" (before the second x) is missing. Even slight modifications produce correct results, unfortunately I need it with text, colon, space, quotes ("") and then space (and this combination seems to always fail).

Note: I need to use lstlistings because I also use stringstyle=\color{purple}\ttfamily and other color styles not shown in the MWE.

Thanks and best regards, Markus

• How about inputting \lstinline^x: "" x^ with 2 spaces following the last "? The output then provides a single space. – Steven B. Segletes Sep 6 '17 at 19:45
• Alternatively, \lstinline^x: ""^\lstinline^ x^ works. – Steven B. Segletes Sep 6 '17 at 19:46
• Or even {\frenchspacing\texttt{x: "" x}} – Steven B. Segletes Sep 6 '17 at 20:00
• The double spacing seems to lead to one and a half spacing, it does not properly align with the next line. (It is a viable workaround for my lstlinline case above, though.) – markus23 Sep 6 '17 at 20:05
• I cannot (easily) use texttt thus there are colors in the quotes and at other places. I removed the colors because they were not necessary to reproduce the problem. – markus23 Sep 6 '17 at 20:07

It looks like a bug to me (but as the handling of spaces in listings is rather complicated it is difficult to be really sure), so you should report it.

It works with visible spaces, so as a workaround you could this:

\documentclass{article}
\usepackage{listings}
\lstset{morestring={[b]"},basicstyle=\ttfamily,showspaces}
\makeatletter
\def\lst@visiblespace{\lst@ttfamily{\phantom{\char32}}{\phantom{\textvisiblespace}}}
% or
%\def\lst@visiblespace{\lst@ttfamily{\ }{\ }}
\makeatother
\begin{document}

\lstinline^x: "" x^

\end{document}


• Thank you for helping me again! The workaround works fabulous and I could not find any negative side effects. I reported it as potential bug, thanks for confirming it. – markus23 Sep 6 '17 at 21:44
• Btw. can you maybe clarify why you commented out the shorter (and more elegant) lst@visiblespace definition? – markus23 Sep 6 '17 at 21:47
• The other one shows more clearly the original definition, also I'm not quite sure if the normal spaces behaves well with e.g. flexible columns or other fonts. – Ulrike Fischer Sep 7 '17 at 8:33