3

Is it possible to change color of all the occurrences of a particular character in entire document ?

This link provide solution for changing color of a "word" but I am looking for changing color of "a character".

Note : A character can be can be inside a word or separate.

9
  • For words, see this: tex.stackexchange.com/questions/248632/… Nov 16 '17 at 17:37
  • awesome, couldn't find that on google search and after scrolling over links, my bad, BTW, thanks!
    – Polish
    Nov 16 '17 at 17:39
  • these solutions work for coloring "word" but if I say I want to change color of every occurrence of a character like each and every occurrence of character a than it doesn't works for that, any pointers on that ?
    – Polish
    Nov 16 '17 at 18:07
  • Why don't you edit the question to hone in on highlighting letters instead of words. Then perhaps we can get it re-open. Nov 16 '17 at 18:12
  • 1
    thanks a lot! that worked. btw which language is this(on which you wrote code) ?
    – Polish
    Nov 16 '17 at 18:36
5

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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