# How to deal with very long lines that do not contain spaces?

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

-
As per the documentation \lstset{breaklines=true,breakatwhitespace=false} should have worked, but don't seem to me for some reason. – Peter Grill Nov 2 '11 at 15:28

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

-
Bizarre that it needs a language=TeX setting to break lines. How would you do this if you had some other language language=Java – Peter Grill Nov 2 '11 at 15:31
+1 It's a strange behaviour ! – projetmbc Nov 2 '11 at 15:35
@PeterGrill I believe it depends on the routines to check for keywords. With Java it doesn't work. – egreg Nov 2 '11 at 15:42
@egreg: Using this code all existing spaces in my code are gone – Robert Nov 3 '11 at 16:12
@Robert See edit – egreg Nov 3 '11 at 18:29

I give you one partial solution given to me here. The problem is that it does not work with special character like _. One partial solution is to change locally the catcode of _.

% Source : http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511

\documentclass{article}
\makeatletter
\newcommand\breakabletexttt[1]{\texttt{\breakable@texttt#1\@nil}}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
%
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother

\begin{document}

\catcode_=11
\breakabletexttt{33b7a2f7c4cc93c46dd4ee2ed81aa1eb?9409135542c79d1ed50c9fde07fa600a?cce5a2fe76bfbd0c48d79fb43a7106f0?263e9a8711c1400fb2a716a1b820ac9a}
\catcode_=8

\end{document}


Finally, here is one possible solution :

% Sources :
%   * http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511
%   * http://tex.stackexchange.com/questions/33465/changing-the-catcode-of-in-one-command

\documentclass{article}
\makeatletter
\newcommand\breakabletexttt{\begingroup\catcode\_12 \breakabletexttt@i}
\newcommand\breakabletexttt@i[1]{\texttt{\breakable@texttt#1\@nil}\endgroup}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother

\begin{document}

\breakabletexttt{33b7a2f7c4cc93c46dd4ee2ed81aa1eb?9409135542c79d1ed50c9fde07fa600a?cce5a2fe76bfbd0c48d79fb43a7106f0?263e9a8711c1400fb2a716a1b820ac9a}

\end{document}

-
It chokes on _. – egreg Nov 2 '11 at 15:29
I've just update my message. Thanks ! – projetmbc Nov 2 '11 at 15:32
I've given one partial solution. – projetmbc Nov 2 '11 at 16:00

I tried the solution in the update to the question but that didn't work when I tried it in a \lstset (it didn't compile). Afer a little searching, I came up with this and it seems to do the trick:

\lstset{various options,
breaklines=true,
literate={\-}{}{0\discretionary{-}{}{-}},
various other options}
`
-