# Hyphenate typewriter / avoid overfull \hboxes in code, using fontspec and XeTeX

I would like to avoid overfull \hboxes as a result of inline code, inserted with \textt{foo} or \ttfamily, e.g. in this MWE:

% compile with xelatex
\documentclass{article}
\usepackage[textwidth=4cm, showframe]{geometry}% to force hyphenation
\usepackage{polyglossia}
\setdefaultlanguage[variant=usmax]{english}
\usepackage{fontspec}
\setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono}
\setlength{\overfullrule}{1mm}
\begin{document}
{\ttfamily Bla \textbackslash newcommand\{\textbackslash foo\}\{bar\}
\textbackslash setmonofont[Scale=MatchLowercase]\{DejaVu Sans Mono\}

For regular words, it works supercalifragilisticexpialidociously (or more like kind of ok),
but real code is tricky.}
\end{document}


I'd like to have hyphenation points (manual instruction is ok) between Match and Lowercase, and between {\foo} and {bar}. However, I don't ever want a hyphen to appear at hyphenation points (in typewriter text).

I've tried \-, which doesn't work (babelshorthands is not available for english). I also know about the fontspec option HyphenChar, but I don't know how to tell it "no hyphenation character, but hyphentation" (space / 32 is not an option because it appears in the OCR layer of the pdf; None would disable hyphenation altogether). I guess these might both be traces of the solution, but I can't figure out how to make use of them.

-
P.S.: Compiling my MWE (minus polyglossia) with LuaLaTeX results in surprisingly different output, especially the MatchLowercase part: i.stack.imgur.com/IGOHK.png (MiKTeX 2.9). Is that just a difference that's to be expected, or a bug on the side of LuaTeX/XeTeX/fontspec? I.e. would this be of interest to any of the development teams? –  doncherry Dec 7 '12 at 6:55

The first attempt uses a space character as hyphenation character, however I realized later that the OP explicitely had said it was a no-go. So there is a second attempt below which does not use the space (or other) character.

The following works with me (I commented out the DejaVu Sans Mono line as for some reason it does not work on my installation, tried to add ExternalLocation as option but this did not solve it and I don't use often enough XeTeX or LuaTeX to investigate further the reason of the problem)

% compile with xelatex
\documentclass{article}
\usepackage[textwidth=4cm, showframe]{geometry}% to force hyphenation
\usepackage{polyglossia}
\setdefaultlanguage[variant=usmax]{english}
\usepackage{fontspec}
%    \setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono}
\setlength{\overfullrule}{1mm}
\begin{document}
{\ttfamily\hyphenchar\font32
Bla \textbackslash newcommand\{\textbackslash foo\}\-\{bar\}
\textbackslash setmonofont[Scale=\-Match\-Lowercase]\{DejaVu Sans Mono\}

For regular words, it works supercalifragilisticexpialidociously (or more like
kind of ok), but real code is tricky.}
\end{document}


Note that the space is used as hyphen character. And I added a few \- to the code.

I read now your 'space is not an option' it probably disqualifies my attempt! So here is a second try:

% compile with xelatex
\documentclass{article}
\usepackage[textwidth=4cm, showframe]{geometry}% to force hyphenation
\usepackage{polyglossia}
\setdefaultlanguage[variant=usmax]{english}
\usepackage{fontspec}
%    \setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono}
\setlength{\overfullrule}{1mm}
\begin{document}
{\ttfamily\def\-{\discretionary{}{}{}}%
Bla \textbackslash newcommand\{\textbackslash foo\}\-\{bar\}
\textbackslash setmonofont[Scale=\-Match\-Lowercase]\{DejaVu Sans Mono\}

For regular words, it works supercalifra\-gilisticexpialidociously (or more like
kind of ok), but real code is tricky.}
\end{document}


But I had to add explicitely a breaking point in the text part.

-
Precisely :). I mean, if there's no better way, I'll do that, but I was hoping for a better way. Btw, \hyphenchar seems to be equivalent to fontspec's HyphenChar option. As for your trouble with DejaVu Mono: It might be Weird spacing with DejaVu Sans Mono with MiKTeX's XeLaTeX (the solution to which I still have to write up), or perhaps you don't have the font on your system at all. –  doncherry Dec 7 '12 at 7:22
@doncherry: I updated my answer! I might well not have the font on my system, but not using enough XeTeX I did not investigate the issue. –  jfbu Dec 7 '12 at 7:31
@doncherry: ok I overcame my laziness, and indeed on my mac os, there is no DejaVu among the system wide fonts, and I found no opentype version in the texlive tree either (but of course I already used it as type1 font). Apart from that, although my \discretionary{}{}{} seems to go towards the solution, I do not understand yet how to at the same time have the automatic hyphenation activated. –  jfbu Dec 7 '12 at 7:51
I don't understand the problem. My version of fontspec enables hyphenatation of typewriter text. So you only need a special \discretionary command for the code parts. (I wouldn't redefine \- but use some other command. german babel e.g. use the shorthand ""). –  Ulrike Fischer Dec 7 '12 at 9:51
@UlrikeFischer The problem seems to be how to have the TeX automatic hyphenation activated but without any hyphenation character at broken words at lines endings. –  jfbu Dec 7 '12 at 12:59