I want to have automatic line breaks for \texttt (specifically for my case in text-mode), while not causing build errors when \texttt occurs in math-mode. Adding hyphens or using \path or \url isn't pretty, as it would be good to be display the text 0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba without any hyphens or other characters that will break it, as well as without URL borders, plus it would be good to be able to double click the text to copy and paste it.
MWE:
\documentclass[9pt,oneside]{amsart}
\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}
filler text $\texttt{KEC}$
This is more filler text. This is more filler text. This is more filler text, \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\end{document}
I tried the solutions in Line break in texttt. If I try either of the answers there, I get the following error. With the above MWE taken from https://tex.stackexchange.com/a/219454/143781, I get:
! LaTeX Error: Command \ttfamily invalid in math mode.
...
l.23 filler text $\texttt{KEC}
$^^M
! ==> Fatal error occurred, no output PDF file produced!


\texttt? I see none. – egreg Jan 26 '18 at 12:51