4

I have a regular polygon, and I'd like to position nodes for instance above the first corner, or next to the nth side.

This is my sample:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,positioning}

\begin{document}
\begin{tikzpicture}
    \node[draw, thick, minimum size=4cm, regular polygon,
      regular polygon sides=5] (polygon) {};
    \node[draw, above of=polygon] (a) {a};
    \node[draw, left of=polygon.corner 3] (b) {b};
    \draw (a) to (b);
  \end{tikzpicture}
\end{document}

I get this error:

! Package pgf Error: No shape named polygon.corner 3 is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.10 ...e[draw, left of=polygon.corner 3] (b) {b};

…which is weird because I can draw edges like this (it works):

\foreach \i in {1, ..., 5} \draw (outer.center) -- (outer.corner \i);

My end goal is to put in overlay the dual of this graph: graph

Thanks.

2 Answers2

2

The solution is not to use the syntax above of=polygon.corner 1 but above=of polygon.corner 1. I find out that the previous syntax was depreciated, as described in Difference between “right of=” and “right=of” in PGF/TikZ.

1

You are absolutely right when you say that the syntax you have used is deprecated, and should not be used. And I fully agree that you should switch to the positioning syntax (of course after loading the library).

This here is to point out that your observation has nothing to do with the polygon shape in particular. Rather, the deprecated syntax happens to fail for all node anchors. This can be seen from the even more basic MWE

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \node at (0,0) (A) {A};
    \coordinate (P) at (1,0) ;
    \node[draw,left of=A] (B) {B};
    %\node[draw,left of=B.south] (C) {C}; % <- fails
    \node[draw,right of=P] (D) {D};
    %\node[draw,right of=P.center] (D) {D};% <- fails
  \end{tikzpicture}
\end{document}

where I commented out all nonworking codes. And Caramdir's nice answer also provides us with the relevant code bits

% The following are deprecated:
\tikzoption{above of}{\tikz@of{#1}{90}}%
\tikzoption{below of}{\tikz@of{#1}{-90}}%
\tikzoption{left of}{\tikz@of{#1}{180}}%
\tikzoption{right of}{\tikz@of{#1}{0}}%
\tikzoption{above left of}{\tikz@of{#1}{135}}%
\tikzoption{below left of}{\tikz@of{#1}{-135}}%
\tikzoption{above right of}{\tikz@of{#1}{45}}%
\tikzoption{below right of}{\tikz@of{#1}{-45}}%
\def\tikz@of#1#2{%
  \def\tikz@anchor{center}%
  \let\tikz@do@auto@anchor=\relax%
  \tikz@addtransform{%
    \expandafter\tikz@extract@node@dist\tikz@node@distance and\pgf@stop%
    \pgftransformshift{\pgfpointpolar{#2}{\tikz@extracted@node@distance}}}%
  \def\tikz@node@at{\pgfpointanchor{#1}{center}}}
\def\tikz@extract@node@dist#1and#2\pgf@stop{%
  \def\tikz@extracted@node@distance{#1}}

that allow us why that happens. It contains \def\tikz@node@at{\pgfpointanchor{#1}{center}}} which tells us that TikZ attempts to access the center anchor of whatever #1 is. If #1 is a node or even a coordinate (which is technically also a node), this works fine, however it does not if #1 is already an anchor. In the latter case, TikZ tries to access something like B.south.center in the first nonworking example of the above code, which does not exist. Of course, the error message is then a bit cryptic, but with this understanding one can sort of defend it.