节点创建后如何填充框?

节点创建后如何填充框?

testbox使用nodetikz 创建了一个,如下所示:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \node[draw, regular polygon, regular polygon sides = 6] (testbox) {};
  % \fill [red] (testbox.south east) rectangle (testbox.north west);
  % Thanks @Symbol1 for providing this method, but I didn't make my question clear.
  % Actually I want to fill a 6-side regular polygon
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果testboxnode由于某些原因必须先(我知道我可以使用\node[fill = red])?

答案1

您可以保存节点路径以供日后使用。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,backgrounds}
\begin{document}
\begin{tikzpicture}
    \node[draw, regular polygon, regular polygon sides = 6,
        save path=\bestpath] (best) {bestagon};
    \scoped[on background layer]% this line not needed if no text to show
    \fill[red, use path=\bestpath];
\end{tikzpicture}

\end{document}

带有文字“bestagon”的红色六边形

答案2

这是一个解决方案:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \node[draw, regular polygon, regular polygon sides = 6] (testbox) {};
  \path[fill=red,draw]  (testbox.corner 1)
  foreach \cornernum in {2,...,6} {-- (testbox.corner \cornernum)} -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容