如何在样式定义中进行部分绘制?
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{%
myrect/.style={%
minimum width=2cm, minimum height=1cm, fill=olive!80
}
}
\begin{tikzpicture}
\node[myrect] (R) at (0, 0){};
\draw[red] (R.north west)--(R.north east);
\end{tikzpicture}
\end{document}
答案1
其中一种可能性是使用path picture={...}
:
\documentclass[tikz, border=1cm]{standalone}
\newcommand\ppbb{path picture bounding box}
\begin{document}
\tikzset{%
myrect/.style={%
minimum width=2cm, minimum height=1cm, fill=olive!80,
path picture={%
\draw[thick,red] (\ppbb.north west) -- (\ppbb.north east);
},
}
}
\begin{tikzpicture}
\node[myrect] (R) at (0, 0){};
\end{tikzpicture}
\end{document}