5

I have several nodes and I need to find which one is the widest.

This example draws a vertical line in through the center of the widest node, but I know which node to choose.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \foreach \name[count = \n] in {peter, wellingon, john, william}{
        \node[draw, anchor = west] at (0, -\n) (name-\n) {\name};
    }
    \draw let \p1 = (name-1.center) in (\x1, 0) -- (\x1, -5);
\end{tikzpicture}
\end{document}

Can I make this choice automatically? I could't find a way to get a coordinate and store it in a global macro for later use. The let operator gives easy access to coordinates, but only inside a path, as far as I know.

enter image description here

2

2 Answers 2

6

Edit:

You can box all the content by placing everything in scope. Adding local bounding box option allows you to name it and use as a regular node.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=foo]
    \foreach \name[count = \n] in {peter, wellingon, john, william}{
        \node[draw, anchor = west] at (0, -\n) (name-\n) {\name};
    }
\end{scope}

\draw ([yshift=20pt]foo.north) -- ([yshift=-20pt]foo.south);

\end{tikzpicture}
\end{document}

Thanks to @sgmoye for this remark.


Original post:

One can be solved using fit library so that you will able to create a phantom box (node) with the side length of the biggest node.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=foo]
    \foreach \name[count = \n] in {peter, wellingon, john, william}{
        \node[draw, anchor = west] at (0, -\n) (name-\n) {\name};
    }
\end{scope}

\node[fit=(foo),inner sep=0pt](bar){};

\draw[shorten >=-20pt,shorten <=-20pt] (bar.south) -- (bar.north);
\end{tikzpicture}
\end{document}

enter image description here

2
  • Actually, you can replace all of the drawing in your proposal with \draw ([yshift=20pt]foo.north) -- ([yshift=-20pt]foo.south); I had not known about local bounding box=... possibility -- thank you for that. And no need for the \usetikzlibrary{fit}.
    – sgmoye
    Dec 23, 2021 at 10:57
  • @sgmoye your're right. Great remark, I edited the answer. Thanks!
    – antshar
    Dec 23, 2021 at 14:34
1

The answer by @antshar is far and away the best approach. However, you did ask how to find the longest node, and this does answer that question. This is a far less elegant solution and is not really recommended. I give it only FYI. The origins for this code can be found at Color two columns separately in a matrix though variations on it can be found in a number of places. Output is the same.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\newlength{\tempx}
\newlength{\longest}

\begin{document}

\begin{tikzpicture}
    \foreach \name[count = \n] in {peter, wellingon, john, william}{%
        \node[draw, anchor = west] at (0, -\n) (name-\n) {\name};
        \pgfextractx{\tempx}
            {\pgfpointdiff{\pgfpointanchor{name-\n}{west}}{\pgfpointanchor{name-\n}{east}}}
            \ifdim\tempx>\longest \global\longest=\tempx\fi%% foreach is *local*
    }%
    \draw ([xshift=\longest/2,yshift=-20pt]current bounding box.south west) --
        ([xshift=\longest/2,yshift=20pt]current bounding box.north west);
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .