[Tex/LaTex] Hyphenation of long Java identifiers

best practicescodehyphenation

What is the best practice for hyphenating long identifiers like Character.isJavaIdentifierStart when they appear in body text? I am aware of the path package that also deals with otherwise active characters.

On occasion I have locally defined

\def\+{\discretionary{}{}{}}

which introduces a hyphenation point at \+ but no hyphen in order to avoid the confusion whether the hyphen belongs to the identifier or not.

Best Answer

I am not too sure if there is a best practice, but here are two approaches.

The first method uses the url command to automatically hyphenate at certain points such as for example dots. The second one uses a discretionary (with or without a hyphen). Whatever you choose, I am sure that you will need to give TeX a helping hand, that is you cannot avoid doing some manual editing in the end.

In the example I colored the word and used a libertine family font.

\documentclass{report}
\usepackage{geometry,xcolor,url}
\usepackage{libertine}
\geometry{paperwidth=130mm, paperheight=60mm,
textwidth=95mm, textheight=50mm}

\def\+{\discretionary{}{-}{}}
\def\textinline#1{{\usefont{T1}{fxb}{m}{n}\selectfont\textcolor{purple}{\url{#1}}}}

\def\ttextinline#1{{\usefont{T1}{fxb}{m}{n}\selectfont\textcolor{purple}{#1}}}
\begin{document}
Java programmers tend like to use very long variable names as compared to other
languages. A string such as \textinline{this.class.VariableName}
will get split rather than produce an overfull box?

Java programmers tend like to use very long variable names as compared to other
languages. A string such as \ttextinline{this.classVari\+ableName}
will get split rather than produce an overfull box?
\end{document}

enter image description here