5

Question

Is it possible to store the variable width of a node and use that width to calculate the text width of another node in TikZ?

Example

I have a list of similar nodes side-by-side (inline, not overlaid). Let's call the left column of nodes "A" and the right column of nodes "B". Column A nodes contain a number (counter variable, which means the node gets wider at each digit carry-over). Column B nodes should have some text width minus the text width of the corresponding node in column A.

  • get width of node A = \fontsize{50}{60}\selectfont\countervalue{}

  • text width of node B = (\textwidth) - (width of node A)

I think the answer I am looking for is hidden somewhere in this answer.

8

Did I understand correctly?

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\usepackage{lipsum}
\begin{document}

\begin{tikzpicture}
\foreach\j in {1,10,100}{
\node[outer sep=0] (a-\j) at (0,{-7*log10(\j)}) {\j};
\path let \p1=($(a-\j.east)-(a-\j.west)$) 
  in 
  node[anchor=west,text width=\textwidth-\x1] (b-\j) at (a-\j.east) 
    {\lipsum[1]};
}
\end{tikzpicture}
\end{document}

enter image description here

  • Hey yes sort of... I was hoping to learn from this how to derive \x in your example, but I am still unsure of what \x is and where it came from. I expected to see the width of the first node being set somewhere (like in a pgf variable). – Jonathan Komar Feb 8 '15 at 21:55
  • 2
    @macmadness86: The width of node A is the distance between its west and east anchors. In the example above, the point \p1 is calculated by subtracting A.west from A.east (@percusse used the a-\j as node name instead of A, but the idea is the same). To refer to the x-coordinate of point \p1, you can use \x1, which is equal to the x-coordinate of A.east minus the x-coordinate of A.west. This difference is exactly the width of node A. – Herr K. Feb 8 '15 at 22:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.