1

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

tobiasBora
  • 8,684
  • 1
    The proper font is not set up because TikZ installs the nullfont inside (so that spaces don't mess things up). You might just want to use the PGFMath functions width, depth and height instead 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
  • (You could also put a rectangular node with no inner and outer seps and no drawn border and measure the node with appropriate tools but I believe the PGFMath functions are enough.) – Qrrbrbirlbel Sep 26 '23 at 14:31
  • 1
    Why that complicated construction when \the\ht\tmpbox suffices? And \ht\tmpbox doesn't need \the in front of it inside a \numexpr, by the way. – egreg Sep 26 '23 at 14:48
  • @Qrrbrbirlbel thanks a lot, \pgfmathheight gives 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
  • @egreg Oh, it comes from simplifying my example to a MWE, I was doing more advanced computation inside. But good to know \the is useless, thanks! – tobiasBora Sep 26 '23 at 14:54
  • @Qrrbrbirlbel what do you mean by "appropriate tools"? I tried to add a node but I get an infinite recursion. – tobiasBora Sep 26 '23 at 15:00
  • 1
  • See discard node key. 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 key overlay to the node might be advisable. 2. There's also Deferred Node Positioning. Have never used it, seems powerful, might be even better.
  • – Qrrbrbirlbel Sep 26 '23 at 15:14
  • 1
    Well measuring text, measuring text inside a node and measuring a node are three different things. Inside a matrix things work a bit different but we have to see whether they interfere her. (A matrix will correct all nodes' positions after the matrix has been constructed because only then the true location of the matrix is known.) – Qrrbrbirlbel Sep 26 '23 at 15:21
  • Why do you want to measure the height of one node inside another node? (If that's what you're trying to do, which it may very well not be as I'm quite confused.) – cfr Sep 27 '23 at 00:59
  • @cfr Because I want to have multi-row/column gates in a tikz matrix, so I need first to measure the final content of the box, compute the width of the inner nodes by basically dividing the width by the number of columns minus column sep, and fit my final node around them. The final result can be seen here, go to section "circuit-related" https://raw.githubusercontent.com/leo-colisson/zx-calculus/main/doc/zx-calculus.pdf – tobiasBora Sep 27 '23 at 08:29
  • 1
    I've got code enabling the measuring of dimensions in the usual way inside a 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
  • @cfr Yeah, its exactly what I was looking for, thanks to you and Qrrbrbirlbel! – tobiasBora Sep 27 '23 at 13:49