![从坐标中提取 x/y 值(以厘米为单位)](https://linux22.com/image/473279/%E4%BB%8E%E5%9D%90%E6%A0%87%E4%B8%AD%E6%8F%90%E5%8F%96%20x%2Fy%20%E5%80%BC%EF%BC%88%E4%BB%A5%E5%8E%98%E7%B1%B3%E4%B8%BA%E5%8D%95%E4%BD%8D%EF%BC%89.png)
我想提取以 cm 为单位的 x/y 坐标。下面的示例将其提取为 pt。
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A) in node[draw,circle,inner sep=5pt,label=above:{\x1,\y1}] at (A) {};
\end{tikzpicture}
\end{document}
我怎样才能获得 2,3 作为标签?
答案1
您可以使用\fpeval
。该\ptto
命令需要将单位作为第一个强制参数,因此您可以使用不同的参数。可选参数是要四舍五入的小数位数。
\documentclass[border=4]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\NewExpandableDocumentCommand{\ptto}{O{2}mm}{%
\fpeval{round((#3)/(1#2),#1)}%
}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A) in node[
draw,
circle,
inner sep=5pt,
label=above:{\ptto{cm}{\x1},\ptto{cm}{\y1}}
] at (A) {};
\end{tikzpicture}
\end{document}
也许您想要以缩放点为单位的长度……
\documentclass[border=4]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\NewExpandableDocumentCommand{\ptto}{O{2}mm}{%
\fpeval{round((#3)/(1#2),#1)}%
}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A) in node[
draw,
circle,
inner sep=5pt,
label=above:{\ptto[0]{sp}{\x1},\ptto[0]{sp}{\y1}}
] at (A) {};
\end{tikzpicture}
\end{document}
答案2
代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}
\makeatletter
% transform a dimension (in pt) into a dimensionless number
\newcommand{\strippt}{\strip@pt\dimexpr}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A),
\n1 = {\x1*1pt/1cm},
\n2 = {\y1*1pt/1cm},
in
node[draw, circle, inner sep=5pt,
label=above:{\strippt(\n1),\strippt(\n2)}] at (A) {};
\end{tikzpicture}
\end{document}
答案3
您可以使用\pgfmathparse
来删除单位。从点到厘米的转换因子是 .03514。您可以使用\pgfmathprintnumber
来避免舍入误差。
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A) in node[draw,circle,inner sep=5pt,label=above:{\pgfmathparse{.03514*\x1}\pgfmathprintnumber{\pgfmathresult},\pgfmathparse{.03514*\y1}\pgfmathprintnumber{\pgfmathresult}}] at (A) {};
\end{tikzpicture}
\end{document}