9

In this minimal example

\documentclass{scrartcl} 

\usepackage{calc} 
\usepackage{tikz} 

\newlength{\TestLength} 

\begin{document} 

    \setlength{\TestLength}{\widthof{\tikz \node {bla};}} 
    \showthe\TestLength% MEASUREMENT 1 

    \begin{tikzpicture} 
            \setlength{\TestLength}{\widthof{\node {bla};}} 
            \showthe\TestLength% MEASUREMENT 2 
    \end{tikzpicture} 

\end{document}

\TestLength is calculated correctly in the first time (MEASUREMENT 1), but too small in the second time (MEASUREMENT 2). I'd like to calculate it within a tikzpicture environment, so that I'm fixed to the second way of calculation. is there any workaround?

Thanks a lot for any kind of comments or ideas.

  • What do you need this length for exactly? Most of the times it is enough to give the node a name and use the various anchors of that node. – Caramdir Jan 11 '11 at 19:28
12

If you want to measure the size of a node that has already been typeset and named, you can use the following code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

% \getwidthofnode will measure the width of the node given as its second
% parameter and store it into the first parameter.
\makeatletter
\newcommand\getwidthofnode[2]{%
    \pgfextractx{#1}{\pgfpointanchor{#2}{east}}%
    \pgfextractx{\pgf@xa}{\pgfpointanchor{#2}{west}}% \pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    \addtolength{#1}{-\pgf@xa}%
}
\makeatother

\newlength\TestLength

\begin{tikzpicture}
    \node (mynode) {bla};
    \getwidthofnode{\TestLength}{mynode}
    \showthe\TestLength
\end{tikzpicture}

\end{document}
  • Thanks for the reply. What I actually want to do is to center a bunch of an arbitrary number of nodes with arbitrary shapes and texts on another longish object with defined length. To accomplish this I use an expandable command that is executed in the very end of the code, drawing the nodes on top of the other object. Into this command I put sequentially the code for the nodes. In parallel I want to calculate the overall length of the nodes to calculate their positions. Therefore I would like to calculate the overall length of the nodes before drawing them. – Robert Jan 11 '11 at 23:31
  • @Robert: I see. Then you might be interested in section 75.2.3 “Deferred Node Positioning” of the TikZ v2.10 manual. This describes how to create a node and get its bounding box without placing it immediately. – Caramdir Jan 11 '11 at 23:58
  • Thanks for this hint. I'll test this as soon as i find the time. I'll let you know whether it'll solve my problem. – Robert Jan 13 '11 at 9:16
5

The solution to my problem was to use the \pgfpositionnodelater command of the pgf-package (version 2.10). Thanks to Caramdir for the hint to this possibility.

\documentclass{scrartcl} 

\usepackage{calc} 
\usepackage{tikz} 

\newlength{\TestLength} 
\newlength{\myminx}
\newlength{\mymaxx}

\newcommand{\PgfPosition}{%
    \global\let\myminx=\pgfpositionnodelaterminx%
    \global\let\mymaxx=\pgfpositionnodelatermaxx%
}%

\begin{document} 

    \setlength{\TestLength}{\widthof{\tikz \node {bla};}} 
    \showthe\TestLength% MEASUREMENT 1 

    \begin{tikzpicture} 
      \setlength{\TestLength}{\widthof{\node {bla};}} 
        \showthe\TestLength% MEASUREMENT 2 
    \end{tikzpicture} 

    \begin{tikzpicture}
           {%
                \pgfpositionnodelater{\PgfPosition}%
                \node {bla};%
                \setlength{\TestLength}{\mymaxx}%
                \addtolength{\TestLength}{-\myminx}%
                \global\TestLength=\TestLength
        }%
        \showthe\TestLength% MEASUREMENT 3
    \end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy