6

My text contains some long, CamelCaseClassNames which LaTeX doesn't like to hyphenate automatically. I found the \camelhyph macro on projects:latexcamel and it works perfectly for automatic hyphenation before each upper case letter.

However, I also want to allow hyphenation within some longer words in the classname. As soon as I use the \camelhyph macro on anything containing \\- it breaks down.

Can anyone fix this?

\documentclass{article}

\usepackage{xspace}
\usepackage{ifthen}

\makeatletter
  \newcommand{\camelhyph}[1]{\@fterfirst\c@amelhyph#1\relax }
  \def\@fterfirst #1#2{#2#1}
  \def\c@amelhyph #1{%
   \ifthenelse{\equal{#1}\relax}{}{%  Do nothing if the end has been reached
     \ifnum`#1<91 \-#1\else#1\fi%     Check whether #1 is an uppercase letter,
                                %     if so, print \-#1, otherwise #1
    \expandafter\c@amelhyph%    %     insert \c@amelhyph again.
}}
\makeatother


\newcommand{\cclass}[1]{\texttt{\camelhyph{#1}}\xspace}
\begin{document}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Curabitur vitae lectus augue. Duis vitae quam sem. 
Integer rhoncus tempor est et convallis. Ddonec orciodio, 
facilisis in portaeget,  
\cclass{MySpecialCamelCaseLooooooongwooooordClassName} 
ornare veltortor. Pellentesque sed risus sed dolor iaculis
malesuada vitae ac purus. Praesent convallis mattis faucibus.
Nulla gravida ante id sapien tincidunt nec suscipit magna placerat. 

%not working because of the additional breakmark: \-
%Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
%Curabitur vitae lectus augue. Duis vitae quam sem. 
%Integer rhoncus tempor est et convallis. Ddonec orciodio, 
%facilisis in portaeget,  
%\cclass{MySpecialCamelCaseLooooooongwooo\-oordClassName} 
%ornare veltortor. Pellentesque sed risus sed dolor iaculis
%malesuada vitae ac purus. Praesent convallis mattis faucibus.
%Nulla gravida ante id sapien tincidunt nec suscipit magna placerat. 

\end{document}
1
  • It might not be the answer you are looking for, but for those special cases you could simply use \texttt{MySpecialCamelCaseLooooooongwooo\-oordClassName}. – Gonzalo Medina May 30 '11 at 13:41
5

You have to test also for \-:

\def\camelhyph#1{\c@melhyph#1\relax}
\def\c@melhyph#1{%
  \ifx#1\relax\else
    \ifx#1\-#1\else
      \ifnum`#1<91 \-\fi
      #1%
      \expandafter\expandafter\expandafter\c@melhyph\expandafter
    \fi
  \fi}

If #1 is \relax we are at the end; if it's \- insert it and move on; if it's an uppercase letter, insert \-, otherwise do nothing; insert #1 and move on.

0
3

Enclose the \- command into braces: {\-}

\documentclass{article}

\usepackage{xspace}
\usepackage{ifthen}

\makeatletter
  \newcommand{\camelhyph}[1]{\@fterfirst\c@amelhyph#1\relax }
  \def\@fterfirst #1#2{#2#1}
  \def\c@amelhyph #1{%
   \ifthenelse{\equal{#1}\relax}{}{%  Do nothing if the end has been reached
     \ifnum`#1<91 \-#1\else#1\fi%     Check whether #1 is an uppercase letter,
                                %     if so, print \-#1, otherwise #1
    \expandafter\c@amelhyph%    %     insert \c@amelhyph again.
}}
\makeatother


\newcommand{\cclass}[1]{\texttt{\camelhyph{#1}}\xspace}
\begin{document}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Curabitur vitae lectus augue. Duis vitae quam sem. 
Integer rhoncus tempor est et convallis. Ddonec orciodio, 
facilisis in portaeget,  
\cclass{MySpecialCamelCaseLooooooongwooo{\-}oordClassName} 
ornare veltortor. Pellentesque sed risus sed dolor iaculis
malesuada vitae ac purus. Praesent convallis mattis faucibus.
Nulla gravida ante id sapien tincidunt nec suscipit magna placerat. 

\end{document}

Result:

result

1
  • Should have though of this one... – B3ret Jun 2 '11 at 9:24

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.