2

How can I programmatically find the height and width of a node?

\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}

I would like to set the height and width node01 identical to node00.

untitled
  • 79
  • 4

2 Answers2

2

You can use \pgfgetlastxy or \pgfextractx and \pgfextracty. One uses macros and the other uses length registers.

\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}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 1
    This makes node01 bigger than node00 since node00's anchors include the outer seps while the outer seps are added to the minimum widths and heights specified for node01. – Qrrbrbirlbel May 06 '23 at 09:10
  • @Qrrbrbirlbel - Close, but it is not due to inner sep or rounded corners, but the line thickness. – John Kormylo May 06 '23 at 12:22
0

You might also be interested by this solution that computes it by computing the distance between the north/east anchors, subtracting the width of the line, directly using standard operations on paths:

TikZ: Get width of node that shouln't be drawn

tobiasBora
  • 8,684