Perhaps
\newcommand{\indexthis}[1]{\textcolor{Maroon}{\textbf{#1}}\lowercase{\index{#1}}}
(deleted xspace as it is not needed here as the macro call ends with }
so does not drop spaces)
You can do it with a regular expression, if you have the input string as an argument.
\documentclass{article}
\usepackage{xparse}
%\usepackage{l3regex} % only with expl3 before June 2017
\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 Begin group
\cE\}
means “a closing brace with its normal function of End group
Thus the replacement text can be read as
\textcolor{#1}{<match>}
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.
\documentclass{article}
\usepackage{xparse}
\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
\tl_set:Nn \l__sheljohn_colorcap_input_tl { #2 }
\regex_replace_all:nnN
% search a capital letter (or more)
{ ([A-Z]+|\cC.\{?[A-Z]+\}?) }
% replace the match with \textcolor{#1}{<match>}
{ \c{textcolor}\cB\{#1\cE\}\cB\{\1\cE\} }
\l__sheljohn_colorcap_input_tl
\tl_use:N \l__sheljohn_colorcap_input_tl
}
\ExplSyntaxOff
\begin{document}
\colorcap{\`Once \r{U}pon a Time}
\colorcap[red]{Once Upon a Time}
\end{document}
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.
Best Answer
If you want a capital È with the accent grave, you use the backtick character (`), not the apostrophe character (').
The backtick is located in the upper left corner of your keyboard (assuming QWERTY) with the tilde (~), above the TAB and below the ESC keys.
The code should look like this:
Not this: