例子中,如何显示出R点的坐标值是(2,2)?现在是pt长度单位,不太符合习惯。
\documentclass{standalone}
\usepackage{xeCJK}
\usepackage{tikz}
\usepackage{color}
\usetikzlibrary{calc}
\newdimen\XCoord \newdimen\YCoord \newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};} \newcommand*{\LabelCurrentCoordinate}[2]{\fill [#1] ($(\XCoord,\YCoord)$) circle (2pt) node [above right] {#2}}
\begin{document}
\begin{tikzpicture}
\draw[->] (-5.2,0)--(5.2,0);
\draw[->] (0,-5.2)--(0,5.2);
\foreach \x in {0,1,...,8}
{ \draw[xshift=\x cm] (-4,0) -- (-4,0.1);
\draw[yshift=\x cm] (0,-4) -- (0.1,-4);
};
\node[below] at (0.2,0){0};
\foreach \x in {-4,-3,...,-1}
\node[below] at(\x,0){\x};
\foreach \y in {1,2,...,4}
\node[below] at(\y,0){\y};
\foreach \y in {-4,-3,...,-1}
\node[left] at(0,\y){\y};
\foreach \y in {1,2,...,4}
\node[left] at(0,\y){\y};
%%%
\node[black](g) at(2,2){R};
\ExtractCoordinate{$(g)$};
\LabelCurrentCoordinate{blue} { (\XCoord,\YCoord) };
\end{tikzpicture}
\end{document}
\pgf@x
\pgf@y
分别用于保存当前的坐标,(类似你的 \pgfgetlastxy
)但是“很遗憾”,他们的单位都是 pt,如:\makeatletter
\begin{tikzpicture}
\path (1,1);
\node[above right] at (1,1) {(\the\pgf@x,\the\pgf@y)};
\end{tikzpicture}
\makeatother
得到
为此你需要将其转化为 cm, 可以定义一个命令
\def\pttocm#1{\pgfmathparse{#1/28.45274}\pgfmathresult}
将 pt 转化为 cm
一个完整的 mwe:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\pttocm#1{\pgfmathparse{#1/28.45274}\pgfmathresult}
\makeatletter
\begin{tikzpicture}
\path (1,1);
\node[above right] at (1,1) {(\pttocm{\the\pgf@x},\pttocm{\the\pgf@y})};
\end{tikzpicture}
\makeatother
\end{document}