I'm looking for a way to do a substring replace on a string in LaTeX. What I'd like to do is build a command that I can call like this:
\replace{File,New}
and that would generate something like
\textbf{File}$\rightarrow$\textbf{New}
This is a simple example, but I'd like to be able to put formatting/structure in a single command rather than everywhere in the document. I know that I could build several commands that take increasing numbers of parameters, but I'm hoping that there is an easier way.
Edit for clarification
I'm looking for an equivalent of
string.replace(",", "$\rightarrow$)
something that can take an arbitrary string, and replace a substring with another substring.
So I could call the command with \replace{File}, \replace{File,New}, \replace{File,Options,User}, etc., wrap the words with bold formatting, and replace any commas with the right arrow command. Even if the "wrapping with bold" bit is too difficult (as I think it might be), just the replace part would be helpful.
There's a LaTeX package called tokenizer which may help you to do what you want.
Here's a hack (but pure LaTeX, no internals) which gets close to what I think you want, but with some extraneous spaces I haven't quite been able to fix. Perhaps Will Robertson can advise further? Unlike his slightly more polished answer, I haven't parameterised the bits and pieces, Here goes:
The general case is rather more tricky (when you're not using commas as separators), but the example you gave can be coded without too much trouble with some knowledge of the LaTeX internals.
Yes there are bugs in tokenizer package. As I said on my blog, the bugfix is to use the following correcting code instead of just "\usepackage[trim]{tokenizer}":
I will report this bugfix to the developer Sascha Herpers
Try the
xstring
package:OK, I withdraw this answer. Thanks for clarifying the question.
I suspect this may not be what you want, but here goes anyway:
If this isn't what you're looking for, could you clarify the question, please?