Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get one component of a tikz/PGF coordinate?

Tags:

latex

tikz

pgf

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?

like image 695
uckelman Avatar asked Oct 19 '09 13:10

uckelman


People also ask

How to use coordinates TikZ?

In TikZ this can be done in two different ways. A cartesian coordinate is given by two numbers (an x- and a y-coordinate) separated by a comma, and surrounded by round brackets, so (-2,0) or (1.5,1.5) . In TikZ coordinates can also be given in polar form.

What is the default unit in TikZ?

Unlike MetaPost, TikZ uses one centimeter as the default unit of measure, so the four points used in this example lie on the x and y axes, one centimeter from the origin.


2 Answers

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.

like image 63
Rupert Nash Avatar answered Oct 14 '22 12:10

Rupert Nash


Alternatively, use

\pgfextractx{<dimension>}{<point>}
\pgfextracty{<dimension>}{<point>}

These are raw PGF commands, so it may be less convenient to use them.

like image 42
Meinersbur Avatar answered Oct 14 '22 13:10

Meinersbur