1

I tried copying the example from Example about hyphenation with ttfamily font and using the name ttfamily as \ttfamily{Some text} and \begin{ttfamily} & \end{ttfamily}.

However why the first line using the macro ttfamily is not hyphenated, but the second using the environment \begin{ttfamily} is hyphenated?

enter image description here

\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage{lmodern}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[main=english]{babel}
\usepackage[showframe,pass]{geometry}

%THIS IS THE IMPORTANT PART   % <=========================================
\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 % <=========================================================

\begin{document}

    \frenchspacing
    \ttfamily{Typewriter/teletype family - (encoding: T1, family: pcr, series: m,
    shape: n, size: 10.5, baseline: 11.0pt)}

    \bigskip

    \begin{ttfamily}
        Typewriter/teletype family - (encoding: T1, family: pcr, series: m,
        shape: n, size: 10.5, baseline: 11.0pt)
    \end{ttfamily}

\end{document}

On the question How can I use `\texttt{Some text}` with automatic hyphenation? we can see the same text without the tttext hyphenation, it causing Overfull \hbox:

enter image description here

test2.tex:182: Overfull \hbox (11.9992pt too wide) in paragraph at lines 182--183[]\T1/lmtt/m/n/10 Typewriter/teletype family - (encoding: T1, family:
test2.tex:182: Overfull \hbox (33.7491pt too wide) in paragraph at lines 182--183\T1/lmtt/m/n/10 pcr, series: m, shape: n, size: 10.5, baseline: 11.0pt)
  • 1
    First, proper group-limited syntax would be {\ttfamily Some text} and not \ttfamily{Some text}. That is, \ttfamily takes no argument (perhaps you are confusing it with \texttt{}). Not that that fixes your problem. – Steven B. Segletes Aug 22 '17 at 18:27
  • 1
    If I comment out your redefinition of \ttfamily, the two examples typeset identically. – Steven B. Segletes Aug 22 '17 at 18:31
  • If I comment out just the \fontdimen4\font=.167em\relax part of your redefinition, both paragraphs hyphenate. – Steven B. Segletes Aug 22 '17 at 18:34
  • @StevenB.Segletes it seems a solution to the problem. – user Aug 22 '17 at 18:35
4

With your definitions, doing

{\ttfamily text}

and

\begin{ttfamily}text\end{ttfamily}

is exactly the same. Indeed the output of the following examples is exactly the same.

\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage{lmodern}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[main=english]{babel}
\usepackage[showframe,pass]{geometry}

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

\begin{document}

\frenchspacing

{\ttfamily 
supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious}

\bigskip

\begin{ttfamily}
supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
\end{ttfamily}

\end{document}

enter image description here

Notes

  1. \ttfamily{...} is wrong, in the sense that the font will be in typewriter type also after }.

  2. The space after \begin{ttfamily} (the end-of-line, to be precise) is ignored because it is scanned when TeX is between paragraphs. It wouldn't be if \begin{ttfamily} is scanned in paragraph mode.

  3. Avoid using pseudoenvironments like these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.