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.

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?

share|improve this question
    
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. –  Jubobs Nov 19 '13 at 10:07
add comment

2 Answers

up vote 4 down vote accepted

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}

enter image description here

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.

share|improve this answer
add comment

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

enter image description here

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

share|improve this answer
    
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. –  Jubobs Nov 19 '13 at 11:21
add comment

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.