我想将颜色渐变设置为“仅所需文本”,但我希望能够将其放置在 内tikzpicure
。我已经检查过了此主题描述一种将渐变颜色添加到章节标题的方法。
问题是:我无法自由地将这样的节点放置在tikzpicure
环境中。
以下是我尝试过的 MWE:
\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}
这样的代码成功地将颜色渐变添加到我的文本中。
但是,当我尝试将后者放置在tikzpicture environment
节点内部的某个位置时,例如:
\node [text=gray!50,inner sep=0pt,outer sep=0pt] (textnode) at (page cs:0,0)
{\bfseries Piece of text to be colored};
似乎只有文本部分移动,而颜色渐变没有移动,我得到了这个:
我做错了什么?我怎样才能自由地将这种颜色渐变的文本放在里面tikzpicture
?
答案1
我不知道 OP 的代码有什么问题,但是使用马克·维布罗 \shadetext
a 里面的命令tikzpicture
似乎有效。
以下代码包含\shadetex
命令作为node's
文本部分。这样就tikzpicture
可以使用绝对定位。第三个示例表明,已定义的current page
节点允许获得与 OP 的新坐标页面系统类似的结果。
\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}