# lstlisting: Unexpected newline/missing background color after trailing space in escape section

\documentclass{article}

\usepackage{listings}
\usepackage{color}
\lstdefinelanguage{x}{
escapechar={!}
}
\lstset{language=x,
breaklines=true,
backgroundcolor=\color{yellow}
}

\begin{document}

\newcommand{\mycommand}[1]{\textbf{#1}}

\begin{lstlisting}[language=x]
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo bar bar bar bar
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo !\mycommand{EUREKA}! bar bar bar bar
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo !\mycommand{EUREKA }! bar bar bar bar
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo bar bar bar bar
\end{lstlisting}

\end{document}


Note the difference between the two EUREKA lines, where the single difference is that the second line also passes the trailing space to \mycommand. This should not have any effect on the rendering of the listing, should it? But here is the output:

This linebreak and especially the missing colors are totally unwanted here. I can reproduce the same behavior by filling in the escape section with !EUREKA ! (without command invocation) as well.

Please note that my definition of \mycommand is a gross simplification - in reality, I am doing some conditional checks here to decide whether the argument with the trailing space should be displayed or whether not. It's not possible for me to forego this space.

Is this behavior considered a bug or a feature of the listings package? In the case of the first, where could I report this bug? Or am I missing something else? I am looking forward to your support!

The problem is that breaklines is basically a hyphenation, only without hyphen. TeX tries hard to avoid that too many lines are hyphenated and so is very happy to find a space where is can inserts a line break without hyphenation.
\makeatletter