# The spacing is not the same after defining the absolute position of the node？

I defined the absolute position of the node, and the difference between their vertical coordinates is fixed y. However, the distance between them is different in the picture display. How to solve it?

\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\tikzstyle{mynode}=[thick,rectangle,minimum size=5mm]
\begin{document}
\begin{tikzpicture}%[x=2.2cm,y=1.8cm]

\def\x {5mm};
\def\y {2mm};

\node[mynode] (20) at (0,5\y) {a};
\node[mynode] (21) at (0,4\y) {b};
\node[mynode] (22) at (0,3\y) {c};
\node[mynode] (23) at (0,2\y) {d};
\node[mynode] (24) at (0,\y) {e};
\node[mynode] (25) at (0,0) {f};

\end{tikzpicture}
\end{document}



• You need * to trigger pgfmath.
– cfr
Commented Aug 19, 2023 at 3:24
• The difference between e and f is 2mm. The difference between a and b, b and c, c and d, d and e is 10mm. \y is fixed, but the difference between 52mm and 42mm is different from that between 2mm and 0.
– cfr
Commented Aug 19, 2023 at 3:33
• While you have probably seen things like 0.5\textwidth, this notataion only works for dimen registers, not macros. Commented Aug 19, 2023 at 14:24

You want the second coordinate to be calculated (I assume) by pgfmath. In that case, you need to use * or some other way of triggering the calculation.

\documentclass[border=3pt,tikz]{standalone}
\tikzstyle{mynode}=[thick,rectangle,minimum size=5mm]
\begin{document}
\begin{tikzpicture}%[x=2.2cm,y=1.8cm]

\def\x {5mm}
\def\y {2mm}

\node[mynode] (20) at (0,5*\y) {a};
\node[mynode] (21) at (0,4*\y) {b};
\node[mynode] (22) at (0,3*\y) {c};
\node[mynode] (23) at (0,2*\y) {d};
\node[mynode] (24) at (0,\y) {e};
\node[mynode] (25) at (0,0) {f};

\end{tikzpicture}
\end{document}


Note that the nodes are correctly placed in the following. They overlap a lot because there isn't enough space, but they just fit due to the shapes of the letters.

In the original code, you're placing them at 52mm, 42mm, 32mm, 22mm, 2mm and 0.

Just for fun:

Actually, I think tikz already defines \x so that \x1, \x2 and \x3 are legal expressions (the 1 etc. are arguments).

\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\tikzstyle{mynode}=[thick,rectangle,minimum size=5mm]

\newlength{\myx}
\newlength{\myy}

\begin{document}
\begin{tikzpicture}%[x=2.2cm,y=1.8cm]

\myx=5mm
\myy=2mm

\node[mynode] (20) at (0,5\myy) {a};
\node[mynode] (21) at (0,4\myy) {b};
\node[mynode] (22) at (0,3\myy) {c};
\node[mynode] (23) at (0,2\myy) {d};
\node[mynode] (24) at (0,\myy) {e};
\node[mynode] (25) at (0,0) {f};

\end{tikzpicture}
\end{document}


Off topic:

\documentclass{article}
\makeatletter
\def\x#1{\@ifundefined{x#1}{\errmessage{Undefined control sequence:}}
{\csname x#1\endcsname}}
\makeatother
\expandafter\dimendef\csname x1\endcsname=0
\begin{document}
\x1=1pt
\the\x1

\x2
\end{document}

• But isn't this because \myy is a dimension? In the original question, the second coordinate was simply a number followed by a macro. But I agree using \y seems problematic (even if it seems to work). If you use \def\myy{2mm} without the *, you still get the OP's result. \show\y before the first \def gives undefined.
– cfr
Commented Aug 19, 2023 at 18:48
• Also \y5 gives an undefined control sequence error.
– cfr
Commented Aug 19, 2023 at 18:57
• @cfr: I can create a \dimen register named \x1, but getting an error message for \x2 was harder. Commented Aug 19, 2023 at 21:45
• But I thought the question was whether tikz defined \x#1 or \y#1? That's how I read the beginning of your answer. But that doesn't seem to be the case. Am I just misunderstanding what you're saying?
– cfr
Commented Aug 20, 2023 at 0:26
• That I actually named the register \csname x1\endcsname is irrelevant. Any name would do. OTOH, since there is no LaTeX equvalent to \p1=\p2, the tikz parser has to be handling at least some of the "registers" directly. Just how much, no clue. Commented Aug 20, 2023 at 19:28