6

I want to place a circle in the corner of a gray background and this is the code:

\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}

However the circle is not in the upper left corner as expected: enter image description here

Now leave out the circle and try it again with:

\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};

And it works: enter image description here

What am I doing wrong or what is going on here?

0

3 Answers 3

10

Well this is, because the north west anchor of the shape circle, which you are using to position the node, sits on the shape's border. As for the shape rectangle, the north west anchor also sits on the border of the shape but here it happens to coincide with the upper left corner of the shape's bounding box.

\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}

enter image description here


You can shift the node downwards and to the right since you know its height and width:

\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}

enter image description here

7

While Jasper Habicht already explained the cause for your problem (north west lies on the border), for a more flexible solution you can define separate anchors, like I did in the code below for corner north west.

I took this from my positioning-plus library. Anchors for the other diagonals are defined there, too.

This is a general anchor, meaning every shape will be able to use it. Though, of course, for the more complex shapes, this will still not return the needed point.

Code

\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}
1
  • 1
    If you'd name the gray node, with the library, you could say corner east below=0cm of <gray node>.north west. Aug 4 at 10:14
2

This draws the circle first and positions everything else (background layer) relative to it.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.