和
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (2,2) circle (3cm);
\node at (current bounding box.north)[anchor=north]{north};
\node at (current bounding box.north west)[anchor=north west]{north west};
\node at (current bounding box.north east)[anchor=north east]{north east};
\end{tikzpicture}
\end{document}
可以使用一组给定的位置( ,等)node
将相对于图片边界框的位置定位。north
north west
我希望在 内更自由地定位文本current bounding box
,例如在 位置(0.45, 0.7)
,其中(0, 0)
是 的左下角和(1, 1)
的右上角current bounding box
。
如何实现这一点?
答案1
您可以使用库计算坐标calc
。请注意,如果您将节点放置在伸出的边缘附近,则current bounding box
节点将发生变化。如果您不想更改边界框,您可以添加一个,\useboundingbox
如下例所示。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\relcoord[2]{({$(current bounding box.south west)!#1!(current bounding box.south east)$} |- {$(current bounding box.south west)!#2!(current bounding box.north west)$})}
\begin{document}
\begin{tikzpicture}
\draw (2,2) circle[radius=2.5cm];
\node at (current bounding box.north)[anchor=north]{north};
\node at (current bounding box.north west)[anchor=north west]{north west};
\node at (current bounding box.north east)[anchor=north east]{north east};
\useasboundingbox \relcoord{0}{0} rectangle \relcoord{1}{1};
\draw [use as bounding box,help lines,step=0.5cm] \relcoord{0}{0} grid \relcoord{1}{1};
\fill [red] \relcoord{0.6}{0.7} circle[radius=3pt];
\fill [blue] \relcoord{0.3}{0.4} circle[radius=3pt];
\foreach \x in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {
\node at \relcoord{\x}{0.7}[anchor=center] {x};
\node at \relcoord{0.7}{\x}[anchor=center] {x};
}
\end{tikzpicture}
\end{document}