3

In Metafont and Metapost I can easily make a variable z1 (as long as it is the letter z followed by an index number) represent a point like this:

z1=(1,2);

and I can use this to make a line, like in:

draw z1--(7,5);

Can I make the same thing in Tikz ? I cannot find it anywhere, and if i write a code like:

\z1=(1,2); \draw z1--(7,5);

It gives me an error, and it would make my work a lot easier if I could reference one point to one single variable that I could change once and it would affect every instance in wich this variable is used.

gernot
  • 49,614
Felipe Dilho
  • 355
  • 1
  • 7
  • 1
    I don't think this is properly tagged as only concerns TikZ. –  Feb 14 '21 at 12:38
  • This is called a node. You can make a node without any content by writing \coordinate (z1) at (1,2); then draw like this \draw (z1) -- (7,5);. – SebGlav Feb 14 '21 at 12:39
  • @SebGlav I wouldn't call a coordinate a node without a content. A node defines more than a single coordinate. What the OP is looking for isn't a node, for sure. – gernot Feb 14 '21 at 13:45
  • Note also that named coordinates are stored globally. – John Kormylo Feb 14 '21 at 17:17
  • @JohnKormylo Globally, but only for the current tikzpicture environment; unless one uses remember picture for the defining environment and another remember picture for the picture where they want to access the coordinate again. – Philipp Imhof Feb 15 '21 at 07:41
  • @PhilippImhof - Actually, the coordinates/nodes are saved as 5 separate global macros (see https://tex.stackexchange.com/questions/570640/can-tikz-matrices-be-forgotten/570765?r=SearchResults&s=1|12.1469#570765). Remember picture mostly saves the origin location to the aux file using \pdfsavepos in order to find it. – John Kormylo Feb 16 '21 at 23:04

2 Answers2

4

Maybe not exactly what you are looking for, but:

\documentclass{article}
\usepackage{tikz}

\begin{document} \begin{tikzpicture} \coordinate (z1) at (1,2); \draw (z1) -- (7,5); \end{tikzpicture} \end{document}

4

Addendum to Philipp's answer: You don't have to define the names of coordinates separately, but can do it on the fly whenever you want to refer to the point later on, by adding coordinate (name) after the place where the point has been reached. In your example:

\draw (1,2) coordinate (z1) -- ++(6,3) coordinate (z2);

From now on, in the remaining picture, you can refer to (1,2) as (z1) and to (7,5) as (z2). In this example, (z2) is defined relative to (z1) by adding the difference (6,3).

Whether coordinates should be defined separately or inline, is a matter of taste and of the role they play. If (z1) belongs to the given information (the information defining the layout), then an explicit definition is probably better. If (z1) is rather an auxiliary point depending on the construction, then an inline definition is more natural.

gernot
  • 49,614