我需要知道 TikZ 节点的高度并将其存储在长度中,以便以后其他命令可以使用它并引用这个精确的动态长度。
所以这里有一个 MWE 来说明我想要做的事情,但是当然这不是\totalheightof
工作原理,它测量文本的高度,但我想获取节点的高度:
\documentclass{article}
\newlength{\heightOfMyNode}
\usepackage{tikz,calc}
\begin{document}
\begin{tikzpicture}
\node(mynode)[text width=3cm]{This is a node that varies in height and I need to know the hieght and store it in a length};
\end{tikzpicture}
\setlength{\heightOfMyNode}{\totalheightof{node.height}}
And here I want to use the length for a \rule{\heightOfMyNode}{2pt}
\end{document}
期待您的建议,特别是我忽略的直接 TikZ/PGF 解决方案,也许有\veclen
?
答案1
好吧,我假设你只想知道包含可能被变换的节点的框的宽度和高度。然后很容易计算边界框的宽度和高度。
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fit, calc}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand {\getnodedimen} {O{\nodewidth} O{\nodeheight} m} {
\begin{pgfinterruptboundingbox}
\begin{scope}[local bounding box=bb@temp]
\node[inner sep=0pt, fit=(#3)] {};
\end{scope}
\path ($([email protected] east)-([email protected] west)$);
\end{pgfinterruptboundingbox}
\pgfgetlastxy{#1}{#2}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node(mynode)[text width=3cm, draw=red]{This is a node that varies in height and I need to know the hieght and store it in a length};
\getnodedimen{mynode}
\node[draw, minimum height=\nodeheight, minimum width=\nodewidth] at (4, 0) {};
\end{tikzpicture}
\end{document}