1

I read this answer, and this one about how to hyphenate \texttt text and handling special chars. However, it is not really doing what I intend.

\documentclass{scrreprt}
\usepackage{classicthesis,lipsum} % classicthesis to make it closer to my actual document

\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=23}
\lccode`\:`\:

\begin{document}
This is a meaningless line of text \texttt{and:this:is:meaninglessly:typewritten:text}.
This line is equally meaningless, but also also also also also also contains a \texttt{somewhatlongishwordwhichshouldbehyphenated}
\end{document}

produces

enter image description here

So apparently, \font=23 is not the codepoint of the character mentioned in this answer. So I figured I could simply \usepackage[T1]{fontenc}, but this un-typewrites the text:

\documentclass{scrreprt}
\usepackage{classicthesis,lipsum}
\usepackage[T1]{fontenc}

\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=23}
\lccode`\:`\:

\begin{document}
This is a meaningless line of text \texttt{and:this:is:meaninglessly:typewritten:text}.
This line is equally meaningless, but also also also also also also also also contains a \texttt{somewhatlongishwordwhichshouldbehyphenated}
\end{document}

This "hyphenates" correctly, but the font is wrong

enter image description here

How can I get both T1 font encoding with hyphenation and typewritten text?

oarfish
  • 1,419

1 Answers1

3

The character 23 in the T1 encoded fonts is the invisible compound word mark; there's no such character in OT1 encoded fonts, so you really need T1.

However, declaring a font family is not sufficient and indeed you get

LaTeX Font Warning: Font shape `T1/cmtt/m/n' undefined
(Font)              using `T1/cmr/m/n' instead on input line 8.

which explains why the font is not monospaced. If you declare explicitly the font family, you need also to supply the whole font definitions, unless the font description file has already been loaded.

\documentclass{scrreprt}
\usepackage{classicthesis,lipsum}
\usepackage[T1]{fontenc}

\lccode`\:=`\:

\AtBeginDocument{%
  % load the .fd file for \ttfamily
  \sbox0{\fontsize{10}{12}%
  % set the hyphenchar for the current font
  \ttfamily\hyphenchar\font=23 }%
  % set the hyphenchar for every other font in the family
  \DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=23 }%
}

\begin{document}

This is a meaningless line of text \texttt{and:this:is:meaninglessly:typewritten:text}.
This line is equally meaningless, but also also also also also also also also contains 
a \texttt{somewhatlongishwordwhichshouldbehyphenated}

\end{document}

enter image description here

egreg
  • 1,121,712