# Hyphenation not working properly when text is prefixed with \textbackslash

First question: what causes text, when prefixed with \textbackslash, not to be hyphenated?

Second question: what is the difference between packages hyphenation and hyphenat - which is the one I am using in the MWE. Browsing through CTAN for an answer is like getting lost in the woods.

\documentclass{article}
% RN. 24 Jan 2019
%=======================
\usepackage[check-declarations,log-functions]{expl3}
\usepackage{MyHyphenationDefinitions}
%-----------------------
\begin{document}
\noindent
\textbf{filler.......................text} thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text} \textbackslash thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\end{document}

% filename: MyHyphenationDefinitions.sty
\ProvidesExplPackage{MyHyphenationDefinitions}{}{}{}

\usepackage[htt]{hyphenat}  %  enables normal as well as tt text to be hyphenated
\hyphenation{the-quick-brown-fox-jumped-over-the-fence-and-al-most-got-his-leg-caught}


Well, package hyphenat is a little bit old and I suggest to use package fontenc instead to get a better hyphenation. The advantage is that you get rid of the several warnings package [htt]{hyphenat} generates as already mentioned in the documentation:

I added package showframe to visualize the resulting typing area and margins of your document. It helps us to see if the hyphenation is inside the typing area or not.

Please see the following changed mwe

\documentclass{article}
% RN. 24 Jan 2019
%=======================
%\usepackage[check-declarations,log-functions]{expl3}
%\usepackage[htt]{hyphenat}  % <==================== enables normal as well as tt text to be hyphenated
\usepackage[T1]{fontenc} % <============================================
\usepackage{showframe} % <==============================================
\hyphenation{the-quick-brown-fox-jumped-over-the-fence-and-al-most-got-his-leg-caught}

\begin{document}
\noindent
\textbf{filler.......................text}
thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler......................text} % <=========================== one blank deleted!
\textbackslash thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text}
\textbackslash thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text}
\textbackslash thequickbrownfoxjumpedoverthefenceandalmostgo\-thislegcaught % <==============
\end{document}


and its result:

As you can see the hyphenation in the first sentence (without textbackslash) is okay.

The second sentence is only okay, because I deleted one blank in the filler text. Well, it is (nearly perfect (1.42pt too wide you can ignore, you can not see it on the screenshot, right?).

The third sentence is ugly, because it goes into the margin (4.62pt too wide). The added backslash needs place and the automatic did not hyphenate at the hyphenate mark before (see red arrow marked with 1 in the screenshot) ...

The forth sentence is also ugly, because I added an rule breaking manually hyphenating point with \- after the o (see red arrow in image, marked with 2).

To summarize:

1. Hyphenation works with package fontenc and option T1 (I would use package microtype too).
2. You can use command \hyphenation{} to define special words with correct hyphenating.
3. You can always add manual hyphenating points with \- in your text.
4. Hyphenating word with special characters (\, ä, ö, ü, ß, ê, -, ... can be problematic, but you can use manual added hyphenation points ...

Hyphenation is inhibited past a font change. The LaTeX kernel has

\DeclareTextSymbolDefault{\textbackslash}{OMS}


and omsenc.def has

\DeclareTextSymbol{\textbackslash}{OMS}{110}        % "6E


which means that \textbackslash uses the OMS font, which is different from the text font. You can use \allowbreaks as defined by babel, which adds an unbreakable zero skip. Here I use \providecommand so a previous definition by babel would not be overwritten.

An alternative is to use the T1 encoding, where the backslash is a character in the same text font.

\documentclass{article}
\usepackage[htt]{hyphenat}  %  enables normal as well as tt text to be hyphenated
\hyphenation{the-quick-brown-fox-jumped-over-the-fence-and-al-most-got-his-leg-caught}

\makeatletter
\providecommand{\allowhyphens}{\ifvmode\else\nobreak\hskip\z@skip\fi}
\makeatother

\begin{document}

\noindent
\textbf{filler.......................text} thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text} \textbackslash thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text} \textbackslash\allowhyphens thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught

\noindent
\textbf{filler.......................text} \texttt{thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught}

\noindent
\textbf{filler.......................text} \texttt{\textbackslash thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught}

\noindent
\textbf{filler.......................text} \texttt{\textbackslash\allowhyphens thequickbrownfoxjumpedoverthefenceandalmostgothislegcaught}

\end{document}