4

I'm trying to do this:

\documentclass{article}
\begin{document}
\begin{minipage}{2in}
\ttfamily
Hello/Hello/Hello/Hello/Hello/Hello
\end{minipage}
\end{document}

The text is not being hyphenated (because LaTeX doesn't hyphenate monospace fonts) and I'm getting "Overfull \hbox". The only solution I've found is to redefine the forward-slash to "forward-slash with \-." How can I do this?

yegor256
  • 12,021

2 Answers2

9

Two comments up front: (a) I will assume that you're not really interested in hyphenating the word "hello" per se, the more so as standard English-language hyphenation practice frowns on hyphenating hello as hel-lo. (ii) You mention hyphenation several times, but I think that what you are really looking to get is better line-breaking, especially by avoiding overfull lines. (What's the difference: line-breaking doesn't necessarily involve sticking hyphenation characters at the ends of lines.)

If (big if!) these assumptions are valid, I think you can proceed along two ways:

  • Replace instances of / with \slash. This will allow LaTeX to insert line breaks after \slash. This'll avoid creating overfull lines, but it may well result in underfull lines.

  • Encase the strings in question in \seqsplit or \url directives.

The horizontal line in the following screenshot is there to illustrate the width of the minipage.

enter image description here

\documentclass{article}
\usepackage{seqsplit} % for '\seqsplit` macro
\usepackage{xurl}     % for '\path' macro
\begin{document}

\begin{minipage}[t]{2in} % just to illustrate width of minipage \hrule \phantom{.} \end{minipage}

\begin{minipage}[t]{2in} \ttfamily Hello/Hello/Hello/Hello/Hello/Hello \end{minipage}\hspace{0.6in}--- bad!

\medskip \begin{minipage}[t]{2in} \ttfamily Hello/-Hello/-Hello/-Hello/-Hello/-Hello \end{minipage}\quad--- hyphen char.\ looks odd

\medskip \begin{minipage}[t]{2in} \ttfamily Hello\slash Hello\slash Hello\slash Hello\slash Hello\slash Hello \end{minipage}\quad--- better

\medskip \begin{minipage}[t]{2in} \ttfamily \seqsplit{Hello/Hello/Hello/Hello/Hello/Hello} \end{minipage}\quad--- with \verb+\seqsplit+

\medskip \begin{minipage}[t]{2in} \ttfamily \path{Hello/Hello/Hello/Hello/Hello/Hello} \end{minipage}\quad--- with \verb+\path+

\end{document}

Mico
  • 506,678
3

I don't think that's quite what you want unless you want to have output of:

Hello/Hello/Hello/-
Hello/Hello/Hello

Assuming you actually just want to allow breaks at the slashes, you could instead set the \hyphenchar for the font to /. There are two gotchas: \hyphenchar is global. If you set it in a group, it will still apply outside of the group and you could also inadvertently have something typeset like:

Something ridicu/
lously absurdly
prolix

The second problem is the easier one to solve and is built into LaTeX's kernel. We can use \language\l@nohyphenation to turn off all hyphenation.¹

The first problem is a bit trickier. What we'll do is save the current hyphen character and use \aftergroup to restore it when the current group ends. Putting all of this together to give us our specialized version of \ttfamily, we can do:

 \makeatletter % ❶
 \NewDocumentCommand{\mytt}{}{% ❷
    \ttfamily
    \language\l@nohyphenation
    \xdef\@restorehypenchar{% ❸
       \hyphenchar\the\font\the\hyphenchar\font
    }%
    \aftergroup\@restorehyphenchar
    \hyphenchar\font`/\relax
 }
 \makeatother

Note that we need access to private LaTeX commands ❶. And if you're stuck using an outdated LaTeX you can replace the line labeled ❷ with:

\newcommand{\mytt}{

We use \xdef at ❸ because we need to expand the contents of the definition (the resulting definition will be something akin to \hyphenchar \OT1/cmtt/m/n/10 -1) and it must be global since it's going to be expanded after the current group is completed.

You can then use \mytt in place of the \ttfamily in your example.

This is a little fragile. If you do something like

 {\mytt {\mytt foo}}

you won't get the original hyphenchar for typewriter type back when you exit since the inner \mytt will overwrite the outer \mytt's definition of \@restorehyphenchar.


  1. This, in fact, is how hyphenation is suppressed in verbatim mode.
Don Hosek
  • 14,078