我需要一个两种颜色的单个字符。考虑到我可以使用shadings
TikZ 库,我遵循了手册(第 23.4.1 节,TikZ 示例):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,shadings}
\begin{document}
\begin{tikzfadingfrompicture}[name=mixed 4]
\node[text=transparent!0] {$4$};
\end{tikzfadingfrompicture}
\fbox{\tikz[scale=10]\shade[path fading=mixed 4,top color=orange,bottom color=purple]
(0,0) rectangle (2ex,2ex);}
\end{document}
然而,结果并不好,原因有二:
- 周围的框太大了,不可能使用公式文本中的字符。
- 角色仅可见阴影的中心部分,因此几乎不可见。
如何使阴影应用仅有的到角色,没有任何周围空间?
如果更容易的话,我也可以不用阴影,而是进行颜色的硬性改变,就像这样(gimped):
分离线的角度并不是那么重要。
答案1
以下是另一个建议:
\documentclass[varwidth,margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newcommand\fadingtext[3][]{%
\begin{tikzfadingfrompicture}[name=fading letter]
\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,#2,fit fading=false]
(textnode.south west) rectangle (textnode.north east);%
\end{tikzpicture}%
}
\begin{document}
\setlength\fboxsep{0pt}
\fbox{%
\fadingtext[scale=10]{top color=orange,bottom color=purple}{$4$}%
}
\end{document}
您可以使用它来为文本着色:
代码:
\documentclass[varwidth,margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newcommand\fadingtext[3][]{%
\begin{tikzfadingfrompicture}[name=fading letter]
\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,#2,fit fading=false]
(textnode.south west) rectangle (textnode.north east);%
\end{tikzpicture}%
}
\usepackage{lipsum}
\begin{document}
\setlength\fboxsep{0pt}
\fbox{%
\fadingtext[scale=4]{top color=orange,bottom color=purple}{$4$}%
}
A short test with green and purple: \fadingtext{left color=green,right color=purple}{green to purple}
\noindent\fadingtext{top color=blue,bottom color=red,middle color=green!80!black}{\parbox[b]{\linewidth}{\strut\lipsum[1]}}
\end{document}
以下是两种颜色之间清晰边界的附加建议:
代码:
\documentclass[margin=5mm,varwidth]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{lipsum}
\tikzset{
bctleft/.style={.},
text left/.style={bctleft/.append style={#1}},
bctright/.style={.},
text right/.style={bctright/.append style={#1}},
}
\newcommand\bicolortext[2][]{%
\tikz[baseline=(n.base),inner sep=0pt,outer xsep=0pt,#1]{
\node(n){\phantom{#2}};
\foreach \a/\c in {north west/bctleft,south east/bctright}{
\begin{scope}
\clip(n.south west)--(n.\a)--(n.north east)--cycle;
\node[\c]at(n){#2};
\end{scope}
}}}
\begin{document}
\tikzset{text left=orange,text right=purple}
\setlength\fboxsep{0pt}
\fbox{%
\bicolortext[scale=4,transform shape]{$4$}%
}
A short test with green and purple: \bicolortext[text left=green, text right=purple]{green to purple}
\noindent\bicolortext{\parbox[b]{\linewidth}{\strut\lipsum[1]}}
\end{document}
使用 TikZ 3.0 版,您也可以使用该transparency group=knockout
选项。然后就可以对角线进行阴影处理。但据我所知,只有 Acrobat Reader 可以显示正确的结果,因为只有此查看器才能处理透明度。
代码:
\documentclass[varwidth,margin=5mm]{standalone}
\usepackage{tikz}
\newcommand\fadingtext[3][]{%
\begin{tikzpicture}[baseline=(textnode.base)]
\node[shade,#2,inner sep=0pt,outer sep=0pt,#1,shading angle=45](textnode){\phantom{#3}};
\begin{scope}[transparency group=knockout]
\fill[white](textnode.south west)rectangle(textnode.north east);
\node[opacity=0,inner sep=0pt,outer xsep=0pt,#1]{#3};
\end{scope}
\end{tikzpicture}%
}
\usepackage{lipsum}
\begin{document}
\setlength\fboxsep{0pt}
\fbox{\fadingtext[scale=10]{top color=orange,bottom color=purple}{$4$}}
A short test with green and purple: \fadingtext{left color=green,right color=purple}{green to purple}
\noindent\fadingtext{top color=blue,bottom color=red,middle color=green!80!black}{\parbox[b]{\linewidth}{\strut\lipsum[1]}}
\end{document}
答案2
另一种解决方案是利用 PDF 规范的文本渲染模式将文本渲染为剪切路径:
\documentclass[border=5]{standalone}
\usepackage{tikz}
\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}
Some
\shadetext[left color=yellow, right color=red, middle color=purple, shading angle=45]{\Large\bfseries shaded}
text
\end{document}
虽然它使用了 PDF 特效,但这种方法的优点在于它很容易扩展以使用任意图片:
\documentclass[border=5]{standalone}
\usepackage{tikz,lipsum}
\newbox\textpicturebox
\tikzset{text picture/.style={%
path picture={%
\pgfmathsetlengthmacro\textpicturewidth{\wd\textpicturebox}%
\pgfmathsetlengthmacro\textpictureheight{\ht\textpicturebox+\dp\textpicturebox}%
\pgftransformshift{\pgfpointanchor{path picture bounding box}{center}}%
#1}}}%
\newcommand\shadetext[2][]{%
\setbox\textpicturebox=\hbox{{\special{pdf:literal 7 Tr }#2}}%
\tikz[baseline=0]\path \pgfextra{\rlap{\copy\textpicturebox}} [#1] (0,-\dp\textpicturebox) rectangle (\wd\textpicturebox,\ht\textpicturebox);
}
\begin{document}
\shadetext[fill=black, text picture={
\tikzset{shift=(90:2)}
\fill [green!75!brown] (-0.25,0) rectangle (0.25,-7);
\fill [green!75!brown] (0,-7) [rotate=-45] arc (270:-90:0.5 and 2);
\fill [green!75!brown] (0,-7) [rotate=45] arc (270:-90:0.5 and 2);
\foreach \i [evaluate={\o=mod(\i/30,2)*100;}] in {0,30,...,330}
\fill [red!\o!pink]
(0,0) -- (\i:3) .. controls ++(\i:1cm) and ++(\i+30:1) .. (\i+30:3) -- cycle;
\fill [yellow] circle [radius=1.5];
}]{\Large\bfseries\begin{minipage}{4in}\lipsum[1]\end{minipage}}
\end{document}
答案3
这是一个使用 的解决方案pstricks
,更准确地说是使用pst-grad
和 pst-text
。它可以与 pdf LaTeX 编译,并且可以使用 更改框大小\psframebox[framesep=…]{…}
:
\documentclass[pdf, x11names]{article}
\usepackage{pst-grad,pst-text}
\psset{framesep=2pt}
\begin{document}
Some text
\begin{tabular}{lll}
\psframebox[linecolor = Coral1!20]{%
\pscharpath[linestyle=none,%
fillstyle=gradient,gradend=IndianRed4,gradbegin=IndianRed1,
gradmidpoint=1]{\LARGE\bfseries 4}}
& \psframebox[linecolor = Coral1!20]{%
\pscharpath[linestyle=none,%
fillstyle=gradient,gradend=IndianRed4,gradbegin=IndianRed1,
gradmidpoint=1]{\Large\bfseries 4}}
& \psframebox[linecolor = Coral1!20]{%
\pscharpath[linestyle=none,%
fillstyle=gradient,gradend=IndianRed4,gradbegin=IndianRed1,
gradmidpoint=1]{\bfseries 4}}
\end{tabular}
\end{document}