Here, I extend my answer at Highlight every occurrence of a list of words? to address both the highlighting of whole words as well as individual letters.
First, we have a macro \onlywords (default T) that tells the macro \colorize{} to operate only on whole words (meaning that foo would not be colorized as part of the word xfoox). If \onlywords is set otherwise to F, then a search for foo would be colorized as part of xfoox.
Search letters/strings or words are set with \setcolor{<string>}{<color>}.
The search strings can be reset with an invocation of \resetcolorize.
I should note that capitalization proves to be no impediment to the string search so that Baz flags as a positive match for baz. Further, for whole word searches, punctuation is screened out so that while xfoox will not be flagged when searching for the whole word foo. (foo) and meta-foo on the other hand will be properly located as a foo word.
In the MWE below, in the first paragraph, I search only for the whole words
\setcolor{foo}{red}
\setcolor{bar}{blue!70}
\setcolor{baz}{cyan}
\setcolor{biz}{green!70!black}
while in the second paragraph, I search for intraword letters
\setcolor{a}{red}
\setcolor{e}{orange!90!black}
\setcolor{i}{cyan}
\setcolor{o}{green!85!black}
\setcolor{u}{blue!20!blue}
Here is the MWE:
\documentclass{article}
\usepackage{listofitems,xcolor}
\def\onlywords{T}
\def\onlywordstrue{T}
\newcounter{colorwords}
\newcommand\colorize[1]{%
\expandafter\setsepchar\expandafter{\theparselist/ /,||.||!||?||;||:||-||(||)||[||]}%
\reademptyitems%
\greadlist\thewords{#1}%
\foreachitem\x\in\thewords[]{%
\x%
\ifnum\xcnt<\listlen\thewords[]\relax%
\if\relax\thewords[\xcnt,-1,-1]\relax%
\if\relax\thewords[\the\numexpr\xcnt+1,1,1]\relax%
\textcolor{\csname\thewordssep[\xcnt]color\endcsname}{\thewordssep[\xcnt]}%
\else%
\conditionalcolor{\xcnt}%
\fi%
\else%
\conditionalcolor{\xcnt}%
\fi%
\fi%
}%
}
\newcommand\conditionalcolor[1]{%
\ifx\onlywords\onlywordstrue%
\thewordssep[#1]%
\else%
\textcolor{\csname\thewordssep[#1]color\endcsname}{\thewordssep[#1]}%
\fi%
}
\def\theparselist{}
\makeatletter
\newcommand\setcolor[2]{%
\stepcounter{colorwords}%
\ifnum\value{colorwords}=1\g@addto@macro\theparselist{#1}\else%
\g@addto@macro\theparselist{||#1}\fi
\expandafter\def\csname#1color\endcsname{#2}%
\edef\thestring{\Capitalize#1\relax}%
\g@addto@macro\theparselist{||}
\expandafter\g@addto@macro\expandafter\theparselist\expandafter{\thestring}
\expandafter\def\csname\thestring color\endcsname{#2}%
}
\makeatother
\def\Capitalize#1#2\relax{%
\ifcase\numexpr`#1-`a\relax
A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or K\or L\or M\or
N\or O\or P\or Q\or R\or S\or T\or U\or V\or W\or X\or Y\or Z\else
#1\fi#2%
}
\newcommand\resetcolorize{\gdef\theparselist{}\setcounter{colorwords}{0}}
\begin{document}
\setcolor{foo}{red}
\setcolor{bar}{blue!70}
\setcolor{baz}{cyan}
\setcolor{biz}{green!70!black}
\colorize{Lorem ipsum dolor foo sit amet bar: consectetuer adipiscing elit baz! Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Baz curabitur baz dictum gravida
mauris. Nam biz arcu libero, nonummy eget, consectetuer id, vulputate a, bar magna.
Donec vehicula augue eu neque. foox xfoo ,foo foo, foo. xfoox meta -foo meta-foo
(foo)-bar.}
\def\onlywords{F}
\resetcolorize
\setcolor{a}{red}
\setcolor{e}{orange!90!black}
\setcolor{i}{cyan}
\setcolor{o}{green!85!black}
\setcolor{u}{blue!70}
\colorize{Lorem ipsum dolor foo sit amet bar: consectetuer adipiscing elit baz! Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Baz curabitur baz dictum gravida
mauris. Nam biz arcu libero, nonummy eget, consectetuer id, vulputate a, bar magna.
Donec vehicula augue eu neque. foox xfoo ,foo foo, foo. xfoox meta -foo meta-foo
(foo)-bar.}
\end{document}

athan it doesn't works for that, any pointers on that ? – Polish Nov 16 '17 at 18:07listofitemspackage, which was written for use in both plain TeX as well as LaTeX. See https://ctan.org/pkg/listofitems for documentation. – Steven B. Segletes Nov 16 '17 at 18:38texas of yet, but I am using sharelatex, and I think its pretty good(does the job). – Polish Nov 17 '17 at 04:38%to the edited line, to prevent an extra space from being inserted. – Steven B. Segletes Nov 17 '17 at 11:01