1

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!
James Ray
  • 261

3 Answers3

2

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 } } }
   \ray_aepath_addbreak:n
 }
\cs_generate_variant:Nn \tl_map_function:nN { f }
\cs_new:Nn \ray_aepath_addbreak:n
 {
  \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},
\end{minipage}\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\aepath{0xcf416c5},
\end{minipage}\qquad\qquad
\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}},
\end{minipage}\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\end{minipage}\qquad\qquad
\begin{minipage}[t]{0pt}\hfuzz=\maxdimen % no overfull
\texttt{\seqsplit{0xcf416c5}},
\end{minipage}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 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
  • 1
    @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
  • this looks more complicated. Why would I do it this way when the implementation of seqsplit is very simple and does the job? – James Ray Jan 26 '18 at 15:45
  • @JamesRay For instance because seqsplit doesn't allow to choose a starting point for the allowed breaks. – egreg Jan 26 '18 at 16:07
  • OK, I don't mind that. I just want the line to break when it gets to the margin, and so it appears justified or formatted the same as the other text. – James Ray Jan 26 '18 at 16:17
1

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}
bmv
  • 3,588
  • 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
-1

Use \seqsplit.

\documentclass[9pt,oneside]{amsart}

\usepackage{seqsplit}

\begin{document}

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

\end{document}

Using seqsplit to split long text.

James Ray
  • 261