Change the color of capital letters

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.

-
For xelatex, see tex.stackexchange.com/questions/33855/… –  David Carlisle Apr 24 '14 at 11:10
For a LuaLaTeX approach, have a look at the chickenize package. –  Martin Schröder May 1 '14 at 19:11

You can do it with a regular expression, if you have the input string as an argument.

\documentclass{article}
\usepackage{xparse,l3regex}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\colorcap}{ O{blue} m }
{
\sheljohn_colorcap:nn { #1 } { #2 }
}

\tl_new:N \l__sheljohn_colorcap_input_tl
\cs_new_protected:Npn \sheljohn_colorcap:nn #1 #2
{
% store the string in a variable for usage with \regex_replace_all:nnN
\tl_set:Nn \l__sheljohn_colorcap_input_tl { #2 }
\regex_replace_all:nnN
{ ([A-Z]+) } % search a capital letter (or more)
{ \c{textcolor}\cB\{#1\cE\}\cB\{\1\cE\} } % replace the match with \textcolor{#1}{<match>}
\l__sheljohn_colorcap_input_tl
\tl_use:N \l__sheljohn_colorcap_input_tl
}
\ExplSyntaxOff

\begin{document}
\colorcap{Once Upon a Time}

\colorcap[red]{Once Upon a Time}
\end{document}


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 B⁣egin group
• \cE\} means “a closing brace with its normal function of E⁣nd group

Thus the replacement text can be read as

\textcolor{#1}{<match>}


where #1 is the optional argument to \colorcap.

-
That looks more like the correct answer actually :) But is it for LaTeX3 only? Or is l3regex available on texlive/popular LaTeX2e distributions? –  Sh3ljohn Apr 24 '14 at 14:38
Could you elaborate on the functions of each line in your code a little bit more? –  azetina Apr 24 '14 at 15:16
@Sh3ljohn The expl3 suite is available on every updated TeX distribution. –  egreg Apr 24 '14 at 16:26
@azetina Done; I had only a few minutes before the LaTeX class when I wrote the answer. ;-) –  egreg Apr 24 '14 at 16:38
Great! I envy your students for having such a wonderful LaTeX teacher like you. They will surely be guided on the right path :-) –  azetina Apr 24 '14 at 16:43

This solution shows an active character approach, which (though heavy in the setup) will allow one to have cap letter automatically colorized. But because active letters will tend to break macros, I provide the means to disable it. This revised solution provides the following macros:

\capcoloron[color] turns all cap letters to this color (default red)

\capcoloroff turns off active characters and restores original cap-letter definitions

\coloron{letter}{color} turns just this [inactive] letter active and sets it to the specified color

\coloroff{letter} turns off just this active character, and restores its original definition.

The only quirk is that to change the color of a given capital letter that is already active, one must first turn the color off, and then turn it on again. Here is my MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{stringstrings}

\let\sva A\let\svb B\let\svc C\let\svd D\let\sve E\let\svf F\let\svg G
\let\svh H\let\svi I\let\svj J\let\svk K\let\svl L\let\svm M\let\svn N
\let\svo O\let\svp P\let\svq Q\let\svr R\let\svs S\let\svt T\let\svu U
\let\svv V\let\svw W\let\svx X\let\svy Y\let\svz Z
\catcodeA=\active\catcodeB=\active\catcodeC=\active\catcodeD=\active
\catcodeE=\active\catcodeF=\active\catcodeG=\active\catcodeH=\active
\catcodeI=\active\catcodeJ=\active\catcodeK=\active\catcodeL=\active
\catcodeM=\active\catcodeN=\active\catcodeO=\active\catcodeP=\active
\catcodeQ=\active\catcodeR=\active\catcodeS=\active\catcodeT=\active
\catcodeU=\active\catcodeV=\active\catcodeW=\active\catcodeX=\active
\catcodeY=\active\catcodeZ=\active
\newcommand\colorcapon[1][red]{%
\catcodeA=\active\def A{\textcolor{#1}{\sva}}%
\catcodeB=\active\def B{\textcolor{#1}{\svb}}%
\catcodeC=\active\def C{\textcolor{#1}{\svc}}%
\catcodeD=\active\def D{\textcolor{#1}{\svd}}%
\catcodeE=\active\def E{\textcolor{#1}{\sve}}%
\catcodeF=\active\def F{\textcolor{#1}{\svf}}%
\catcodeG=\active\def G{\textcolor{#1}{\svg}}%
\catcodeH=\active\def H{\textcolor{#1}{\svh}}%
\catcodeI=\active\def I{\textcolor{#1}{\svi}}%
\catcodeJ=\active\def J{\textcolor{#1}{\svj}}%
\catcodeK=\active\def K{\textcolor{#1}{\svk}}%
\catcodeL=\active\def L{\textcolor{#1}{\svl}}%
\catcodeM=\active\def M{\textcolor{#1}{\svm}}%
\catcodeN=\active\def N{\textcolor{#1}{\svn}}%
\catcodeO=\active\def O{\textcolor{#1}{\svo}}%
\catcodeP=\active\def P{\textcolor{#1}{\svp}}%
\catcodeQ=\active\def Q{\textcolor{#1}{\svq}}%
\catcodeR=\active\def R{\textcolor{#1}{\svr}}%
\catcodeS=\active\def S{\textcolor{#1}{\svs}}%
\catcodeT=\active\def T{\textcolor{#1}{\svt}}%
\catcodeU=\active\def U{\textcolor{#1}{\svu}}%
\catcodeV=\active\def V{\textcolor{#1}{\svv}}%
\catcodeW=\active\def W{\textcolor{#1}{\svw}}%
\catcodeX=\active\def X{\textcolor{#1}{\svx}}%
\catcodeY=\active\def Y{\textcolor{#1}{\svy}}%
\catcodeZ=\active\def Z{\textcolor{#1}{\svz}}%
}
\def\colorcapoff{%
\catcodeA=11\let A\sva%
\catcodeB=11\let B\svb%
\catcodeC=11\let C\svc%
\catcodeD=11\let D\svd%
\catcodeE=11\let E\sve%
\catcodeF=11\let F\svf%
\catcodeG=11\let G\svg%
\catcodeH=11\let H\svh%
\catcodeI=11\let I\svi%
\catcodeJ=11\let J\svj%
\catcodeK=11\let K\svk%
\catcodeL=11\let L\svl%
\catcodeM=11\let M\svm%
\catcodeN=11\let N\svn%
\catcodeO=11\let O\svo%
\catcodeP=11\let P\svp%
\catcodeQ=11\let Q\svq%
\catcodeR=11\let R\svr%
\catcodeS=11\let S\svs%
\catcodeT=11\let T\svt%
\catcodeU=11\let U\svu%
\catcodeV=11\let V\svv%
\catcodeW=11\let W\svw%
\catcodeX=11\let X\svx%
\catcodeY=11\let Y\svy%
\catcodeZ=11\let Z\svz%
}
\newcommand\coloron[2]{%
\if\sva#1\catcodeA=\active\def A{\textcolor{#2}{\sva}}\else%
\if\svb#1\catcodeB=\active\def B{\textcolor{#2}{\svb}}\else%
\if\svc#1\catcodeC=\active\def C{\textcolor{#2}{\svc}}\else%
\if\svd#1\catcodeD=\active\def D{\textcolor{#2}{\svd}}\else%
\if\sve#1\catcodeE=\active\def E{\textcolor{#2}{\sve}}\else%
\if\svf#1\catcodeF=\active\def F{\textcolor{#2}{\svf}}\else%
\if\svg#1\catcodeG=\active\def G{\textcolor{#2}{\svg}}\else%
\if\svh#1\catcodeH=\active\def H{\textcolor{#2}{\svh}}\else%
\if\svi#1\catcodeI=\active\def I{\textcolor{#2}{\svi}}\else%
\if\svj#1\catcodeJ=\active\def J{\textcolor{#2}{\svj}}\else%
\if\svk#1\catcodeK=\active\def K{\textcolor{#2}{\svk}}\else%
\if\svl#1\catcodeL=\active\def L{\textcolor{#2}{\svl}}\else%
\if\svm#1\catcodeM=\active\def M{\textcolor{#2}{\svm}}\else%
\if\svn#1\catcodeN=\active\def N{\textcolor{#2}{\svn}}\else%
\if\svo#1\catcodeO=\active\def O{\textcolor{#2}{\svo}}\else%
\if\svp#1\catcodeP=\active\def P{\textcolor{#2}{\svp}}\else%
\if\svq#1\catcodeQ=\active\def Q{\textcolor{#2}{\svq}}\else%
\if\svr#1\catcodeR=\active\def R{\textcolor{#2}{\svr}}\else%
\if\svs#1\catcodeS=\active\def S{\textcolor{#2}{\svs}}\else%
\if\svt#1\catcodeT=\active\def T{\textcolor{#2}{\svt}}\else%
\if\svu#1\catcodeU=\active\def U{\textcolor{#2}{\svu}}\else%
\if\svv#1\catcodeV=\active\def V{\textcolor{#2}{\svv}}\else%
\if\svw#1\catcodeW=\active\def W{\textcolor{#2}{\svw}}\else%
\if\svx#1\catcodeX=\active\def X{\textcolor{#2}{\svx}}\else%
\if\svy#1\catcodeY=\active\def Y{\textcolor{#2}{\svy}}\else%
\if\svz#1\catcodeZ=\active\def Z{\textcolor{#2}{\svz}}\else%
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
}
\newcommand\coloroff[1]{\catcode#1=11%
\edef\tmp{\detokenize{#1}}%
\caselower[q]{\tmp}%
\expandafter\let\expandafter#1\csname sv\thestring\endcsname%
}
\catcodeA=11\catcodeB=11\catcodeC=11\catcodeD=11\catcodeE=11
\catcodeF=11\catcodeG=11\catcodeH=11\catcodeI=11\catcodeJ=11
\catcodeK=11\catcodeL=11\catcodeM=11\catcodeN=11\catcodeO=11
\catcodeP=11\catcodeQ=11\catcodeR=11\catcodeS=11\catcodeT=11
\catcodeU=11\catcodeV=11\catcodeW=11\catcodeX=11\catcodeY=11
\catcodeZ=11

\parskip 1em

\begin{document}

\colorcapon[cyan]
\verb|\colorcapon[color]| will change all caps to the specified color\\
A Test of The Active Method (\verb|Even in \Verbatim|)

\colorcapoff
\verb|\colorcapoff| will restore all things to the original LaTeX setting\\
A Test of The Active Method

\coloron{M}{blue}\coloron{A}{red}\coloron{T}{orange}
\verb|\coloron{letter}{color}| will turn an inactive letter active with the
specified color, done here to letters M'', A'', and T'':\\
A Test of The Active Method

\coloroff{A}
\verb|\coloroff{letter}| will restore that letter to the original LaTeX
setting, here done to the letter A'':\\
A Test of The Active Method

To change The color an already active letter, one must first turn the color off
and then set the color anew, as done here to the letter T'':\\
\coloroff{T}\coloron{T}{green}
A Test of The Active Method
\end{document}


ORIGINAL SOLUTION:

This shows the traditional and dangerous active character approach. The active approach takes a fair amount of "setup" code, but then will automatically change the cap letter colors upon use.

Because it is dangerous, I provide the means to enter and exit the active approach with \colorcapon and \colorcapoff. I've only set up 3 letters... but the other 23 follow in a similar manner.

\documentclass{article}
\usepackage{xcolor}
\begin{document}

\textcolor{red}{A} letter can have its color \textcolor{blue!60}{C}hanged
with the \textcolor{cyan!80}{U}se of \verb|\textcolor|.\vspace{2em}

ACTIVE CHARACTER APPROACH

\let\sva A
\let\svc C
\let\svu U
\catcodeA=\active
\catcodeC=\active
\catcodeU=\active
\def\colorcapon{%
\catcodeA=\active\def A{\textcolor{red}{\sva}}%
\catcodeC=\active\def C{\textcolor{blue!60}{\svc}}%
\catcodeU=\active\def U{\textcolor{cyan!80}{\svu}}%
}
\def\colorcapoff{%
\catcodeA=11\let A\sva%
\catcodeC=11\let C\svc%
\catcodeU=11\let U\svu%
}
\catcodeA=11
\catcodeC=11
\catcodeU=11

\colorcapon
A letter can have its color Changed with the Use of\\
\verb|\textcolor.  Wow.  And I can Use it in verbatim|.\vspace{2em}\colorcapoff

RESTORED TO ORIGINAL

A letter can have its color Changed with the Use of \verb|\textcolor|.

\end{document}


-
Impressive :) I'm going to read about these catcodes and what they do. Is there a way to select caps in a more generic manner than defining them individually? –  Sh3ljohn Apr 24 '14 at 10:54
@Sh3ljohn I'm thinking about that, but it requires another trick that I still haven't unraveled. If I obtain it, I will post. –  Steven B. Segletes Apr 24 '14 at 10:59
@Sh3ljohn Please see revision. –  Steven B. Segletes Apr 24 '14 at 13:36
Thanks, I'm going to mark it as the answer, but I find it a bit hackish personally. There ought to be a proper way of iterating over the chars of a string, checking for case, and modify those characters conditionally. –  Sh3ljohn Apr 24 '14 at 14:31
@Sh3ljohn There may be a better way, but it gets complicated, and problems arise when the argument passed to a macro is active, but the macro was defined when that character was not active. When that happens, things that look the same in the code will actually fail a comparison test, because they possess different catcode designations. –  Steven B. Segletes Apr 24 '14 at 14:49

This is a snippet programmed in LuaLaTeX, a new command \capme takes the argument, uses Lua and converts preselected characters via regular expression and unicode.utf8.gsub function to a new command \colorme. I tried to implement change of common words as well as marked one (words preceeded by a { letter, e.g. \textit{Ňouma}). I have extended a common series of uppercase letters (A to Z) by some letters with diacritics to demonstrate its additional feature.

I have found a way how to disobey \noexpand command I frequently used in my previous LuaTeX examples. I am extending the individual commands in a local group, in this example it is the \textit command. However, it would need more complex example and data if this is the right way how to save typing time.

We run lualatex mal-capitalization.tex, I enclose the file and a preview of the result.

% lualatex mal-capitalization.tex
\documentclass[a4paper]{article}
%\usepackage{fontspec} % It also loads Latin Modern.
%\usepackage{luacode}
\pagestyle{empty}
\usepackage{xcolor}

\begin{document}
\begin{luacode*}
selection="[%uČĎŇŘŠŤŽ]" -- the characters we consider to be coloured
function capme(text)
text=" "..text -- the first letter of a string is also considered for change
text=unicode.utf8.gsub(text, "([%s{])("..selection..")", "%1\\colorme{%2}")
text=unicode.utf8.sub(text,2) -- delete that extra space
print(text) -- a new text written to the terminal
tex.print(text) -- deliver the result back to TeX
end -- function capme
\end{luacode*}

\def\capme#1{%
% We neither need \noexpand\textit nor \noexpand{#1} and \\textit.
\begingroup % Let's redefining begin!
\let\oldtextit=\textit
\def\textit##1{\noexpand\oldtextit{##1}}
\directlua{capme([[#1]])}
\endgroup % \textit returns to its common definition.
}
\def\colorme#1{{\color{red}\bfseries#1}}

% A small demonstration of using it.
Hello World! \capme{Hello Capitalized WORLD!}
Several words with diacritics: \capme{Čenich. \textit{Ňouma.}
Ďagilev. Šiška. Živočich.} I'm back in \textit{common} text.
\end{document}
`

-