[Tex/LaTex] wordbreaking, justification and ttfamily

typewriter

Edit

I think this differes slightly from the linked "duplicate" as here are paragraphs formatted as typewriter, using \ttfamily while in the duplicate there are

a lot of "common words" in typewriter font sprinkled around his text.

The solution is of course similar, but "Heiko Oberdiek", marked answer here, gave a nice working example and explanation regarding this problem. Personally I had problems understanding what parts of the suggested codes in the "duplicate" question were necessary to solve my issue.

Original Question

I recently have some bigger texts I want to put into \ttfamily.

However, I noticed wordbreaking isn't really working and did some research to add these lines:

\setlength{\emergencystretch}{2em}
\newcommand{\origttfamily}{}%
\let\origttfamily=\ttfamily%
\renewcommand{\ttfamily}{\origttfamily \hyphenchar\font=`\-}

Also using english option in a memoir-class and babel-package.

This is somewhat helping as there is some wordbreaking and space-stretching happening now, but in the end, most paragraphs are looking more like left-aligned than justified. and there are still some lines going over the defined borders.

Here is a minimal example with 2 longer text-passages, one in plain and one in ttfamily.

\documentclass[english,11pt,oneside,article,a5]{memoir}
\usepackage[english]{babel}
\usepackage{geometry} 

\setlength{\emergencystretch}{2em}
\newcommand{\origttfamily}{}%
\let\origttfamily=\ttfamily %
\renewcommand{\ttfamily}{\origttfamily \hyphenchar\font=`\-}

\geometry{left=20mm,right=20mm, top=20mm, bottom=50mm}
\OnehalfSpacing
\leftskip3em\rightskip2em

\begin{document}
$>$She didn’t want to spend the time trying to locate them, or try to thin out the operator’s patience for Alison doing whatever. She expects they are already rather sour at her burrowing a tunnel through most of the building’s floors to make that railgun using an enormous amount of jetalium. If she gets a chance later, though, to come back to this place, she’ll put rescuing Loviro on the table. 
\\{}\ttfamily According to the engineer, they are most likely tethers to launch things cheaply into space, given how high they go. Although, Alison does come by close enough to see one to see a revolving restaurant at the top of it, so tossing stuff into space may not be its only function.
\end{document}

Did I to something wrong in using ttfamily like this?

Best Answer

The issue is not the hyphenation here, because TeX does not find a good place to hyphenate a word. The problem is rather that the interword space does not have flexibility. This can be added via font parameters. Alternatively \spaceskip and \xspaceskip can be set.

\documentclass[english,11pt,oneside,article]{memoir}
\usepackage[english]{babel}
\usepackage{geometry}
\setlength{\emergencystretch}{2em}

\usepackage{letltxmacro}
\LetLtxMacro\origttfamily\ttfamily
\DeclareRobustCommand*{\ttfamily}{%
  \origttfamily
  \hyphenchar\font=`\-\relax
  \fontdimen3\font=.25em\relax
  \fontdimen4\font=.167em\relax
  \fontdimen7\font=.167em\relax
}

\makeatletter
\DeclareRobustCommand\vttfamily{%
  \not@math@alphabet\vttfamily\relax
  \fontfamily{cmvtt}% cmvtt (Computer Modern) or lmvtt (Latin Modern)
  \selectfont
}
\DeclareTextFontCommand{\textvtt}{\vttfamily}
\makeatother

\geometry{left=20mm,right=20mm, top=20mm, bottom=50mm}
\OnehalfSpacing
\leftskip3em\rightskip2em

\begin{document}
$>$She didn’t want to spend the time trying to locate them, or try to thin
out the operator’s patience for Alison doing whatever. She expects they are
already rather sour at her burrowing a tunnel through most of the building’s
floors to make that railgun using an enormous amount of jetalium. If she gets
a chance later, though, to come back to this place, she’ll put rescuing
Loviro on the table.
\\{}\ttfamily According to the engineer, they are most likely tethers to
launch things cheaply into space, given how high they go. Although, Alison
does come by close enough to see one to see a revolving restaurant at the
top of it, so tossing stuff into space may not be its only function.
\\{}\vttfamily According to the engineer, they are most likely tethers to
launch things cheaply into space, given how high they go. Although, Alison
does come by close enough to see one to see a revolving restaurant at the
top of it, so tossing stuff into space may not be its only function.
\end{document}

Result

Remarks:

  • For comparison: the third part is set by a variable width typewriter font.

  • As explained in How to automatically hyphenate within \texttt?, the setting of font dimens is global. But this might not be an issue here, because \ttfamily is redefined anyway.

  • Package letltxmacro is used, because saving macros that are defined via \DeclareRobustCommand are trickier.

Related Question