1

I'll start with a minimum failing example:

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

weird lstlisting output with an unexplainable linebreak after the second EUREKA

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!

1

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.

Imho it would make sense to set the relevant demerits to zero for listing:

\makeatletter
\lst@AddToHook{InitVars}{\finalhyphendemerits=0\relax\doublehyphendemerits=0\relax}
\makeatother
1

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.