我想在灰色背景的角落放置一个圆圈,这是代码:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\newlength{\width}
\setlength{\width}{16cm}
\newlength{\height}
\setlength{\height}{10cm}
\begin{document}
\begin{tikzpicture}
\node[fill=gray,minimum width=\width,minimum height=\height] at (0.5\width,0.5\height) {};
\draw[green] (0,0) |- (\width,\height);
\node[circle,anchor=north west,fill=blue,text=white,minimum size=0.2\height,font=\Large,inner sep=0pt,outer sep=0pt] at (0,\height) {T};
\end{tikzpicture}
\end{document}
现在忽略circle
并再次尝试:
\node[anchor=north west,fill=blue,text=white,minimum size=0.2\height,font=\Large,inner sep=0pt,outer sep=0pt] at (0,\height) {T};
我做错了什么或者这里发生了什么事?
答案1
这是因为,您用来定位节点的north west
形状的锚点位于形状的边框上。至于形状,锚点也位于形状的边框上,但此处它恰好与形状边界框的左上角重合。circle
rectangle
north west
\documentclass[tikz]{standalone}
\begin{document}
\Huge
\begin{tikzpicture}
\node[name=s, shape=circle, line width=10pt, draw=black!15, fill=yellow!15, inner sep=1cm] {circle\vrule width 1pt height 2cm};
\edef\northwestanchor{north west}
\foreach \anchor/\placement in
{north west/above left, north/above, north east/above right,
west/left, center/above, east/right,
mid west/right, mid/above, mid east/left,
base west/left, base/below, base east/right,
south west/below left, south/below, south east/below right,
text/left, 10/right, 130/above}
\draw[shift=(s.\anchor), \ifx\anchor\northwestanchor red\fi] plot[mark=x] coordinates{(0,0)}
node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{document}
由于您知道节点的高度和宽度,因此您可以将节点向下和向右移动:
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\newlength{\width}
\setlength{\width}{16cm}
\newlength{\height}
\setlength{\height}{10cm}
\begin{document}
\begin{tikzpicture}
\node[fill=gray,minimum width=\width,minimum height=\height] at (0.5\width,0.5\height) {};
\draw[green] (0,0) |- (\width,\height);
\node[circle,shift={(0.1\height,-0.1\height)},fill=blue,text=white,minimum size=0.2\height,font=\Large,inner sep=0pt,outer sep=0pt] at (0,\height) {T};
\end{tikzpicture}
\end{document}
答案2
尽管贾斯珀·哈比希特已经解释了问题的原因(north west
位于边界上),为了获得更灵活的解决方案,您可以定义单独的锚点,就像我在下面的代码中所做的那样corner north west
。
我从我的positioning-plus
库。其他对角线的锚点也在此处定义。
这是一个通用锚点,意味着每个形状都可以使用它。当然,对于更复杂的形状,这仍然不会返回所需的点。
代码
\documentclass[tikz]{standalone}
\newlength{\width} \setlength{\width}{16cm}
\newlength{\height}\setlength{\height}{10cm}
\makeatletter
\pgfdeclaregenericanchor{corner north west}{%
\pgf@sh@reanchor{#1}{north}%
\pgf@ya\pgf@y
\pgf@process{\pgf@sh@reanchor{#1}{west}}%
\pgf@y\pgf@ya}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[fill=gray,minimum width=\width,minimum height=\height] at (0.5\width,0.5\height) {};
\draw[green] (0,0) |- (\width,\height);
% ......
\node[circle,anchor=corner north west,fill=blue,text=white,
minimum size=0.2\height,font=\Large,inner sep=0pt,outer sep=0pt] at (0,\height) {T};
\end{tikzpicture}
\end{document}
答案3
这将首先绘制圆圈,然后将其他所有东西(背景层)相对于它定位。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc,backgrounds}
\newlength{\width}
\setlength{\width}{16cm}
\newlength{\height}
\setlength{\height}{10cm}
\begin{document}
\begin{tikzpicture}
\node[circle,fill=blue,text=white,minimum size=0.2\height,font=\Large,inner sep=0pt,outer sep=0pt] (T) {T};
\coordinate (origin) at ($(T.north -| T.west) + (0,-\height)$);
\begin{scope}[shift=(origin), on background layer]
\node[fill=gray,minimum width=\width,minimum height=\height] at (0.5\width,0.5\height) {};
\draw[green] (0,0) |- (\width,\height);
\end{scope}
\end{tikzpicture}
\end{document}