Tikz スケールされた画像のノードテキストの座標を表示する

Tikz スケールされた画像のノードテキストの座標を表示する

座標の値を表示したいのですが、以下を使用すると値が正確に印刷されません。

スケールは以下を使用して取得しようとしますTikZ でスケーリング係数を回復するそして、スケール係数の値を次のように使用します。TikZ で任意の点の x、y 座標を抽出します。ただし、Tikz の図で使用された場合、@scalefactor は実際の係数を表示するにもかかわらず、\xcoord と \ycoord で常に 1 を返します。

修正方法について何かアイデアはありますか?

%!TeX program = lualatex
\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{math, calc}

%% https://tex.stackexchange.com/questions/86897/recover-scaling-factor-in-tikz
\newcommand*\getscale[1]{%
\begingroup
\pgfgettransformentries{\scaleA}{\scaleB}{\scaleC}{\scaleD}{\whatevs}{\whatevs}%
\pgfmathsetmacro{#1}{sqrt(abs(\scaleA*\scaleD-\scaleB*\scaleC))}%
\expandafter
\endgroup
\expandafter\def\expandafter#1\expandafter{#1}%
}

% for printing out coordinates in Tikz coordinate variable
\makeatletter
\newcommand\xcoord[1]{{%
    \getscale{\@scalefactor}%
    \pgfpointanchor{#1}{center}%
    \pgfmathparse{\pgf@x/\pgf@xx/\@scalefactor}%
    \pgfmathprintnumber{\pgfmathresult}%    
}}
\newcommand\ycoord[1]{{%
        \getscale{\@scalefactor}%
        \pgfpointanchor{#1}{center}%
        \pgfmathparse{\pgf@y/\pgf@yy/\@scalefactor}%
        \pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

\begin{document}

\begin{figure}
    \centering
    
    \begin{tikzpicture}[scale=1/10]
        % GRID - X
        \foreach \i in {0,24,...,120}{
            \draw[gray, thin] (\i,-12) -- (\i,184);
            \tikzmath{int \value; \value = \i;}; 
            \node[gray, below] at (\i,-12) {\value};
        }
        
        % GRID - Y
        \foreach \i in {0,24,...,168}{
            \draw[gray, thin] (-12,\i) -- (132,\i);
            \tikzmath{int \value; \value = \i;};
            \node[gray, left] at (-12,\i) {\value};
        }
    
        % Draw some lines with relative coordinates
        \draw[thick] (0,0) -- ++(12,0) coordinate (c);
        \draw[thick] (0,0) -- ++(0,60) coordinate (c);
        \draw[thick] (c) -- ++(0,36) coordinate (c);
        \draw[thick] (c) -- ++(0,76) coordinate (c);
        \draw[thick] (c) -- ++(120,0) coordinate (c);
        
        % Display coordinate
        \getscale{\scalefactor};
        \node[fill=white, text=blue] at (60,60) {Scale = \scalefactor};
        \node[fill=white, text=magenta, yshift=-15pt] at (c) {(\xcoord{c}, \ycoord{c})};            
    \end{tikzpicture}
\end{figure}
\end{document}

上記はコンパイルされますが、マゼンタの座標は黒の線が終わる場所 (120, 172) と一致しません。

ここに画像の説明を入力してください

答え1

これは、目盛りを手動でラベル付けする問題です。つまり、x ラベルを「計算」しています\tikzmath{int \value; \value = \i;};が、これらは単なる数値であり、「tikz 座標」ではないため、スケーリングについては何も知りません。

解決策としては、例えば次のように手動でスケーリングを行う。

\tikzmath{int \value; \value = \i * \scalefactor;}; 

\value整数として使用すると丸め誤差が生じることを考慮する必要がありますが、これは別の問題です。

答え2

変換されていない元の座標を取得する方法はここにあります。TikZ座標の論理値にアクセスする

カスタムコマンドの使用

\makeatletter
\def\extractcoord#1#2#3{
  \path let \p1=(#3) in \pgfextra{
    \pgfmathsetmacro#1{\x{1}/\pgf@xx}
    \pgfmathsetmacro#2{\y{1}/\pgf@yy}
    \xdef#1{#1} \xdef#2{#2}
  };
}
\makeatother

座標は次のように表示できる。

\coordinate (A) at (120,172);
\extractcoord{\x}{\y}{A};
\node at (A) {Coordinates: \x, \y};

これは、スケーリング、x と y の両方の単位の変更、およびスコープ環境でテストされました。

関連情報