3

I have short and simple question. How get the size and location of a block in Tikz ?

Example:

\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}
Rariusz
  • 173

1 Answers1

4
\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}

enter image description here

percusse
  • 157,807
  • 1
    This, this right here. This is why TIKZ is literal hellspawn. How are you supposed to know that by setting \p1 to a coordinate, you can access \x1 as the x-value of that coordinate? Who's idea was that?? – rosstex Jul 21 '20 at 06:14