# How to add hyphenation support for custom command?

Given the following custom commands:

\newcommand{\topic}[1]{#1\index{#1}}
\newcommand{\class}[1]{\textsf{\topic{#1}}}


which is used for Java classes with full package name, like:

\class{org.openscience.cdk.layout.StructureDiagramGenerator}


With the current command definition, the class name will not be hyphenated at the periods. How can I change my \newcommand{} to have LaTeX hyphenate the class at periods?

I am using the \class{} inside \caption{} methods which seems to invalidate some solutions.

-
Can you please clarify where the \topic command comes from? (It would be even better if you could provide a minimal working expample.) – Hendrik Vogt Oct 28 '10 at 8:52
Oops. Thanx for the ping. Added now. – Egon Willighagen Oct 28 '10 at 9:05

I would be inclined to use the url package for this task. (It provides flexible options for linebreaking of such things, not only URLs.) Here's an example:

\documentclass[twocolumn]{article}
\usepackage{url}
\DeclareUrlCommand\printtopic{\urlstyle{sf}}
\newcommand{\topic}[1]{\protect\printtopic{#1}\index{#1}}
\DeclareRobustCommand{\class}[1]{\topic{#1}}
\begin{document}
text foo bar baz hello
\class{org.openscience.cdk.layout.StructureDiagramGenerator}
\end{document}

-
This set up gives me: "\Url Error ->\url used in a moving argument." – Egon Willighagen Oct 28 '10 at 10:00
The problem seems to be that I use the \class{} command in a \caption{}... is there a solution to that? – Egon Willighagen Oct 28 '10 at 10:05
@Egon: Can you please specify where the error shows up? I have no problem compiling Will's code. – Hendrik Vogt Oct 28 '10 at 11:03
Put the class{} in a figure \caption{}... – Egon Willighagen Oct 28 '10 at 11:12
Use \DeclareRobustCommand for such cases (look up "fragile commands" in a LaTeX reference to explain all this). Now fixed above. – Will Robertson Oct 28 '10 at 11:20

Whatever you do it will be next to impossible to get an acceptable typesetting with all variants of class names. One could use an approach where you can use a semi-manual approach as shown in the example below (last example).

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{url}
\begin{document}
\overfullrule=0.5pt
\parskip=20pt
Using \textbackslash url, it will remove all spaces the colon will be placed at end of section, whereas a common convention is to move it at the next line

\fbox{\begin{minipage}{1.65in}
\url{org.openscience.cdk.layout.StructureDiagramGenerator}
\end{minipage}}

\gdef\classformatter#1{
\hyphenpenalty=-200
\textsf{#1}
}

It is impossible for \TeX to hyphenate the next example, as the fist word of a paragraph is never hyphenated and this is treated as one long word (\TeX\ is probably thinking it is just a long abbreviation).

\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk.organizationlayout.StructureDiagramGenerator}
\end{minipage}

If you type a space at the point you want to allow a break \TeX\ will attempt to hyphenate,

\fbox{\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk .organizationlayout .StructureDiagramGenerator}
\end{minipage}}

You will never avoid a little bit of hand manipulation, allow a break now and then;

\fbox{\begin{minipage}{1.65in}
\classformatter{org.openscience.cdk\\ .organizationlayout .StructureDiagramGenerator}
\end{minipage}}

\end{document}

-