9

I was looking to apply a color gradient to some text (i.e. Chapter / section headings) and I searched through the Internet but was unable to find a minimum working example. Is it something that can be done in XeLaTeX?

I decided to ask here, thinking someone in the community might have already done similar to that.

lockstep
  • 250,273
Aku
  • 11,026

1 Answers1

13

The following example comes from the TikZ 2.10 manual, p237, and seems to do what you describe:

\begin{tikzfadingfrompicture}[name=tikz]
  \node [text=transparent!20]
    {\fontfamily{ptm}\fontsize{45}{45}\bfseries\selectfont Ti\emph{k}Z};
\end{tikzfadingfrompicture}
% Now we use the fading in another picture:
\begin{tikzpicture}
  \fill [black!20] (-2,-1) rectangle (2,1);
  \pattern [pattern=checkerboard,pattern color=black!30] (-2,-1) rectangle (2,1);
  \shade[path fading=tikz,fit fading=false, left color=blue,right color=black]
    (-2,-1) rectangle (2,1);
\end{tikzpicture}

Adapting this somewhat, you can achieve the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns}
\begin{document}
\newsavebox{\tempbox}
\newcommand\tikzsection[1]{%
  \begin{tikzfadingfrompicture}[name=tikzsection]
    \node [text=white] {\normalfont \Large \bfseries #1};
  \end{tikzfadingfrompicture}
  \begin{lrbox}{\tempbox}%
    \begin{tikzpicture}
      \node [text=white,inner sep=0pt,outer sep=0pt] (textnode) {\normalfont \Large \bfseries #1};
      \shade[path fading=tikzsection,fit fading=false,left color=blue,right color=black]
      (textnode.south west) rectangle (textnode.north east);
    \end{tikzpicture}%
  \end{lrbox}
  % Now we use the fading in another picture:
  \section{XX \usebox\tempbox{} XX}%
}
\tikzsection{First section}
Some text
\tikzsection{Second section}
Some text
\end{document}

which produces

screenshot of faded text

Ben Lerner
  • 7,082