我想遮蔽一个节点和节点的边界。
以下代码可以工作,但我不太喜欢必须在背景层上的节点后绘制一些东西。我更喜欢可以在样式中使用并直接在节点上使用的解决方案。有人有什么好主意吗?
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}
\tikzset{
shrink inner sep/.code={
\pgfkeysgetvalue{/pgf/inner xsep}{\currentinnerxsep}
\pgfkeysgetvalue{/pgf/inner ysep}{\currentinnerysep}
\pgfkeyssetvalue{/pgf/inner xsep}{\currentinnerxsep - 0.5\pgflinewidth}
\pgfkeyssetvalue{/pgf/inner ysep}{\currentinnerysep - 0.5\pgflinewidth}
}}
\begin{document}
\begin{tikzpicture}
\draw (0,0) node[rectangle,
rounded corners,
shading=axis,
thick,
outer sep=0pt,
shrink inner sep,
left color=red!50!white,
right color=green!50!white
](A){abcabc abc};
\begin{scope}[on background layer]
\shade[rectangle,
left color = red,
right color= green,
thick,
rounded corners=4pt+\pgflinewidth]
($(A.south west)+(-\pgflinewidth,-\pgflinewidth)$) rectangle
($(A.north east)+(\pgflinewidth,\pgflinewidth)$)
;
\end{scope};
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) node[rectangle,
rounded corners,
fill=red!50!white,
draw=red,
thick
](A){abcabc abc};
\end{tikzpicture}
\begin{tikzpicture}
\draw (0,0) node[rectangle,
rounded corners,
shading=axis,
left color=red!50!white,
right color=green!50!white,
draw=red,
thick
](A){abcabc abc};
\end{tikzpicture}
\end{document}
答案1
这可能是一种可行的方法;已经定义了两种样式:
horizontal shaded border
接收左颜色和右颜色作为参数:horizontal shaded border=red and green
vertical shaded border
接收顶部和底部颜色作为参数:vertical shaded border=blue and orange
代码:
\documentclass[tikz,border=10pt,png]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{
shrink inner sep/.code={
\pgfkeysgetvalue{/pgf/inner xsep}{\currentinnerxsep}
\pgfkeysgetvalue{/pgf/inner ysep}{\currentinnerysep}
\pgfkeyssetvalue{/pgf/inner xsep}{\currentinnerxsep - 0.5\pgflinewidth}
\pgfkeyssetvalue{/pgf/inner ysep}{\currentinnerysep - 0.5\pgflinewidth}
}
}
\tikzset{horizontal shaded border/.style args={#1 and #2}{
append after command={
\pgfextra{%
\begin{pgfinterruptpath}
\path[rounded corners,left color=#1,right color=#2]
($(\tikzlastnode.south west)+(-\pgflinewidth,-\pgflinewidth)$)
rectangle
($(\tikzlastnode.north east)+(\pgflinewidth,\pgflinewidth)$);
\end{pgfinterruptpath}
}
}
},
vertical shaded border/.style args={#1 and #2}{
append after command={
\pgfextra{%
\begin{pgfinterruptpath}
\path[rounded corners,top color=#1,bottom color=#2]
($(\tikzlastnode.south west)+(-\pgflinewidth,-\pgflinewidth)$)
rectangle
($(\tikzlastnode.north east)+(\pgflinewidth,\pgflinewidth)$);
\end{pgfinterruptpath}
}
}
}
}
\begin{tikzpicture}
\draw (0,0) node[rectangle,
rounded corners,
thick,
outer sep=0pt,
shrink inner sep,
left color=red!50!white,
right color=green!50!white,
horizontal shaded border=red and green
](A){abcabc abc};
\draw (2.5,0) node[rectangle,
rounded corners,
thick,
outer sep=0pt,
shrink inner sep,
top color=cyan!50,
bottom color=orange!50,
vertical shaded border=blue and orange
](A){abcabc abc};
\end{tikzpicture}
\end{document}
结果:
代码注释
边框在节点之后绘制,方法是:append after command
;要访问节点的锚点,宏\tikzlastnode
很有用:事情非常类似于 如何修改 TikZ 中的节点以自动在其顶部添加一条线?
答案2
我不知道这算不算一个“好”主意。它有一个很大的缺陷,(我在 Ubuntu 上)我无法在 evince 中正确查看它(褪色不显示),但可以在 okular 中查看它。我无法在 gimp 中对生成的 PDF 进行任何操作,因为褪色没有保留/尊重。我不得不对 okular 进行屏幕截图以获取此图像。这真是令人讨厌。
所以,这不是一个可移植的解决方案。我怀疑是处理/渲染 PDF 的底层库导致了 Linux 上的问题(大概是libpoppler
)。因此,这不太可能成为 Windows 上的问题。
除此之外,我们的想法是使用可以与preaction
样式一起应用的淡入淡出。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\tikzset{
shade border west to east/.style args={#1 to #2}{
preaction={draw, very thick, path fading=east, #1},
preaction={draw, very thick, path fading=west, #2}
},
shade fill west to east/.style args={#1 to #2}{
left color=#1,
right color=#2
}
}
\begin{document}
\begin{tikzpicture}
\node [rounded corners=1ex,
shade fill west to east=red!50 to green!50,
shade border west to east=red to green] {abcabc abc};
\end{tikzpicture}
\end{document}