How to get one component of a tikz/PGF coordinate?
Solution 1
You can get at the components inside a let
operation. Look it up in the PGF manual for the works, but from memory:
\draw
let
\p1=($(a.north)!0.5!(b.south)$),
\p2=(current bounding box.west),
\p3=(current bounding box.east)
in
(\x2,\y1) -- (\x3, \y1);
That'll probably need debugging... EDIT: and now has been thanks to the questioner.
Solution 2
Alternatively, use
\pgfextractx{<dimension>}{<point>}
\pgfextracty{<dimension>}{<point>}
These are raw PGF commands, so it may be less convenient to use them.
Solution 3
Another option I just found from this shows that you can get relative positioning from a node by doing this:
\node at (2, 1) (a) {};
\draw (a) -- ++(right:2);
This will draw a line from node a to the point 2 units to the right of point a. Like in the example I linked, this is useful when you don't know the exact coordinates of a node, and need to draw things relative to it.
uckelman
I am one of the principals of Lightbox Technologies, a digital forensics firm. I have a PhD in computer science from the University of Amsterdam, with a background in logic and social choice. I've used C, C++, Perl, PHP, Java, shell, and LaTeX extensively. I also have some familiarity with PostScript, Python, and Prolog. I'm an avid wargamer, and one of the developers for the VASSAL, a boardgame engine, as well as mkhexgrid, a hex grid creation program.
Updated on June 14, 2022Comments
-
uckelman almost 2 years
I'm trying to draw a horizontal line across my diagram. The Y coordinate of the line should be halfway between points a and b (a is below b). The left and right endpoints of the line are on the bounding box of the tikzpicture. Here's how I'm doing this now, using the intersection operator:
\coordinate (h0) at ($(a.north)!0.5!(b.south)$); \draw (h0 -| current bounding box.west) -- (h0 -| current bounding box.east);
This strikes me as rather roundabout. What I'd rather do is get the Y coordinate of (h0) and the X coordinates of the east and west sides of the bounding box, and compose the coordinates myself. I'd like to do this, but it isn't supported syntax:
\coordinate (h0) at ($(a.north)!0.5!(b.south)$); \draw (current bounding box.west.x,h0.y) -- (current bounding box.east.x,h0.y);
Is there a way to reference individual components of coordinates that I'm missing?
-
uckelman over 14 yearsThanks! Debugging: The comma ending the
\p3
line needs to be deleted,a.north
needs a closing ')' andb.south
needs an opening '('. Once that's done, this works perfectly. -
leemes over 12 yearsBut
<point>
really needs to be a lowlevel pgfpoint. To make it work with points defined using\coordinate at ...
, you have to say\pgfpointanchor{coordinate}{center}
, since\coordinate (coordinate) at (1,2);
defines a node of shape coordinate (having one ancher called center) and \pgfpointanchor returns the pgf lowlevel point for this anchor. -
tobiasBora over 2 years@leemes Any idea how to make
\pgfpointanchor
work for pgfpoints (i.e. if the coordinate is in fact a pgfpoint, then just return that point)? Or maybe to make the distinction between pgfpoints and coordinate? I'm asking because in path\tikztotarget
can be both a point or a coordinate depending on whether the start isA
orA.east
.