(updated version after Qrrbrbirlbel's answer: I want to measure tikz nodes)
I'd like to compute the width of the content (tikz multiline node) that I will typeset in a tikz-cd node to adapt the shape of a parent node. However, sbox created inside tikz have zero width, and typesetting a tikzpicture inside it gives me an infinite recursion… any idea why and how to avoid that issue?
MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tikz-cd}
\begin{document}
\newsavebox{\tmpbox}
\NewExpandableDocumentCommand{\myNode}{m}{
\node[inner sep=5pt,fill=green,draw,align=center]{#1};
}
\def\myContentOfInterest{\begin{tikzcd}\myNode{42\and multiple lines};\end{tikzcd}}
\def\myContentOfInterestTikzVersion{\begin{tikzpicture}\myNode{42\and multiple lines};\end{tikzpicture}}
Goal: measure the content of \myContentOfInterest
\sbox\tmpbox{\myContentOfInterest}%
\def\myvalue{\the\dimexpr\the\ht\tmpbox\relax}
Outside tikz: \myvalue
Inside tikz:
\tikzset{
my style/.style={
/utils/exec={%
%%%%% works but too simple:
% \pgfmathheight{"this works but too simple case"}%
% \edef\myvalue{\pgfmathresult pt}%
%%%%% fails (infinite computation):
% \pgfmathheight{"\noexpand\myContentOfInterestTikzVersion"}%%
% \edef\myvalue{\pgfmathresult pt}%
%%%%% fails: issue with infinite recursion \pgf@selectfontorig ->\pgf@selectfontorig
% \sbox\tmpbox{\myContentOfInterestTikzVersion}%
\def\myvalue{\text{What should I do to measure the final node itself?}}
},
}
}
\begin{tikzcd}[font={\fontsize{10}{12}\selectfont}]
\node[my style]{\myvalue};
\end{tikzcd}
\end{document}
width,depthandheightinstead and save yourself the hassle. (They do exactly that, they typeset a box with the right font and measure it.) You will need\selectfont(for LaTeX) at the start, it seems. – Qrrbrbirlbel Sep 26 '23 at 14:30\the\ht\tmpboxsuffices? And\ht\tmpboxdoesn't need\thein front of it inside a\numexpr, by the way. – egreg Sep 26 '23 at 14:48\pgfmathheightgives a nice result… but unfortunately it fails for multiline content (ideally I’d like to measure the width of a tikz node directly). I updated my MWE. Any idea how to make it work here as well? – tobiasBora Sep 26 '23 at 14:52\theis useless, thanks! – tobiasBora Sep 26 '23 at 14:54discard nodekey. Give the node a name and then you can measure all the things with it you need. This will create the node the same as without it and TikZ has the same control over it as usual. (I can't say anything about the box's dimension, don't trust it.) Adding keyoverlayto the node might be advisable. 2. There's also Deferred Node Positioning. Have never used it, seems powerful, might be even better.tikzpicture. That interrupts the picture environment (see @Qrrbrbirlbel comment below), but it obviously doesn't give the node's dimensions, but only the dimensions of the content, possibly set in a different font or whatever. The code is originally from https://tex.stackexchange.com/a/56405. (The code I'm using may be modified - I don't remember.) But I'm not sure if that's what you need here? – cfr Sep 27 '23 at 11:07