# LaTeX listings Package Only gives Unbreaking Spaces when in tex4ht

I am trying to make a programming e-book, but the listings package is converting all of the spaces in the listings to non-breaking spaces. I have tried breakatwhitespace as both true and false, as well as several other settings. I have tried defining \def\lst@outputspace{\char32} and \def\lst@visiblespace{\char32} (as well defining them to be HCode{ } and \), though I am not sure I am doing that correctly. In any case I can't seem to get it to stop outputting non-breaking spaces when run through tex4ht.

The way I know they are non-breaking spaces is that, (1) when I look at it in an e-book reader, they don't break if the book is too small, and (2) in the HTML code, it is peppered with x00A0 (I left off the HTML-y parts because I didn't know how to write those here).

Ideally, leading space would be non-breaking, but all other spaces would be normal spaces.

Also, if there is a different package I should be using, that is great. I just need verbatim inputs from an external file.

I am using tex4book to build, if that makes any difference.

We can use lst@outputspace for configuring inter-word space, but id doesn't suffice to set it to normal space, we must also configure CSS for listing environment to break lines.

Try this config file:

\Preamble{xhtml}

\begin{document}
\makeatletter
\def\lst@outputspace{\HCode{ }}
\makeatother
\Css{div.lstlisting{white-space:pre-wrap;}}
\EndPreamble

default white-space property for lstlisting environment is nowrap, which according to Mozzila developer network:

Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text.

This is clearly not something we want, pre-wrap seems better:

Sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

Some sample - without linebreak:

and with linebreak: