如何以编程方式找到节点的高度和宽度?

如何以编程方式找到节点的高度和宽度?

如何以编程方式找到节点的高度和宽度?

\usepackage{twemojis}

\begin{tikzpicture}
    \tikzset{nodestyle/.style={rectangle, rounded corners, draw=black, thick, fill=black!50}}
    \node[nodestyle] (node00) at (0, 0) {{\LARGE\texttwemoji{dollar banknote}}};
    \node[nodestyle, right=0pt of node00] (node01) {};
\end{tikzpicture}

我想将高度和宽度设置node01为相同node00

答案1

您可以使用\pgfgetlastxy\pgfextractx\pgfextracty。一个使用宏,另一个使用长度寄存器。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{twemojis}

\newlength{\mywidth}
\newlength{\myheight}

\newcommand{\getwh}[1]{% #1 = node name (no parens)
  \pgfextractx{\mywidth}{\pgfpointdiff{\pgfpointanchor{#1}{west}}{\pgfpointanchor{#1}{east}}}%
  \pgfextracty{\myheight}{\pgfpointdiff{\pgfpointanchor{#1}{south}}{\pgfpointanchor{#1}{north}}}%
}
  
\begin{document}
\begin{tikzpicture}
    \tikzset{nodestyle/.style={rectangle, rounded corners, draw=black, thick, fill=black!50}}
    \node[nodestyle] (node00) at (0, 0) {{\LARGE\texttwemoji{dollar banknote}}};
    \getwh{node00}% get size based on anchors
    \addtolength{\mywidth}{-0.8pt}% remove line thickness
    \addtolength{\myheight}{-0.8pt}% remove line thickness
    \node[nodestyle, right=0pt, minimum width=\mywidth, minimum height=\myheight] at (node00.east) (node01) {};
\end{tikzpicture}
\end{document}

答案2

您可能还对此解决方案感兴趣,该解决方案通过计算北/东锚点之间的距离,减去线的宽度,直接使用路径上的标准操作来计算:

TikZ:获取不应绘制的节点的宽度

相关内容