3

After several \coordinate pairs are created in a TikZ picture, how can I get the minimum and maximum values of the x-coordinates of those pairs, so as to use them further?

Toy example (does not compile, of course):

\documentclass[tikz,border=0pt]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\coordinate (X1) at (0,0); 
\coordinate (X2) at (2,2); 
\coordinate (X3) at (4,1);
\draw[thick] 
    let \xmax= <maximum of x-coordinates>, %WHAT?
        \xmin=<minimum of x-coordinates>        % WHAT?
    in 
        (\xmin,-1) -- (\xmax,-1);
\end{tikzpicture}

\end{document}

2 Answers 2

7

Looks like you want to access the boundary box of the path. You can do that by scopeing and assigning local bounding box name so you can reference it as a node.

\documentclass[tikz, border=10pt]{standalone}
\begin{document}

\begin{tikzpicture}

\begin{scope}[local bounding box=foo]
\path (0,0) coordinate (X1)
   to (2,2) coordinate (X2)
   to (4,1) coordinate (X3);
\end{scope}

\draw (foo.south west) -- (foo.south east);

\end{tikzpicture}

\end{document}

If you want to shift the line, you can use

\draw[transform canvas={shift={(0,-1)}}] (foo.south west) -- (foo.south east);

In case relative shifting is not the way to go, you can precisely put the line at a specific y-coordinate like so

\draw (foo.south west |- 0,-2) -- (foo.south east |- 0,-2);

this will place the line at y=-2.

3
  • But I want to locate that line segment below where your code puts it, say at y-coordinate -2. So it is really the min and max coordinates of (X1), (X2), (X3) that I need, to use a x-coordinates for the ends of that line segment.
    – murray
    Commented May 22 at 14:39
  • @murray added to the answer.
    – antshar
    Commented May 22 at 16:05
  • Aha! And thank you for the version using |- 0,-2. I had seen such notation elsewhere but was unable to fully understand it.
    – murray
    Commented May 22 at 18:10
6

For example like this, if you want to make use of the let syntax:

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\coordinate (X1) at (0,0); 
\coordinate (X2) at (2,2); 
\coordinate (X3) at (4,1);
\draw[thick] 
    let 
        \p1 = (X1), 
        \p2 = (X2), 
        \p3 = (X3)
    in 
        ({min(\x1,\x2,\x3)},-1) -- ({max(\x1,\x2,\x3)},-1);
\end{tikzpicture}

\end{document}
3
  • 1
    why is it necessary to create new names \p1, \p2, \p3 when the names X1, X2, X3 are already there?
    – murray
    Commented May 21 at 23:13
  • And suppose I had already defined \path[save path=\pathX] (X1) (X2) (X3);. Is there a way to find that max and min of x-coordinates without explicitly relisting the individual X1, X2, X3? I ask because, in a real example, I would have a longer set of points, and it would be tedious to type their individual names in such a 'let` construction.
    – murray
    Commented May 22 at 0:44
  • @murray It you use the let syntax, it is necessary to define the coordinates again inside. This is just how this syntax works. See antshar's answer which is probably better suited for your needs. Commented May 22 at 19:21

You must log in to answer this question.

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