Full justification with typewriter font

In a typewriter font aka monospaced font each character is given the same width. Monospaced fonts are frequently used by programmers to increase the readability of source code, but long text passages with monospace typeface are considerably less readable than those with variable-width fonts.

The space between words is fixed too, that prevents justification, and hyphenation may be disabled too. That’s useful for presenting source code, but sometimes a typewriter font is wanted but justification is required. This post will give some information how to fulfill that. We will use the Computer Modern Typewriter font (cmtt) you get by default when using \ttdefault, \ttfamily, \texttt.

The space between words is 1em, the same is valid for the extra space following the end of a sentence. Zero stretch and shrink means the space between the words will always be 1em. The hyphenchar is set to -1, that’s why hyphenation is disabled. Let’s look how a normal text is set when typewriter is used:

To get justification I just modify some of the font parameters above, that the spaces may be stretched and shrinked. I use the everysel package to ensure that my changes are applied every time the font is selected. Further I set the \hyphenchar to the – symbol.

Comments (10)

Is it possible to set monospaced text accross a vertical grid? I mean, achieving a faithful typewriter effect, in which all horizontal spaces are integer multiples of 1em making all letters fit vertically?

My issue is that I like to change fonts only for selected words or phrases within a paragraph. These monospaced words often hang off the end of my otherwise fully justified text. Do you know a solution for this issue?

take care of proper hyphenation. It may be that those words aren’t hyphenated because of the way they are written. Justification can still happen with real monospaced words in a line because the remaining line can be adjusted.