Line break in texttt

I would like latex to break long text inside \texttt{} that does not contain any whitespaces. Currently the text just runs off the page.

The latex document is generated from a reStructureText document (I have to use reStructuredText). Therefore I prefer not to use these solutions:

• replacing \texttt{} by \path{} or \url{} (unless someone could tell me how to have Docutils do that)
• inserting \allowbreak{} / soft hyphens (feasable but it destroys the readability of the reStructuredText document)

Is there some global configuration in the latex document that would cause breaking of texttt strings? Is there some other good way to get a printable document that shows all the text?

Here a minimal example for my problem:

\documentclass[a4paper]{article}
\begin{document}
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.
\end{document}


When I try to create a pdf (using "pdflatex a.tex") then only "TranformerEx" is visible of "TranformerException".

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Dec 25, 2014 at 14:30
• Though the accepted answer of tex.stackexchange.com/questions/299/… is not what you want (since it uses \path), you can find alternative answers there which might help you (especially the one by Stefan Kottwitz). Dec 25, 2014 at 15:18
• @cryingshadow as you already had a solution posted, you might consider that, since the OP's concern lies with the body of the document, you might suggest, after loading the url package, the following \let\oldtexttt\texttt\let\texttt\path as a possible work around which can be made from the preamble. Dec 25, 2014 at 15:24
• possible duplicate of How to automatically hyphenate within \texttt? Dec 25, 2014 at 16:16
• @PeterGrill Is this really a question about hyphenation? I like the link you're pointing to, but it seems that for what the OP is asking a bit more needs to be done than to automate hyphenation. It would seem that taking the linked solution and showing how to adapt it to this situation could be helpful. Dec 25, 2014 at 17:17

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
\end{document}


• This completely baffles and amazes me. Seems @egreg didn't explain how or why this works.... do you or he care to elaborate? Dec 26, 2014 at 18:14
• This seems to work for "regular" characters, but how about breaking on underscores? Adding either _ or _ doesn't seem to work. Jan 30, 2019 at 13:16
• @lemdan Please see the SUPPLEMENT in my edited answer. Apr 13, 2020 at 0:40
• @JoeyDumont Sorry it took so long, but I have addressed your question. Apr 13, 2020 at 0:43
• @lemdan I have removed the backslashes and noted that \_ are no longer needed in \texttt...one can directly use underscores. Apr 13, 2020 at 1:04

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.

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.

• Mmmhh… In normal text, you don’t want periods and slashes to behave as discretionary breaks. Allowing (ordinary) hyphenation is good in normal text, @egreg’s solution is good for filenames, but these two approaches should not be mixed.
– GuM
Apr 25, 2018 at 1:44
• See also here and here for some related questions. Apr 25, 2018 at 2:06

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.

• This is very helpful. It resolves 90% of my problems. It still fails for example at \$GLOBALS['TCA']['tt_address']['feInterface']['fe_admin_fieldList'] or chains separated by -> or file paths separated by /. To me it is much more important that no text is hidden than where exactly it is broken. Dec 25, 2014 at 17:36
• @Ferdinand Then I think loading url package and going with my suggestion in the comments above about \let\texttt\path in the preamble might work best for you. Dec 25, 2014 at 17:56

This is a late reply, but I posted a question with an answer here, here it is again, verbatim:

Use \seqsplit.

\documentclass[9pt,oneside]{amsart}

\usepackage{seqsplit}

\begin{document}

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

\end{document}


• seqsplit doesn't provide a font like \texttt
– bmv
Jan 26, 2018 at 14:52
• @bmv It should be noted that \seqsplit does the job if it is wrapped in \texttt: \texttt{\seqsplit{javax.xml.transform.TransformerException}} will work and display as expected. Dec 4, 2020 at 22:25
• This is the best answer by far and should be selected as the answer for this question. I had to scroll to the bottom to find it... Mar 5, 2021 at 19:01