How to get long \texttt sections to break

If you are using the default LaTeX fonts (or a small number of others), you can enable hyphenation within \texttt using the hyphenat package:

\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[htt]{hyphenat}
\begin{document}
\texttt{\lipsum[1]}
\end{document}

Note that this still won't do a great job of formatting arbitrary strings because it will be relying on the hyphenation patterns in use.

If your problem is about typesetting paths specifically, another option (arguably better) is to 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}

This can have limitations with non-ASCII characters, however. If you get really stuck, you can force a line to end using \newline (aka \\) or \linebreak. These differ in important ways: \newline will cause the paragraph to end the line abruptly at that point, whereas \linebreak will still attempt to fill the line completely for justification purposes. This is illustrated in the following example:

\documentclass[twocolumn]{article}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabitur dictum grav$\bullet$\newline ida mauris. 
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
\bigskip

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabitur dictum grav$\bullet$\linebreak ida mauris. 
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
\end{document}

enter image description here


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}

alt text

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


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.