14

I would like to be able to use x, y and z components of a defined coordinate in TikZ to reuse the values somewhere else in my code. For example, let's have something like,

\coordinate (p1) at (-2.6,0.7,0);

I would like to do something like as below,

\coordinate (p11) at (p1_x,p1_y,12);

How can I do this?

Rasoul
  • 1,726

1 Answers1

9

Well, while a solution is impossible for the general case (for the reasons Andrew explained in a comment), a solution for this particular case is possible, via a horrible, horrible, hack.

The problem is that, as soon as you set something like:

\coordinate (p) at (3,4,5);

tikz computes the projection of the 3D point (3,4,5), according to the current values of keys x, y and z, and gets a 2D point, whose coordinates are stored in (p). So the final value for the coordinates stored in (p) are in absolute units (pt) and of course they are not 3 and 4.

But in this particular case, coordinate p1 has z=0:

\coordinate (p1) at (3,4,0);

so for this point the computed coordinates should be equal to the ones given, i.e: 3 and 4. But there is a problem, they are stored in absolute units (pt in this case). Tikz multiplies 3 (and 4) by the value of the key x (and y) which are 1cm by default, and the result expressed in pt is what (p1) gets. So, using let...in construct we can extract those values, but they will be in points.

So the horrible, horrible hack is to define x and y to be equal to 1pt, so that the figure uses points instead of cm as default units. This works:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{calc}

\begin{tikzpicture}[x=1pt, y=1pt, z=-0.5pt]  % Agh
% Let's draw some 3D axes
\coordinate (x) at (100,0,0);
\coordinate (y) at (0,100,0);
\coordinate (z) at (0,0,100);
\foreach \axis in {x,y,z}
   \draw[-latex] (0,0,0) -- (\axis);

% Our point
\coordinate (p1) at (80, 30, 0);
% Compute the point at the same x,y, elevated 25pt in z
\path let \p1 = (p1)
         in coordinate (p11) at (\x1, \y1, 25);

% Draw those points
\fill[red]     (p1) circle (1pt);
\fill[blue]   (p11) circle (1pt);
% Some bells and whistles
\draw[dotted] (p1) -- (p1|-x);
\draw[dotted] (p1) -- (p1-|y);
\draw[dashed,-latex] (p1) -- (p11);
\end{tikzpicture}

Results in:

Result

JLDiaz
  • 55,732