# Hyphenation for words with underscore locally

I am trying to hyphenate long keywords like given in the example:

\documentclass[10pt]{article}
\usepackage{underscore}

\begin{document}

\pagestyle{empty}

\parbox{1pt}{TESTSTRING\_TESTSTRING\_HYPHENATION}
\\\\

%\begin{figure}[here]
%\centerline{\includegraphics*[width=0.8\paperwidth]{/pictures/regular_testname.png}}
%\centerline{\includegraphics*[width=0.8\paperwidth]{/pictures/escaped\_testname.png}}
%\end{figure}

\end{document}


I tried this and this and more without success. If I uncomment the figures in the example, the underscore-package produced problems because the underscore in the filenames isn't interpreted properly anymore. Without using this package, the pictures can be inserted, but I don't get my strings hyphenated.

Do you have an idea how to hyphenate properly and just locally, like e.g. in that kind: TESTSTRING\myUnderTESTSTRING\myUnderHYPHENATION?

And how can I control that no hyphen is written while hyphenating the string? This would be even preferable.

As a side-question: In my example, why isn't the first part of the string being hyphenated?

• First word of a paragraph is not hyphenated, which explains the intiial "TESTSTRING" – Steven B. Segletes Jan 19 '17 at 17:45
• @StevenB.Segletes Thx for this. Can I control it? My final goal is to use the hyphenation for keywords in several parbox in a table/tabular environment. – EverythingRightPlace Jan 19 '17 at 17:47
• Using \hspace{0pt}TESTSTRING\_TESTSTRING\_HYPHENATION will allow the first word to be hyphenated. – Steven B. Segletes Jan 19 '17 at 17:50
• Without the underscore package, hyphenation can be achieved with \hspace{0pt}TESTSTRING\_\allowbreak\hspace{0pt}TESTSTRING\_\allowbreak\hspace{0pt}HYPHENATION – Steven B. Segletes Jan 19 '17 at 17:52

Perhaps something like this would help, without using the underscore package, which you would seem to prefer (not using it). It activates the hyphenability with \newuson and restores the original definition of \_ with \newusoff. Through this on/off mechanism, you can control the scope of the modifications.

Also, the "hyphenation" of the underscore is without the dash, which also seemed to be desired, if I understood the question properly.

In any case, to get the first word of a paragraph to hyphenate, a \hspace{0pt} is required to start the paragraph.

\documentclass{article}
%\usepackage{underscore}
\textwidth0pt
\let\svus\_
\newcommand\newuson{\def\_{\svus\allowbreak\hspace{0pt}}}
\newcommand\newusoff{\let\_\svus}
\begin{document}
TESTSTRING\_TESTSTRING\_HYPHENATION

\newuson
\hspace{0pt}TESTSTRING\_TESTSTRING\_HYPHENATION

\newusoff
TESTSTRING\_TESTSTRING\_HYPHENATION
\end{document}


• Works like a charm! – EverythingRightPlace Jan 20 '17 at 14:52
• Could you please explain what the command is doing? Why do I need \textwidth0pt or \let\svus\_? And what is it doing? – EverythingRightPlace Jan 20 '17 at 14:59
• @EverythingRightPlace \textwidth0pt is merely to test the method by making the document line of zero width, which essentially tries to hyphenate everything in sight. It is like your \parbox{1pt}{} artifice. Thus, it can go. The \let\svus\_ needs to stay. It makes a copy of the originally defined \_, in order to avoid self-reference when redefining \_ subsequently. Also, it provides the copy that gets reinstated by \newusoff. – Steven B. Segletes Jan 20 '17 at 19:00
• A more compact version: \newcommand\myunderscoredword{\let\svus\_\def\_{\svus\allowbreak\hspace{0pt}}UNDER\_SCORE\_STRING \let\_\svus} – Roy Shilkrot Feb 11 '18 at 7:09

If you are keen to keep your long strings with underscore as the argument to a macro, it's easy:

\documentclass[10pt]{article}

\DeclareRobustCommand\Name[1]{{%
\let\_\hyphenationunderscore#1%
}}
\newcommand{\hyphenationunderscore}{%
\textunderscore\nobreak\hspace{0pt}%
}

\begin{document}

\pagestyle{empty}

\parbox{1pt}{
\hspace{0pt}%
\Name{TESTSTRING\_TESTSTRING\_HYPHENATION}
}

\end{document}


You could also redefine \_ globally:

\documentclass[10pt]{article}

\renewcommand{\_}{%
\textunderscore\nobreak\hspace{0pt}%
}

\begin{document}

\pagestyle{empty}

\parbox{1pt}{
\hspace{0pt}%
TESTSTRING\_TESTSTRING\_HYPHENATION
}

\end{document}


Alternatively, you can make _ active:

\documentclass[10pt]{article}

\newcommand{\hyphenationunderscore}{%
\textunderscore\nobreak\hspace{0pt}%
}
\catcode_=\active
\protected\def_{\ifmmode\sb\else\hyphenationunderscore\fi}

\begin{document}

\pagestyle{empty}

\parbox{1pt}{
\hspace{0pt}%
TESTSTRING_TESTSTRING_HYPHENATION
}

\end{document}


Note that without \hspace{0pt} you get no hyphenation in the \parbox, because TeX doesn't hyphenate a word that's not preceded by glue.

If you want to break also after the underscore, change \nobreak into \linebreak[0] throughout. For instance, the second solution becomes

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}

\renewcommand{\_}{%
\textunderscore\linebreak[0]\hspace{0pt}%
}

\begin{document}

\pagestyle{empty}

TESTSTRING\_TESTSTRING\_HYPHENATION

\parbox{1pt}{
\hspace{0pt}%
TESTSTRING\_TESTSTRING\_HYPHENATION
}

\end{document}


The output here also shows what happens with T1.

• Your 1) idea isn't working in my table/tabular environment. What do you mean with your 3) by making _ active? – EverythingRightPlace Jan 20 '17 at 15:06
• @EverythingRightPlace When a character is made active, it behaves like a macro (and should be given a definition). As for the first solution not to be working, something more should be seen: are you sure you add some space (even zero) before the string? – egreg Jan 20 '17 at 15:41
• Yes I add \hspace{0pt} in front. Also in your example the string isn't hyphenated at the location of the underscore. The code of @Steven B. Segletes is doing so. Nevertheless, thx for the effort, I am using the \newuson now. – EverythingRightPlace Jan 20 '17 at 15:53
• @EverythingRightPlace If you want to split also after the underscore, it's just a matter of changing \nobreak into \linebreak[0]`. – egreg Jan 20 '17 at 15:55