For some reason, I need to get the width of a tikz node in
centimetres. The node has a label, and ideally I'd like to have a
macro which returns me the width as a function of the label. I only
intend to use the macro in the tikzpicture that draws the node, and
after the drawing of the node. Is there an easy way to implement such
a macro?
Any help/pointers/suggestions will be greatly appreciated.
Regards,
Marc van Dongen
[ getting width of node ]
Got it. The let feature inside paths in the CVS version of tikz lets
me do the trick.
Regards,
Marc van Dongen
I might be interested in your work. Could you give me a little more
details? I'd like to get the dimensions of what I put in nodes (width
and height) but I don't know how!
> I might be interested in your work. Could you give me a little more
> details? I'd like to get the dimensions of what I put in nodes (width
> and height) but I don't know how!
Hi,
It took me several days to find this workaround and it isn't pretty.
The main reason is that \settowidth and friends don't work as per
usual inside a tikzpicture.
The following solution just draws the node once, measures the width in
points and writes it out in decimals. The key to the solution is the
let operation. (Tested only once and probably requires the tikz
version from CVS.)
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\STUFF}[0]{Blah blah blah}
\newcounter{counterOne}
\newcounter{counterTwo}
\begin{document}
\makeatletter
\begin{tikzpicture}
% Inside tikzpicture \settowidth doesn't work.
\path (0,0)node(label) {\STUFF};
\path
let \p{east} = (label.east),
\p{west} = (label.west) in
let \n{width} = {\x{east}-\x{west}} in
\pgfextra
\pgf@x=\n{width}
\pgf@y=1cm
\divide\pgf@x by \pgf@y
\setcounter{counterOne}{\pgf@x}
\multiply\pgf@x by \pgf@y
\pgf@y=\n{width}
\advance\pgf@y by -\pgf@x
\pgf@x=1cm
\advance\pgf@y by 99\pgf@y
\divide\pgf@y by \pgf@x
\setcounter{counterTwo}{\pgf@y}
\endpgfextra
(label.south) node[anchor=north]
{Width = \thecounterOne.\thecounterTwo\,cm.};
\end{tikzpicture}
\makeatother
\end{document}
In my original solution, I need the width of \STUFF *before* I can do
the actual drawing, as some sizes of other things depend on it. I then
draw these other things, and based on their sizes. draw \STUFF. I
first use \phantom to draw \STUFF hidden, then measure the width, then
draw my other things, and finally draw \STUFF.
Regards,
Marc van Dongen
Thank you very much.
Samuel DM