[Tex/LaTex] Rainbow-colored one letter with tikz and xcolor

colortikz-pgf

I'm trying to define rainbow-color to use with only one letter.
I did some digging. It is possible to give different color each letter ofcource.
But here is what i intend to do:

letter1

letter2

I'm sorry about that i don't have any trial code. I look everywhere, yet i don't know where to begin. Can you help?

Best Answer

Collecting from some other questions: Making a strechable shading and How to shade a text

We can define a strechable rainbow shading and apply it to a text:

enter image description here

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{fadings, shadings}
\newcounter{fadcnt}\setcounter{fadcnt}{0}
\newcommand\fadingtext[3][]{%
\stepcounter{fadcnt}
  \begin{tikzfadingfrompicture}[name=fading letter\thefadcnt]
    \node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
  \end{tikzfadingfrompicture}%
  \begin{tikzpicture}[baseline=(textnode.base)]
    \node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}}; 
    \shade[path fading=fading letter\thefadcnt,#2,fit fading=false]
    (textnode.south west) rectangle (textnode.north east);% 
  \end{tikzpicture}% 
}
\usetikzlibrary{calc}
\newbox\shbox
\tikzset{%
  path picture shading/.style={%
  path picture={%
%
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
  {\pgfpointanchor{path picture bounding box}{north east}}%
\pgfgetlastxy\pathwidth\pathheight%
\pgfinterruptpicture%
   \global\setbox\shbox=\hbox{\pgfuseshading{#1}}%
 \endpgfinterruptpicture%
\pgftransformshift{\pgfpointanchor{path picture bounding box}{center}}%
\pgftransformxscale{\pathwidth/(\wd\shbox)}%
\pgftransformyscale{\pathheight/(\ht\shbox)}% \dp will (should) be 0pt
\pgftext{\box\shbox}%
%
    }
  }
}
\pgfdeclarehorizontalshading{rainbow}{10bp}{color(0bp)=(violet);
            color(1.6667bp)=(blue);
            color(3.3333bp)=(cyan);
            color(5bp)=(green);
            color(6.6667bp)=(yellow);
            color(8.3333bp)=(orange);
            color(10bp)=(red)}
\begin{document} 
 \fadingtext[scale=10, font=\bfseries]{upper left=red, upper right=green, lower left=blue,lower right=yellow}{\LaTeX}

\fadingtext[scale=10, font=\bfseries]{path picture shading=rainbow}{\LaTeX}

\fadingtext[scale=10, font=\bfseries]{path picture shading=rainbow}{X}
\end{document}