# [Tex/LaTex] How to deal with very long lines that do not contain spaces

line-breakinglistings

I have a problem with a lstlisting environment. I want to use it for displaying several very long lines that can not be reformatted. Independently of the option breakatwhitespace=true or false my lines are not broken down so that the end is far outside of the printable area.

How to specify that line breaks are allowed at any character? Or is it possible to specify the characters (besides whitespace) that can be used for a line break?

Alternative: inserting a special (not-printed) character that indicates "break line here" – doe something like this exists?

\begin{lstlisting}[]
Test1: 33b7a2f7c4cc93c46dd4ee2ed81aa1eb-9409135542c79d1ed50c9fde07fa600a_cce5a2fe76bfbd0c48d79fb43a7106f0_263e9a8711c1400fb2a716a1b820ac9a
Test2: 33b7a2f7c4cc93c46dd2423423423423-9409135542c79d1ed50c9fde07fa600a_cce5a2fe76bfbd0c48d79fb43a7106f0_263e9a8711c1400fb2a716a1b820ac9a
\end{lstlisting}


Update:

The answer from egreg works fine. Additionally I found a different approach which works directly with a lstlisting environment:

\begin{lstlisting}[breakatwhitespace=true, literate={\-}{}{0\discretionary{-}{\\}{}}]
...
\end{lstlisting}


It allows to break the lines also at hyphenations. It has some drawbacks especially that the line breaks are looking different from the others in the environment. But may be for others this code may be helpful…

Found here: LaTeX: Lstinline and Hyphenations

\begin{lstlisting}[language=TeX,breaklines]
...
\end{lstlisting}


works, depending however on the language used. For instance, with TeX as language.

Another solution, similar to the one proposed by projetmbc is

\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
{\ttfamily\hfuzz=0.4em
\list{}{\leftmargin=2em
\itemindent=-\leftmargin
\listparindent=-\leftmargin
\parsep=0pt}
\item\relax\obeylines\breakable@texttt}
{\endlist}
\def\breakable@texttt#1{%
\ifx#1\end
\expandafter\end
\else
\expandafter\ifx\bt@eol#1%
#1%
\else
\string#1\hskip1sp
\fi
\expandafter\breakable@texttt
\fi}
\makeatother


Then

\begin{breakabletexttt}
<long line 1>
<long line 2>
...
\end{breakabletexttt}


will print the lines breaking them when at the right margin. The \hfuzz=0.4em allows at most one character to stick out (the line width should be made an integer multiple of the monospaced font characters, or a flexible space should be added between characters instead of \hskip1sp).

EDIT: the following variant will respect spaces

\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
{\ttfamily\hfuzz=0.4em
\list{}{\leftmargin=2em
\itemindent=-\leftmargin
\listparindent=-\leftmargin
\parsep=0pt}
\item\relax\obeylines\obeyspaces\expandafter\breakable@texttt\@gobble}
{\endlist}
\def\breakable@texttt{\futurelet\@let@token\breakable@texttti}
\def\breakable@texttti#1{%
\ifx\@let@token\end
\expandafter\end
\else
\expandafter\ifx\bt@eol\@let@token
\par
\else
\string#1\hskip1sp
\fi
\expandafter\breakable@texttt
\fi}
\makeatother


It's important to code as

\begin{breakabletexttt}
line
...
\end{breakabletexttt}


with a new line after the \begin.