Line break in texttt

Stealing egreg's answer at How to emulate \url hyphenating without using the url package?, wherein he redefined \url, I start with that same redefinition to instead redefine \texttt instead of \url. In egreg's original incarnation, it allowed linebreaks at . and / characters.

But other breakpoint symbols can be added, for example, below I also make [ a breakpoint:

\documentclass[a4paper]{article}
\renewcommand{\texttt}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active
  \scantokens{#1\noexpand}%
  \endgroup
}
\usepackage{lipsum}
\begin{document}
\lipsum[4]
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

If an unrecoverable error occurs during the transformation, then a
\texttt{\$GLOBALS['TCA']['tt\_address']['feInterface']['fe\_admin\_fieldList']}
\end{document}

enter image description here

If you need to use this solution in section or other headings, or other places written to and read back from .aux files, you will need to replace \renewcommand with \DeclareRobustCommand - see this answer for more info: Combining Wrapping \texttt{} with Sections and TOC - Improper alphabetic constant


SUPPLEMENT

Lemdan asks (and Joey asked a long time ago) in a comment how to do this to make the underscore _ a breakpoint? Because the underscore is usually catcode 8, one has to absorb the argument to \texttt using instead the catcode of _ set to 12. Of course, this will prevent the use of subscripts inside of math inside of a \texttt, but that should not be too difficult to live with. This will also remove the need to escape underscores in \texttt.

\documentclass[a4paper]{article}

\catcode`_=12 %
\renewcommand{\texttt}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \begingroup\lccode`~=`_\lowercase{\endgroup\def~}{_\discretionary{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active\catcode`_=\active
  \scantokens{#1\noexpand}%
  \endgroup
}
\catcode`_=8 % 

\usepackage{lipsum}
\begin{document}
\lipsum[4]
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

If an unrecoverable error occurs during the transformation, then a
\texttt{\$GLOBALS_'TCA']['tt_address']['feInterface']['fe_admin_fieldList']}
\end{document}

enter image description here


I'm not sure how much you're willing to do from the preamble, but here's a suggestion:

\documentclass{article}

\begingroup
  \catcode`\.=\active
  \gdef.{\normalperiod\allowbreak}%
\endgroup

\newcommand\aepath[1]{%%
  \bgroup 
    \let\normalperiod=.%%
    \catcode`\.=\active
    \everyeof{\noexpand}%%
    \endlinechar=-1%%
    \ttfamily\scantokens{#1}%%
  \egroup}

\let\oldtexttt\texttt
\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

\end{document}

From your MWE, I assumed that you want your code to be breakable at points where a . occurs. Assuming that all situations where you might run into longish text involves such . separated names and that you do not need \texttt for any other purposes in your document, this might be a solution for you.

In case you might be concerned that another package might desire . to be active but defined differently, you could take the following approach:

\documentclass{article}

\begingroup
  \catcode`\.=\active
  \gdef\redefineperiod{\def.{\normalperiod\allowbreak}}%%
\endgroup

\newcommand\aepath[1]{%%
  \bgroup 
    \let\normalperiod=.%%
    \catcode`\.=\active
    \redefineperiod
    \everyeof{\noexpand}%%
    \endlinechar=-1%%
    \ttfamily\scantokens{#1}%%
  \egroup}

\let\oldtexttt\texttt
\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.  Followed
by more text which is just to fill to the ned of the line.

\end{document}

Naively (ie: this is what I initially thought I could do), you might try

\documentclass{article}

\newcommand\aepath[1]{%%
  \bgroup 
    \let\normalperiod=.%%
    \catcode`\.=\active
    \def.{\normalperiod\allowbreak}%%
    \everyeof{\noexpand}%%
    \endlinechar=-1%%
    \ttfamily\scantokens{#1}%%
  \egroup}

\let\oldtexttt\texttt
\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.  Followed
by more text which is just to fill to the ned of the line.

\end{document}

But this will fail because the period following \def has already been tokenized an is not active. So, LaTeX will throw an error about a missing control sequence.

UPDATE

If you're not particularly concerned about where the breaks occur then you can use something like:

\documentclass{article}

\makeatletter
\newcommand\aepath[1]{%%
  \bgroup
    \ttfamily
    \ae@path#1\relax\@nil
  \egroup}
\def\ae@path#1#2\@nil{%%
  \def\ae@continue{}%%
  \detokenize{#1}\unskip\penalty\z@  
  \ifx\relax#2%%
  \else 
    \def\ae@continue{\ae@path#2\@nil}%%
  \fi
  \ae@continue}
\makeatother

\let\texttt\aepath

\begin{document}

If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.

\end{document}

though this seems a bit suboptimal to me. I think it would be better to decide where breakpoints should be and follow the example with . to make those characters (such as /, -, etc) into active characters within the context of the command and smuggle in the penalty to allow a break following them.


Following from Steven's answer, in my document I was using \texttt in Figures to show paths and urls but also I was using \texttt in my text to highlight small pieces of code.

While the code Steven's provided works with my figures it still overflows the margins when writing \texttt commands for normal text that doesn't use symbols. Like the below image shows, \texttt works just fine for figures but for normal text does not work well.

enter image description here

To fix this, I have borrowed the code for \justify from this tex.se post and just added that to existing code.

\newcommand*\justify{%
  \fontdimen2\font=0.4em% interword space
  \fontdimen3\font=0.2em% interword stretch
  \fontdimen4\font=0.1em% interword shrink
  \fontdimen7\font=0.1em% extra space
  \hyphenchar\font=`\-% allowing hyphenation
}

\renewcommand{\texttt}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active
  \justify\scantokens{#1\noexpand}%
  \endgroup
}

This produces a much better output, and works for my case where I am using typewriter in different places.

enter image description here