# 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