How to automatically hyphenate within \texttt?

In LaTeX it is standard behavior that typewriter fonts do not do any hyphenation because it is typically used for code. Thus, the fonts used normally for \texttt all suppress hyphenation automatically.

To change this, there are essentially three options:

  • enable hyphenation for the fonts used by \texttt throughout the document
  • define your own variant of \texttt that enables hyphenation inside
  • use different a font that has hyphenation enabled automatically

Generally enabling hyphenation on \texttt

The hyphenation status is normally tied to the font family. For example, the default font used for typewriter in LaTeX is "Computer Modern Typewriter" (i.e., cmtt) and its font family definition is

\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=-1}

for the OT1 font encoding. The "-1" means do not use a hyphenation character. A value between 0 and 255 would mean use the character in that particular slot as a hyphenation character.

Once all fonts have been set up one can alter the font family declaration for the typewriter font in a generic way (thanks to @greg) as follows:

\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}

This declaration sets the \hyphencharto the slot used by "-" in the current font for the default typewriter font family (\ttdefault) in the default encoding (\encodingdefault).

An explicit setting for a single family in one encoding would be:

\DeclareFontFamily{T1}{cmtt}{\hyphenchar \font=45}

Instead of 45 (which is the position of "-" in OT1 or T1 encoding) there is also the posibility to select an alternate hyphenation character if T1 encoded fonts are used. This alternate hyphenation character is in slot 127 in this encoding.

Defining your own tt command with hyphenation enabled

If one wishes to have both possibility with and without hyphenation that it is possible to define a separate command, for example

\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}

The trick is to not only set the fontfamily but also enable the \hyphenchar for the current font. Please note the \relax after the 45. It is necessary to make sure that TeX stops scanning for a number (a blank would have worked too). Otherwise your argument #1 could become part of the number if it happened to start with a digit.

Unfortunately there is an issue with this approach: assignments to font parameters are always global, so the above definition would also change \texttt's behavior as well (thanks to @mhp for pointing this out). This could be fixed with a similar \DeclareTextFontCommand with a setting of -1 for the hyphen. However, there may also be \ttfamily commands and they would also be affected. So a fully working solution unfortunately requires to also update that command.

\makeatletter
\DeclareRobustCommand\ttfamily
        {\not@math@alphabet\ttfamily\mathtt
         \fontfamily\ttdefault\selectfont\hyphenchar\font=-1\relax}
\makeatother
\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}

Use a typewriter font with hyphenation already enabled

Within Computer Modern there is not only Computer Modern Typewriter but also another typewriter font which looks similar but has variable width characters. That font has hyphenation automatically enabled and since it is using different widths for different characters it may be a good choice in a document using a lot of typewriter text. So here one could define a command like

\newcommand\textvtt[1]{{\normalfont\fontfamily{cmvtt}\selectfont #1}}

The hyphenat-package works fine.

\usepackage[htt]{hyphenat}