# Converting an underscored string to camelcase

I would like to take a string containing underscores and convert it to CamelCase. I am attempting to do this using the stringstrings package. Here is my code:

\documentclass[11pt]{report}
\usepackage{amsmath}
\usepackage{stringstrings}
\begin{document}

\convertchar[q]{test_string}{\textunderscore}{ ??? }
\capitalizewords[q]{\thestring}
\noblanks{\thestring}

\end{document}


I am not sure what to put as the third argument to \convertchar to convert the underscores to spaces. I have tried ~, quad, and . They all produce strange errors. What can I use as the third argument, or, is there another way to obtain camelcase?

• As you are dealing with "magic chars" I guess you have to provide the [e] (encoded) option instead of [q] to stringstrings functions. – Daniel Nov 19 '13 at 8:23
• @Daniel Ah! I didn't realize. Thanks for pointing that out. – Sean Mackesey Nov 19 '13 at 8:34
• I have provided this now as an answer. If this answer serves your needs better as the other answer (wrt. using stringstrings and providing the CamelCase), you should move the accept mark to it, as this helps further visitors to figure what has solved your requirements. Note that at this site it is considered generally a good idea to wait for a couple of days before accepting an answer. – Daniel Nov 19 '13 at 9:21
• @Daniel is right. Feel free to give him the checkmark if his answer is more satisfactory than mine is. – jub0bs Nov 19 '13 at 10:07

When chaining stringstrings commands, you should work with the encoded form (given by the [e] option) until the very last command:

\documentclass[11pt]{report}
\usepackage{amsmath}
\usepackage{stringstrings}
\begin{document}

\convertchar[e]{test_string}{_}{ }
\capitalizewords[e]{\thestring}
\noblanks[q]{\thestring}
\thestring

\end{document}


Basically, [e]-mode works like [q]-mode in that it does not produce any output, but makes sure that special characters (such as _) do not get expanded prematurely. This is generally required, if \thestring is fed into another stringstring command. The big benefit of this approach is that you can prevent \expandafter chains if composing string manipulation commands. §2 of the stringstrings manual explains this in detail.

Here is an easy way of substituting spaces for underscores in a string (with the xstring package).

\documentclass{article}

\usepackage{xstring}

\begin{document}

\newcommand\myStringWithUnderscores{Man_is_a_rational_animal_so_at_least_I_have_been_told}
\StrSubstitute{\myStringWithUnderscores}{_}{ }[\myStringWithSpaces]

% just for showing the results
\begin{tabular}{ll}
Original string & \texttt{\expandafter\detokenize\expandafter{\myStringWithUnderscores}}\\
Final string    & \texttt{\myStringWithSpaces}
\end{tabular}

\end{document}


As for converting to CamelCase, well, that's not specific enough for a computer, which wouldn't automatically know which letter to make uppercase/lowercase. I guess 'converting to CamelCase' is not something that can be easily automated to great effect, and you'll probably have to do it on a string-by-string basis.

• CamelCase usually just means uppercasing all words and joining them together, no other processing needed, e.g. ManIsARationalAnimalSoAtLeastIHaveBeenTold – SztupY Nov 19 '13 at 11:04
• In that case, that's easy; but I'm not sure that's exactly what the OP wants. – jub0bs Nov 19 '13 at 11:21