3

I need a length long as /tikz/cs/x[y]. It can be done with a trick like this:

\documentclass{article}
\usepackage{tikz}

\newlength{\scaledx}
\newlength{\scaledy}

\begin{document}
\begin{tikzpicture}[
x=30pt,
y=60pt,
]

\makeatletter
\pgfpointxy{1}{1}
\setlength{\scaledx}{\pgf@x};
\setlength{\scaledy}{\pgf@y};
\makeatother

\node[draw] at (0,0) { \the\scaledx };
\node[draw] at (1,1) { \the\scaledy };
\end{tikzpicture}

\end{document}

However, in the place I need my picture the \makeatletter breaks something. Besides this method seems a bit hackish for this task.

What is the proper way to get a length from /tikz/cs/x[y]?

Torbjørn T.
  • 206,688

2 Answers2

3

Can't you just define a macro in your preamble to set \scaledx and \scaledy, which needs to be surrounded by \makeatletter....\makeatother. Later on in your document when you need them call the macro. This way tikzpicture doesn't see the catcode changes.

In more detail, something like this:

\documentclass{article}
\usepackage{tikz}

\newlength{\scaledx}
\newlength{\scaledy}

\makeatletter
\newcommand\SetScales{%
  \pgfpointxy{1}{1}
  \setlength{\scaledx}{\pgf@x};
  \setlength{\scaledy}{\pgf@y};
}
\makeatother

\begin{document}
  \begin{tikzpicture}[x=30pt, y=60pt]\SetScales
    \node[draw] at (0,0) { \the\scaledx};
    \node[draw] at (1,1) { \the\scaledy };
  \end{tikzpicture}
\end{document}
2

You could also use

\documentclass{article}
\usepackage{tikz}

\newlength{\scaledx}
\newlength{\scaledy}
\newcommand\SetScales{%
  \pgfextractx{\scaledx}{\pgfpointxy{1}{0}}%
  \pgfextracty{\scaledy}{\pgfpointxy{0}{1}}%
}

\begin{document}
\begin{tikzpicture}[
    x=30pt,
    y=60pt,
  ]
  \SetScales
  \node[draw] at (0,0) { \the\scaledx };
  \node[draw] at (1,1) { \the\scaledy };
\end{tikzpicture}
\end{document}

or

\documentclass{article}
\usepackage{tikz}

\newlength{\scaledx}
\newlength{\scaledy}
\newcommand\SetScales{%
  \pgfpointxy{1}{1}%
  \pgfextractx{\scaledx}{}%
  \pgfextracty{\scaledy}{}%
}

\begin{document}
\begin{tikzpicture}[
    x=30pt,
    y=60pt,
  ]
  \SetScales
  \node[draw] at (0,0) { \the\scaledx };
  \node[draw] at (1,1) { \the\scaledy };
\end{tikzpicture}
\end{document}
esdd
  • 85,675