Is there a way to define a command that changes the colour of any capital letter in an input string?
Most of the results I find when I type "LaTeX string capital" are results about capitalizing the first letter of each word. Here I would like to process all the capital letters of a string.
I know TeX is really nice for many reasons, but when it comes to actual programming I really feel like there is nothing else like it. I hope I won't offend anyone if I say that despite how grateful I am for LaTeX to even exist, I sometimes wish TeX's syntax was more traditional when it comes to actual programming.
Best Answer
You can do it with a regular expression, if you have the input string as an argument.
Note
The syntax for the replacement string is a bit convoluted, but not difficult:
\c{textcolor}
means “the control sequence\textcolor
”\cB\{
means “an opening brace with its normal function of Begin group\cE\}
means “a closing brace with its normal function of End groupThus the replacement text can be read as
where
#1
is the optional argument to\colorcap
.A version that also supports accents, provided they're input in the “classical” way with a macro.
Here
\cC.
matches any control sequence,\{?
zero or one open brace,\}?
zero or one closed brace, so both\`O
and\r{U}
inputs are caught.