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.
Asked
Active
Viewed 238 times
2
-
To the extent that you are trying to distinguish vowels from consonants, a simple version will have problems with 'y'. – Alan Munn Nov 23 '13 at 20:03
-
XeTeX can do that. Let me find the reference – Thruston Nov 23 '13 at 20:04
-
See p.14 in http://mirror.ox.ac.uk/sites/ctan.org/info/xetexref/xetex-reference.pdf Is that what you need? – Thruston Nov 23 '13 at 20:08
-
Related (if not duplicate) question: Color all vowels differently in a LaTeX document – Alan Munn Nov 23 '13 at 20:16
-
In fact, you seem to have the same user name. Why is the answered question not sufficient for you? – Alan Munn Nov 23 '13 at 20:29
-
What's different (slightly) is the background colour. The xetex charclass solution in the referenced question changes foreground colours. – Thruston Nov 23 '13 at 20:53
2 Answers
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}

Read the XeTeX reference for details of what \XeTeXinterchartoks does.
-
Note that the word boundary class currently has the number 4095 (since 2016) instead of 255. – Marijn Mar 10 '23 at 22:07
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}
