3

I want to put a color gradient to a "Desired Text Only" but then I want to be able to position it inside a tikzpicure. I already checked this thread describing a way to put gradient color to a section heading.

Problem is: I am unable to freely place such a node inside a tikzpicureenvironment.

Here is a MWE of what I tried:

\documentclass[12pt,a5paper,landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns}

%******************************************************************
%
% Defining a new coordinate system for the page:
%
% --------------------------
% |(-1,1)    (0,1)    (1,1)|
% |                        |
% |(-1,0)    (0,0)    (1,0)|
% |                        |
% |(-1,-1)   (0,-1)  (1,-1)|
% --------------------------
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
    \parsecomma#1\endparsecomma
    \pgfpointanchor{current page}{north east}
    % Save the upper right corner
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % save the lower left corner
    \pgfpointanchor{current page}{south west}
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    % Transform to the correct placement
    \pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
    \expandafter\pgf@x\expandafter=\pgfmathresult pt
    \pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
    \expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother
%******************************************************************

\begin{document}

\begin{tikzfadingfrompicture}[name=tikz]
    \node [text=transparent!20] {\bfseries Piece of text to be colored};
\end{tikzfadingfrompicture}

\begin{tikzpicture}[remember picture, overlay]
    \node [text=gray!50,inner sep=0pt,outer sep=0pt] (textnode)% 
    {\bfseries Piece of text to be colored};
    \shade[path fading=tikz,fit fading=false,left color=blue,right color=black]%
        (textnode.south west) rectangle (textnode.north east);
\end{tikzpicture}

\end{document}

Such code succeeded in putting a color gradient into my piece of text.

enter image description here

But then, when I try to place the latter somewhere inside the tikzpicture environment adding a position to the node such as:

\node [text=gray!50,inner sep=0pt,outer sep=0pt] (textnode) at (page cs:0,0) 
    {\bfseries Piece of text to be colored};

it seems that only the piece of text moves and not the color gradient and I obtain this :

enter image description here

What am I doing wrong? How can I freely place such color gradient piece of text inside a tikzpicture?

3

I don't know what's wrong with OP's code, but using Mark Wibrow's \shadetext command inside a tikzpicture seems to work.

enter image description here

Following code includes \shadetex commands as node's text part. This way tikzpicture absolute positioning can be used. Third example shows that already defined current page node allows to obtain similar results than OP's new coordinate page system.

\documentclass[12pt,a5paper,landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns}

%******************************************************************
%
% Defining a new coordinate system for the page:
%
% --------------------------
% |(-1,1)    (0,1)    (1,1)|
% |                        |
% |(-1,0)    (0,0)    (1,0)|
% |                        |
% |(-1,-1)   (0,-1)  (1,-1)|
% --------------------------
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
    \parsecomma#1\endparsecomma
    \pgfpointanchor{current page}{north east}
    % Save the upper right corner
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % save the lower left corner
    \pgfpointanchor{current page}{south west}
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    % Transform to the correct placement
    \pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
    \expandafter\pgf@x\expandafter=\pgfmathresult pt
    \pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
    \expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother
%******************************************************************

\newcommand\shadetext[2][]{%
  \setbox0=\hbox{{\special{pdf:literal 7 Tr }#2}}%
  \tikz[baseline=0]\path [#1] \pgfextra{\rlap{\copy0}} (0,-\dp0) rectangle (\wd0,\ht0);%
}

\begin{document}
\begin{tikzpicture}[remember picture, overlay]
    \node at (page cs:0,0) {\shadetext[left color=blue, right color=black]{\bfseries Piece of text to be colored}};

    \node[anchor=north east] at (page cs:1,1) {\shadetext[left color=red, right color=yellow]{\bfseries Piece of text to be colored}};

    \node[anchor=south west] at (current page.south west) {\shadetext[left color=green, right color=purple]{\bfseries Piece of text to be colored}};
\end{tikzpicture}
\end{document}

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.