0

I'm trying to prevent a word that contains an underscore to be hyphenated.

I have tried:

\hyphenation{Word\textunderscore With\textunderscore Underscore}
\begin{document}
\end{document}

Improper \hyphenation will be flushed.
\PD1-cmd ...\TextSymbolUnavailable #1}\fi \global 
                                                  \expandafter \let \csname ...
l.50  \hyphenation{Word\textunderscore
                                       With\textunderscore Underscore} 

and

\usepackage{underscore}
\hyphenation{Word_With_Underscore}
\begin{document}
\end{document}

Improper \hyphenation will be flushed. l.50 \hyphenation{Word_
                       With_Underscore}
3

Package underscore does the opposite, it tries to allow hyphenation before and after the underscore. The macro \BreakableUnderscore can be redefined by modifying the original definition:

\documentclass{article}
\usepackage{underscore}

\makeatletter
% Redefine to an unbreakable underscore
\renewcommand*{\BreakableUnderscore}{%
  \leavevmode
  \null % prevent hyphenation before the underscore
  % \nobreak
  % \hskip\z@skip
  \ifx\f@family\ttdefault
    \string_%
  \else
    \textunderscore
  \fi
  % \usc@dischyph\nobreak\hskip\z@skip
}
\makeatother

\begin{document}
\parbox{0pt}{%
  \hspace{0pt}document_document
}
\end{document}

Result

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.