考虑一个 TikZ 图片,其中有一个圆在 TikZ 坐标系中以 (0,0) 为中心。节点周围是一个复杂的形状,例如如下所示的曲线。在包中standalone
,边框经过计算以自动贴合图片内容。
编译时是否可以输出standalone
坐标系中(0,0)的最终坐标?
最小工作示例
以下代码片段生成了下图的简化版本。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);
\end{tikzpicture}
\end{document}
答案1
节点中心的坐标(在 处(0,0)
)将保持不变。但您可以提取边界框的坐标(如果使用包,则与输出格式相同standalone
),并使用这些坐标来计算节点中心与每条边的距离。
您可以使用虚拟节点current bounding box
及其锚点north east
并south west
结合\pgfgetlastxy
获取当前边界框的坐标(south west
但您可能只需要):
\path (current bounding box.north east);
\pgfgetlastxy{\boundingboxeast}{\boundingboxnorth}
\path (current bounding box.south west);
\pgfgetlastxy{\boundingboxwest}{\boundingboxsouth}
以下代码将打印(current bounding box.south west) is at (-77.49pt,-13.25343pt).
到日志中:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);
\path (current bounding box.south west);
\pgfgetlastxy{\boundingboxwest}{\boundingboxsouth}
\message{(current bounding box.south west) is at (\boundingboxwest,\boundingboxsouth).}
\end{tikzpicture}
\end{document}
您可以创建一个这样的自定义辅助宏:
\documentclass{standalone}
\usepackage{tikz}
\NewDocumentCommand{\whereiscoordinate}{m}{
\path (current bounding box.south west);
\pgfgetlastxy{\currentboundingboxwest}{\currentboundingboxsouth}
\path #1;
\pgfgetlastxy{\currentcoordinatex}{\currentcoordinatey}
\pgfmathsetmacro{\currentcoordinatedistancex}
{-1*\currentboundingboxwest+\currentcoordinatex}
\pgfmathsetmacro{\currentcoordinatedistancey}
{-1*\currentboundingboxsouth+\currentcoordinatey}
\message{Coordinate #1
is \currentcoordinatedistancex pt from the left and
\currentcoordinatedistancey pt from the bottom
of the current bounding box. ^^J}
}
\begin{document}
\begin{tikzpicture}
\coordinate (c1) at (0,0);
\coordinate (c2) at (-2, 0.2);
\node[circle,inner sep=2pt,draw=none,fill=red] at (c1) {};
\draw[color=blue,bend left=150] (c1) to (c2);
\whereiscoordinate{(0,0)}
% prints to the log:
% Coordinate (0,0) is 77.49pt from the left and 13.25343pt from the bottom
% of the current bounding box.
\whereiscoordinate{(-2,0.2)}
% prints to the log:
% Coordinate (-2,0.2) is 20.58452pt from the left and 18.9439pt from the bottom
% of the current bounding box.
\end{tikzpicture}
\end{document}