考虑一下这个MWE:
\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\pagecolor{yellow!15}
\begin{document}
\begin{tikzpicture}
\makeatletter
\pgfdeclareshape{testshape}{ %
\inheritsavedanchors[from={rectangle}] %
\inheritbackgroundpath[from={rectangle}] %
\inheritanchorborder[from={rectangle}] %
\foreach \x in {center,north,north east,north west,south,south east,south west,east,west}{ %
\inheritanchor[from={rectangle}]{\x} %
} %
%\backgroundpath{} %
\foregroundpath{ %
\draw[] (\tikz@[email protected] west) -- (\tikz@[email protected] east)
(\tikz@[email protected] west) -- (\tikz@[email protected] east);
}
}
\makeatother
\tikzstyle{mynode} = [testshape,draw=gray,line width=2pt,inner sep=2pt, outer sep=5pt, minimum width=2cm,minimum height=0pt,align=center]
\node[mynode] (Starter) {Testing the node\\(a bit)};
\node[mynode] (Ender) [right=2cm of Starter] {Also test\\(even more)};
\node[mynode,draw=none] (tester) [below=2pt of Starter] {\phantom{Trying to measure this one}};
\path let \p1=(tester.north east), \p2=(tester.south west)
in coordinate (testerSize) at (\x1-\x2,\y1-\y2);
\pgfpointanchor{testerSize}{center} % "returns"/sets a (last) pgfpoint
\pgfgetlastxy{\testerWidth}{\testerHeight} % ... and globalize:
\global\let\testerWidth\testerWidth
\global\let\testerHeight\testerHeight
\typeout{tester size is: \testerWidth\space X \testerHeight}
\end{tikzpicture}
tester size is: \testerWidth\ X \testerHeight
\end{document}
在这里我尝试tester
以不可见的方式输出节点,因为我试图测量它的最终大小(顺便说一句,有没有比坐标 -> pgfpointanchor -> pgfgetlastxy 更直接的方法?);但是,正如您在输出图像上看到的:
...我真的无法用它隐藏它draw=none
,因为它是一个自定义节点 - 如果我尝试将它“绘制在屏幕外” - 它tikzpicture
只会拉伸并且其尺寸也会改变。
那么,有没有一种方法可以为了测量目的而“不可见地”绘制一个节点及其所有样式,即使它通常是一个自定义形状,而没有这些伪影 - 可以说是 TikZ 节点的“幻影”环境?
答案1
最新 PGF 版本手册的第 101.2.3 节(“延迟节点定位”)中有一组用于创建节点并保存以供日后使用的命令。在这种情况下,您无需对节点执行任何操作,但仍可获取节点的一些属性。
\documentclass[tikz,margin=3pt]{standalone}
\begin{document}
\newbox\mybox
\def\mysaver{%
\pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
\xdef\savednodewidth{\pgfmathresult pt}%
\pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
\xdef\savednodeheight{\pgfmathresult pt}%
}
\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
{
\pgfpositionnodelater{\mysaver}
% This node is clearly outside the grid
\node [minimum width=150pt, minimum height=75pt] at (20,20) {};
}
\node [align=left, fill=black!20, font=\ttfamily] at (1.5,1)
{
\savednodewidth \\
\savednodeheight
};
\end{tikzpicture}
\end{document}
答案2
编辑:截至 2015 年,以下内容不再有效,请参阅如何在 tikz 中拥有一个不可见的图层?
好吧,事实证明这很容易 - 感谢 Tex.SE 的自动建议链接TikZ 图层不可见; 当然,这是一个问题 - 但在这里它却成为了解决方案:)
基本上 - 只需声明一个 pgf 层,但不要设置它 - 它将使您在其上绘制的所有内容都不可见;因此,我得到了输出图像:
...这就是我想要的。代码如下:
\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\pagecolor{yellow!15}
% just declare the layer, and don't do
% \pgfsetlayers ; then it will be invisible:
\pgfdeclarelayer{invisible}
\begin{document}
\begin{tikzpicture}
\makeatletter
\pgfdeclareshape{testshape}{ %
\inheritsavedanchors[from={rectangle}] %
\inheritbackgroundpath[from={rectangle}] %
\inheritanchorborder[from={rectangle}] %
\foreach \x in {center,north,north east,north west,south,south east,south west,east,west}{ %
\inheritanchor[from={rectangle}]{\x} %
} %
%\backgroundpath{} %
\foregroundpath{ %
\draw[] (\tikz@[email protected] west) -- (\tikz@[email protected] east)
(\tikz@[email protected] west) -- (\tikz@[email protected] east);
}
}
\makeatother
\tikzstyle{mynode} = [testshape,draw=gray,line width=2pt,inner sep=2pt, outer sep=5pt, minimum width=2cm,minimum height=0pt,align=center]
\node[mynode] (Starter) {Testing the node\\(a bit)};
\node[mynode] (Ender) [right=2cm of Starter] {Also test\\(even more)};
\begin{pgfonlayer}{invisible}
\node[mynode] (tester) [at=(current bounding box.center)] {Trying to measure this one};
\end{pgfonlayer}
\path let \p1=(tester.north east), \p2=(tester.south west)
in coordinate (testerSize) at (\x1-\x2,\y1-\y2);
% \pgfpointanchor{testerSize}{center} % "returns"/sets a (last) pgfpoint
\path(testerSize.center); % a bit easier than \pgfpointanchor: https://tex.stackexchange.com/a/33706/2595
\pgfgetlastxy{\testerWidth}{\testerHeight} % ... and globalize:
\global\let\testerWidth\testerWidth
\global\let\testerHeight\testerHeight
\typeout{tester size is: \testerWidth\space X \testerHeight}
\end{tikzpicture}
tester size is: \testerWidth\ X \testerHeight
\end{document}