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.

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

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
Works perfectly. Thank you. –  B3ret Jun 2 '11 at 9:27

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

share|improve this answer
    
Should have though of this one... –  B3ret Jun 2 '11 at 9:24

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.