我有一个简短而简单的问题。如何获取 Tikz 中块的大小和位置?
例子:
\begin{document}
\tikzstyle{block} = [draw, fill=white!10, rectangle]
\begin{center}
\begin{tikzpicture}[auto,>=latex']
\node [block, minimum width=1cm, minimum height=1cm] (TestBlock)
{
This is simple text
};
\end{tikzpicture}
\end{center}
\end{document}
答案1
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,calc}
\tikzset{block/.style={draw, fill=white!10, rectangle}}
\begin{document}
\begin{tikzpicture}[auto,>=latex']
\node [block, minimum width=1cm, minimum height=1cm] (TestBlock) {This is simple text};
\draw[red] let \p1=(TestBlock.east),\p2=(TestBlock.west), \n1={\x1-\x2} in
([yshift=-1mm]TestBlock.south west) -- ++(\n1,0pt) node[midway,below] {this long};
% An alternative which you can hide inside a macro
\pgfpointdiff{\pgfpointanchor{TestBlock}{south east}}{\pgfpointanchor{TestBlock}{north east}}
\pgfmathsetmacro\mytemp{\csname pgf@y\endcsname}
\draw[blue] ([xshift=1mm]TestBlock.south east) -- ++(0pt,\mytemp pt)
node[midway,right] {this high};
\end{tikzpicture}
\end{document}