Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tikz: getting width of node programmatically

2,533 views
Skip to first unread message

Marc van Dongen

unread,
Jul 8, 2009, 7:17:31 AM7/8/09
to
Howsagoin,


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

Marc van Dongen

unread,
Jul 9, 2009, 4:58:59 AM7/9/09
to
On Jul 8, 12:17 pm, Marc van Dongen <don...@cs.ucc.ie> wrote:

[ 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

Samuel DM

unread,
Jul 30, 2009, 9:44:53 AM7/30/09
to

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!

Marc van Dongen

unread,
Jul 31, 2009, 3:04:33 AM7/31/09
to
On Jul 30, 2:44 pm, Samuel DM <sder...@gmail.com> wrote:

> 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

Samuel DM

unread,
Aug 1, 2009, 12:16:37 PM8/1/09
to

Thank you very much.

Samuel DM

0 new messages