# How to get long \texttt sections to break

I often format path names with \texttt which can be very long. Usually Tex will not break this text. The line will just create a hbox overflow. It looks like this:

................
...C:\documents and settings
................
................
................
................
................
................
................


How do I get Tex to create a line break?

-
I have the same problem. Apparently using the geometry package or the fullpage package causes \texttt{} command to fail in cutting lines... Hope it will help – user6818 Jul 21 '11 at 15:40

Load the url package and use its \path{...} command:

\documentclass[twocolumn]{article}
\usepackage{url}
\begin{document}
Here is a long path: \path{/usr/local/texlive/2010/texmf-dist/tex/latex/biblatex/biblatex.sty}
\end{document}

-
Just a note: \path apparently breaks on forward slash /, but not on 'minus' (i.e. suggested/soft hyphens '\-' will be output verbatim, with the backslash included, and it will break on the backslash) - try for instance \path{A\-VERYLONGWORDENTRYISBEINGWRITTENHERE}. – sdaau Oct 7 '11 at 12:15

Hyphenation and full justification is possible with typewriter text as well. Here's a command \justify for this purpose, shown with the example above:

\documentclass{minimal}

\usepackage{lipsum}

\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
}

\begin{document}
\texttt{\justify\lipsum[1]}
\end{document}


Further the everysel package might be useful, as shown in Full justification with typewriter font.

-
Thanks, @Stefan Kottwitz - Just wanted to note that \justify also seems to work (break) \texttt{\justify with only a few words} in a paragraph of otherwise different font. – sdaau Oct 7 '11 at 12:23

Actually, \texttt does break the text flow, as evidenced from the following test:

\documentclass{minimal}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
\texttt{\lipsum[1]}
\end{document}


The problem is that \texttt doesn’t break the text correctly, because apparently it computes the line width incorrectly.

The easiest solution is to introduce soft hyphens at appropriate places:

\texttt{C:\doc\-uments and set\-tings}


This will use the hyphens as hints for possible word separators, and hyphenate the words accordingly. If that doesn’t help, a manual line break helps.

Though quite unsatisfactory, another advice is actually to reformulate the offending sentence so that it flows more nicely.

-
"because apparently it computes the line width incorrectly" — not true: TeX just doesn't have built-in rules for hyphenating URLs and paths (it's used to words). I suspect the example in the question is a slightly poor way to describe the problem since there a linebreak would have been found where the space is. – Will Robertson Jul 30 '10 at 16:40
@Will: try setting my MWE … you won’t get a properly formatted paragraph for the tt-ed text, even though it just contains normal text, so this isn’t due to missing hyphenation rules for paths. – Konrad Rudolph Jul 30 '10 at 17:09
Oh, I forgot to mention :-) hyphenation is disabled inside tt text. – Will Robertson Jul 31 '10 at 0:43
is there any way to enable it then? – Frederick Nord May 5 '14 at 12:40
@WillRobertson I would REALLY like to have an answer to Frederick Nord's question. (However, I think I just found it here tex.stackexchange.com/a/10378/13552) – macmadness86 Feb 11 at 17:48

I have an answer that uses a borrowed replace function from here: http://tex.stackexchange.com/a/213952/78981

I had a document where all the \texttt blocks were paths - at least the long ones. I used the following:

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\replace}{mmm}
{
\marian_replace:nnn {#1} {#2} {#3}
}

\tl_new:N \l_marian_input_text_tl

\cs_new_protected:Npn \marian_replace:nnn #1 #2 #3
{
\tl_set:Nn \l_marian_input_text_tl { #1 }
\tl_replace_all:Nnn \l_marian_input_text_tl { #2 } { #3 }
\tl_use:N \l_marian_input_text_tl
}
\ExplSyntaxOff

\usepackage{url}
\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{\OldTexttt{\replace{#1}{/}{/\allowbreak}}}


The code makes all the \texttt commands call the snippet above which replaces every / with /\allowablebreak. This is very similar to the \url` command, but I went this route because it works better with Pandoc.

-