9

Based on my old question Push long words in a new line, I search for a possibility to allow linebreak before a capital character in camel case terms.

For example the term createUnspecifiedNodeErrorMarker can be break into create-Unspecified-Node-Error-Marker.

\documentclass{scrartcl}

\begin{document}
\section{Test}
And another example the show must go on, but we have too less text (\textbf{createUnspecifiedNodeWarningMarker} and
\textbf{createUnspecifiedNodeErrorMarker}, sdjklashjksa \textbf{createUnspecifiedLinkWarningMarker} and
\textbf{createUnspecifiedLinkErrorMarker}).
\end{document}
CSchulz
  • 1,915
  • 1
    Be (but only slightly) careful what you wish for. Compare: "StringRenderingEngine" with "String[space]RenderingEngine". If it splits in the wrong place you have ambiguity. – Brent.Longborough Aug 10 '12 at 08:18
  • You are right, I only want to use it in an seperate environment, not for the whole document. – CSchulz Aug 10 '12 at 08:48

2 Answers2

8

TeX would linebreak (hyphenate) the camel case words, but perhaps not where you wish eg

cre-a-te-Un-spec-i-fiedNodeEr-ror-Marker

shows the default hyphenation points (using the default US English hyphenation)

It wasn't clear if you wanted hyphens at the break points. I have assumed not. If you do then change \penalty2 to \-.

enter image description here

This boxes the parts between capital letters so they don't break, and puts a small penalty before capitals so they can break.

As with anything using catcode changes (like \verb it will not work in the argument to another command)

\documentclass{scrartcl}

\showhyphens{createUnspecifiedNodeErrorMarker}

\makeatletter
\def\zzz{\leavevmode\begingroup
\let\ifcase\iftrue
\def\or##1{%
  \catcode`##1\active\uccode`\~`##1\uppercase{%
    \def~{\egroup\penalty2\hbox\bgroup\string##1}}}%
\@Alph{}%
\@zzz}

\def\@zzz#1{\textbf{\hbox\bgroup#1\egroup}\endgroup}
\makeatother

\begin{document}
\section{Test}


And another example the show must go on, but we have too less text (\zzz{createUnspecifiedNodeWarningMarker} and
\zzz{createUnspecifiedNodeErrorMarker}, sdjklashjksa \zzz{createUnspecifiedLinkWarningMarker} and
\zzz{createUnspecifiedLinkErrorMarker}).
\end{document}
David Carlisle
  • 757,742
2

An implementation in expl3 that also allows a (named) color as optional argument to \keyw:

\documentclass{article}
\usepackage{xcolor}
\usepackage{microtype}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\keyw}{O{.}m}
 {
  \textcolor{#1}{ \bfseries \cschulz_keyw:n { #2 } }
 }

\cs_new_protected:Nn \cschulz_keyw:n
 {
  \tl_map_inline:nn { #1 }
   {
    % if the current char is uppercase, add a discretionary hyphen
    \str_if_eq:eeT { ##1 } { \str_uppercase:n { ##1 } } 
     { \- }
    ##1
   }
 }
\ExplSyntaxOff

\begin{document}

\section{Test}

And another example the show must go on, but we have too less 
text (\keyw{createUnspecifiedNodeWarningMarker} sdjkle
\keyw{createUnspecifiedNodeErrorMarker}, sdjklashjksa 
\keyw{createUnspecifiedLinkWarningMarker} and
\keyw[red]{createUnspecifiedLinkErrorMarker}).

\end{document}

enter image description here

Expect several overfull boxes, anyway, as breaking only at capital letters is rather rigid.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
egreg
  • 1,121,712