TikZ get current unit length of x-coordinate

I am searching for a macro which prints the current x-unit-length. For instance the following document:

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[x=2cm,y=3cm]
\node at (0,0) {};
\end{tikzpicture}
\end{document}


Now I want a macro which can return the '2cm' of 'x' or '3cm' of y.

Here is a solution using the calc library:

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\getxlengthincm#1{
\path let \p{x}=(1,0), \n{xlencm}={scalar(veclen(\x{x},\y{x})/1cm)}
in \pgfextra{\xdef#1{\n{xlencm}}};
}

\begin{document}
\begin{tikzpicture}[x=2cm,y=3cm]
\getxlengthincm{\myxlength}
\node[align=center] at (0,0) {x length:\myxlength cm};
\end{tikzpicture}
\end{document}


Here is a variant with explicit pt unit:

\def\getxlength#1{
\path let \p{x}=(1,0), \n{xlen}={veclen(\x{x},\y{x})}
in \pgfextra{\xdef#1{\n{xlen}}};
}

• Why not simply use \path let \p{x}=(1,0), \n{xlencm}={scalar(\x{x}/1cm)} without veclen ?
– Kpym
Apr 4 '18 at 13:51
• @Kpym the value of x (and y) can be a length or a coordinate. Apr 4 '18 at 13:54
• I also want the unit, so, cm to be retrieved. Here you added it as text.
– Arne
Apr 4 '18 at 14:15
• @PaulGaborit you use it here with \p{x}=(1,0) so veclen(\x{x},\y{x}) and \x{x} are the same, no ?
– Kpym
Apr 4 '18 at 14:30
• @Kpym Try with x={(1cm,1cm)} for example... Apr 4 '18 at 14:41

Using the printlen package and https://tex.stackexchange.com/a/15996/36296

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{printlen}

\newlength{\pgfx}
\newlength{\pgfy}

\uselengthunit{cm}
\begin{document}

\begin{tikzpicture}[x=2cm,y=3cm]
\node at (0,0) {};
\pgfpointxy{1}{1};
\makeatletter
\setlength{\pgfx}{\pgf@x}
\setlength{\pgfy}{\pgf@y}
\makeatother
\node {\printlength{\pgfx}~\printlength{\pgfy}};
\end{tikzpicture}
\end{document}


• So there is no simple, native macro which can do this. Feels a bit weird to me ;)
– Arne
Apr 4 '18 at 12:00
• @Arne I did not say that, just that you could do it like this Apr 4 '18 at 12:01
• If I replace y=3cm by y={(-2cm,1cm)}, I got a wrong result! Apr 4 '18 at 13:53
• What does y={(-2cm,1cm)} mean? I thought y is a coordinate...
– Arne
Apr 4 '18 at 14:17
• @Arne x defines the x-vector. You can use x=1cm (equivalent to x={(1cm,0)}) or a true vector as x={(1cm,5mm)} (see pp.357-358, pgfmanual, v3.0.1a). Apr 4 '18 at 14:51

\documentclass[border=2pt]{standalone}

\usepackage{tikz,luatex85,siunitx}

\makeatletter
\newcommand{\NodeDist}[3][\MyDist]{%
\pgfpointdiff{\pgfpointanchor{#2}{center}}
{\pgfpointanchor{#3}{center}}
% no need to use a new dimen
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
% to convert from pt to cm
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)/28.45274}
\global\let#1\pgfmathresult % we need a global macro
}

\newcommand{\TZUnits}{
\coordinate (@0) at (0,0) ;
\coordinate (@X) at (1,0) ;
\coordinate (@Y) at (0,1) ;
\NodeDist[\Xunit]{@0}{@X}\edef\Xunit{\SI{\Xunit}{\cm}}
\NodeDist[\Yunit]{@0}{@Y}\edef\Yunit{\SI{\Yunit}{\cm}}
}

\makeatother

\begin{document}

\begin{tikzpicture}[x=2cm,y=3cm]
\TZUnits
\node {\Xunit~\Yunit};
\end{tikzpicture}

\end{document}

• Here, also the unit (cm) is missing.
– Arne
Apr 4 '18 at 14:16
• Vos désirs font désordre... Apr 4 '18 at 14:53