2

This question already has an answer here:

Is it possible to give the letters a,e,i,o,u,y a different background color in a piece of text by using a loop or package? For instance, give the a's a red background, the e's a blue background, etc. Without changing the background of the other letters in the text.

marked as duplicate by Alan Munn, Martin Schröder, Peter Jansson, egreg, Sean Allred Nov 23 '13 at 22:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Adapting this answer, here is a way to use the same XeTeX extensions to put in background colours. I've only done "a" and "e" but it could obviously be extended.

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

\XeTeXinterchartokenstate = 1
\newXeTeXintercharclass \tta \XeTeXcharclass `\a \tta
\newXeTeXintercharclass \tte \XeTeXcharclass `\e \tte
%
\setbox2=\hbox{a}
\XeTeXinterchartoks    0 \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tta \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tte \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks  255 \tta = {\rlap{\textcolor{green}{\vrule height \ht2 width \wd2 depth \dp2}}}
\setbox2=\hbox{e}
\XeTeXinterchartoks    0 \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tta \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks \tte \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}
\XeTeXinterchartoks  255 \tte = {\rlap{\textcolor{orange}{\vrule height \ht2 width \wd2 depth \dp2}}}

The awful weary green fox jumped over the lazy dog.

\end{document}

enter image description here

Read the XeTeX reference for details of what \XeTeXinterchartoks does.

1

Combining the answers to two questions yields a simple solution:

Leo Liu's answer to: - soul: broken highlighting with xcolor when using \selectcolormodel

and egreg's answer to: - Color all vowels differently in a LaTeX document

\documentclass{article}
\usepackage{xparse,xcolor}
\usepackage[normalem]{ulem} % use normalem to protect \emph
\newcommand\hl[1]{\bgroup\markoverwith
  {\textcolor{#1}{\rule[-.5ex]{2pt}{2.5ex}}}\ULon}
  \ExplSyntaxOn
\NewDocumentCommand{\colorize}{mm}
 {
  \cs_set:cpn { maryjane_color_#1: } { \hl{#2}{#1} }
 }
\tl_new:N \l_maryjane_text_tl
\NewDocumentCommand{\changecolors}{ O{aeiou} m }
 {
  \tl_set:Nn \l_maryjane_text_tl { #2 }
  \tl_map_inline:nn { #1 } 
   {
    \tl_replace_all:Nnn \l_maryjane_text_tl { ##1 } { \use:c { maryjane_color_##1: } }
   }
  \l_maryjane_text_tl
 }
\ExplSyntaxOff

\colorize{a}{red}
\colorize{e}{blue}
\colorize{i}{green}
\colorize{o}{yellow}
\colorize{u}{brown!30}

\begin{document}

\changecolors{The quick brown fox jumped over the lazy dog}

\changecolors[a]{The quick brown fox jumped over the lazy dog}

\end{document}

output of code

Not the answer you're looking for? Browse other questions tagged or ask your own question.