答案1
您可以使用path picture
和path picture bounding box
:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
[path picture={
\fill[gray!30](path picture bounding box.north west)rectangle
($(path picture bounding box.north east)!.75!(path picture bounding box.south east)$);
}](n)
{Some Text}
;
\end{document}
如果节点是以下情况,这也有效circle
:
或cloud
(选项:cloud,cloud ignores aspect
,需要库shapes.symbols
):
仅当节点是矩形时,才可以使用节点名称代替path picture bounding box
:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
[path picture={
\fill[gray!30](n.north west)rectangle($(n.north east)!.75!(n.south east)$);
}](n)
{Some Text}
;
\end{document}
更新(因为一条评论)
如果背景颜色应按一定间隔着色,例如从 0.3 到 0.8,则使用
($(<name>.north west)!.3!(<name>.south west)$)
rectangle
($(<name>.north east)!.8!(<name>.south east)$);
替换<name>
为节点名称或path picture bounding box
。
代码:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikz
\node[draw]
[path picture={
\fill[gray!30]
($(path picture bounding box.north west)!.3!(path picture bounding box.south west)$)
rectangle
($(path picture bounding box.north east)!.8!(path picture bounding box.south east)$);
}](n)
{Some Text}
;
\end{document}
答案2
在单独的步骤中进行填充可能是一种选择。
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc,backgrounds,scopes}
\begin{document}
\begin{tikzpicture}
\node[draw] (a) {Some Text};
{ [on background layer]
\fill[gray!30] (a.north west) rectangle ($(a.south east)!0.3!(a.north east)$);
}
\end{tikzpicture}
\end{document}
答案3
我建议做类似的事情:阴影
\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\tikz\node[draw, top color=gray!30,bottom color=white]{Some Text};
\end{document}
编辑
由于是要求的,这里有一个带有褪色的样本;我将背景设置为橙色,以使效果明显
\documentclass{article}
\usepackage{pagecolor}
\usepackage{tikz}
\pagecolor{yellow!30!orange}
\usetikzlibrary{fadings}
\begin{document}
\tikzfading[name=fade down,
top color=transparent!0,
bottom color=transparent!100]
\begin{tikzpicture}[path fading=fade down]
\node[draw, fill=gray!30,path fading]{Some Text};
\end{tikzpicture}
\end{document}
结果: