Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
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

2 Answers 2

up vote 8 down vote accepted

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}
share|improve this answer
    
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
3  
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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.