Color-highlight selected letters in LaTeX

I intentionally produced my latex leaflet to promote myself as an actor with reduced coloration, but I had an idea to have some letters throughout the text coloured, just to amuse.   My first attempt was to replace all occurances of “et” (French for “and”…) in the text with \textcolor{Red}{et}.  This was fairly quick to do with the power of gVim, and I quite liked the pdf output result, but not the visual mess in the tex file.  I looked for a way to semi-automate it in LaTeX, and found Sh3ljohn’s helpful StackExchange question, which got me thinking that I could colour-highlight these letters throughout my leaflet: J H T j h t.

First attempt, following Segletes’ answer

First I reduced his revised code to highlight just two capitals, M and T:

\let\svmc M \let\svtc T 
%o-\let\svol o
\catcode`M=\active \catcode`T=\active
  \catcode`M=\active\def M{\textcolor{#1}{\svmc}}%
  \catcode`T=\active\def T{\textcolor{#1}{\svtc}}%
%o- \catcode`o=\active\def o{\textcolor{#1}{\svol}}%
  \catcode`M=11\let M\svmc%
  \catcode`T=11\let T\svtc%
%o- \catcode`o=11\let o\svhl%
\catcode`M=11 \catcode`T=11
\parskip 1em
\verb|\colorcapon[color]| will change all caps to the specified color\\
Mostly to Tom (\verb|Even in \Verbatim|)
\verb|\colorcapoff| will restore all things to the original LaTeX setting\\
Mostly to Tom

– which makes this:

Segletes reduced colorization of M & T

The next step was to try lower-case highlighting,  for the letter o I thought (just remove those %o- comment marks from my tex code).  Nope, it doesn’t work: the line \catcode`o=\active breaks all further use of o in the tex file – for example the following line’s \newcommand is interpreted as \newc !  I now realised why he was pointing out that it’s a dangerous method.  It only happens to work for capitals because they’re not being used in subsequent LaTeX commands…

Success with egreg’s answer

I found egreg’s regex answer daunting at first, but having identified why Segletes’ answer can’t work for me, I had a go at stripping down egreg’s code to my requirements.  I don’t understand it all, he’s using packages developed from LaTeX3, but adjusting it turned out to be fairly quick:

\NewDocumentCommand{\colorJHT}{ m }{\JH_colorJHT:nn { #1 }}
\tl_new:N \l__JH_colorJHT_input_tl
\cs_new_protected:Npn \JH_colorJHT:nn #1
  % store the string in a variable for usage with \regex_replace_all:nnN
  \tl_set:Nn \l__JH_colorJHT_input_tl { #1 }
   { ([H J T h j t]+) } % search for one or more occurance of selected letters
   % and now replace the match with \textcolor{#1}{<match>}:
   { \c{textcolor}\cB\{MidnightBlue\cE\}\cB\{\1\cE\} }
  \tl_use:N \l__JH_colorJHT_input_tl
\colorJHT{To Joseph Harriott}
\huge\textcolor{Gray}{\bf \colorJHT{Joseph Harriott}}
\textcolor{Gray}{\colorJHT{Acteur: \bf}}

– which makes this:

h & t's recolored

You can see from the code, that egreg’s NewDocumentCommand nests nicely with other commands.  This is a good enough solution for me.

You can find more LaTeX notes in my DokuWiki.

Share Button