Given the following custom commands:


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


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.

  • 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

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:

text foo bar baz hello
| 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).


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



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).


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

\classformatter{org.openscience.cdk .organizationlayout .StructureDiagramGenerator}

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

\classformatter{org.openscience.cdk\\ .organizationlayout .StructureDiagramGenerator}

| improve this answer | |

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.