# Linebreak in texttt, works with textttt in mathmode

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!

• (1) welcome, (2) please post full minimal examples, makes it a lot easier to help when there is code that can be copied and tested as is. – daleif Jan 26 '18 at 12:38
• What's the reason for overloading \texttt? I see none. – egreg Jan 26 '18 at 12:51
• I will try using ctan.org/pkg/seqsplit. – James Ray Jan 26 '18 at 14:09

I'm not sure why you want to overload \texttt, but the following works.

\documentclass[9pt,oneside]{amsart}
\usepackage{letltxmacro}

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

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

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

\begin{document}

filler text $\texttt{KEC}$

This is more filler text,  \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},

\end{document}


Here's how you can automatically split long sequences like that. I add also the same with \seqsplit. With the new \aepath macro you can decide after how many characters breaks are possible, which the implementation of \seqsplit doesn't allow.

\documentclass[9pt,oneside]{amsart}
\usepackage{xparse}
\usepackage{seqsplit}

\ExplSyntaxOn
\NewDocumentCommand\aepath{O{4}m}
{
\texttt{ \ray_aepath:nn { #1 } { #2 } }
}

\cs_new:Nn \ray_aepath:nn
{
\tl_range:nnn { #2 } { 1 } { #1 }
\tl_map_function:fN
{ \tl_range:nnn { #2 } { #1+1 } { \tl_count:n { #2 } } }
}
\cs_generate_variant:Nn \tl_map_function:nN { f }
{
\hspace{0pt plus 0.2pt} #1
}
\ExplSyntaxOff

\begin{document}

This is more filler text,
\aepath{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},

\begin{minipage}[t]{4.5cm}
This is more filler text,
\aepath{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\aepath{0xcf416c5},
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\aepath[1]{0xcf416c5},
\end{minipage}

\bigskip\bigskip

\begin{minipage}[t]{4.5cm}
This is more filler text,
\texttt{\seqsplit{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}},
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\end{minipage}

\end{document}


• Thanks, that passes the build, but I also need to automatically break \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba} over a line. – James Ray Jan 26 '18 at 13:10
• Any way, to add a manual break I can just use \\, e.g. \texttt{0xcf416c536ec1a19ed1fb89e\\4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}. That's not ideal as I wanted something that would use an automatic break, but nevertheless it's better than nothing. – James Ray Jan 26 '18 at 13:43
• @JamesRay You're asking quite a different question, then. – egreg Jan 26 '18 at 14:30
• I guess so. The answers that I linked to didn't work, as they caused errors, then after your fix they still didn't work. But \seqsplit does, and doesn't clash with existing \texttts in math-mode. – James Ray Jan 26 '18 at 14:33
• @JamesRay I added a different way from seqsplit. – egreg Jan 26 '18 at 14:42

You can use \mathtt inside math mode:

\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 $\mathtt{KEC}$

This is more filler text,  \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba},

\end{document}

• That doesn't seem like a good solution. Using \texttt in math-mode or not seems to work fine, apart from it failing now. – James Ray Jan 26 '18 at 13:18

Use \seqsplit.

\documentclass[9pt,oneside]{amsart}

\usepackage{seqsplit}

\begin{document}

Filler text. Filler text. Filler text. Filler text. \seqsplit{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}

\end{document}