TikZ: круговая нумерация с перекрывающимся квадратом

TikZ: круговая нумерация с перекрывающимся квадратом

Мне нужно пронумеровать узлы круга с помощью TikZ, так чтобы это число находилось внутри небольшого квадрата, который перекрывается с кругом сверху (или снизу, или слева, или справа). Мне удалось сделать это отдельно, сначала нарисовав квадрат, а затем круг, чтобы они перекрывались так, как мне нужно. Однако, учитывая, что мне нужно нарисовать много таких узлов, мне было интересно, есть ли способ сделать это лучше, чтобы я мог создать круг с его квадратом вместе, не выясняя их относительное положение по отношению друг к другу. Может быть, сделать квадрат частью украшения круга? Есть идеи?

Вот что я имею в виду:

введите описание изображения здесь

А вот пример кода, воспроизводящего это:

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[
    C/.style = {circle, draw, fill=white, minimum size=#1, inner sep=0mm},
    S/.style = {rectangle, draw, fill=white, minimum size=#1, inner sep=0mm},
                   ]
\node[S=1cm, text depth = 0.5 cm,anchor=north] (a) at (0,0) {1};  % square
\node[C=4cm, below = -0.5cm of a] {label};                        % circle

\end{tikzpicture}
\end{document}

решение1

Итак, есть много вариантов, вот один из них, основанный на картинках


(Мне было немного весело, делая его достаточно настраиваемым, заставляя его работать по кругу, настраивать расстояние и т. д. ПРАВКА: я также добавил больше параметров конфигурации, чтобы текст можно было поворачивать):

введите описание изображения здесь

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{backgrounds,matrix}
\begin{document}
\tikzset{
  C/.style = {circle, draw, fill=white, minimum size=#1, inner sep=0mm},
  apply style/.code={\pgfkeysalso{#1}},
  apply style in macro/.style={apply style/.expand once={#1}},
  my decorator pic/.pic={
    \node[anchor=\myAnchor,rotate=-\myRotationAngleText,apply style in macro=\myExtraStyleText,](labelNode){\myText};
    \begin{scope}[on background layer]
      \node[
        rotate fit={\myAngle},
        fit=(labelNode)(\myCurrentNode.center),
        rectangle,
        fill=\myColor,
        minimum size=\myMinimumSize,
        inner sep=0mm,
        apply style in macro=\myExtraStyle,
      ]
      {};
    \end{scope}
  },
  add deco/.style={
    append after command={
      \pgfextra{%
        \edef\myCurrentNode{\tikzlastnode}%
        \def\myAnchor{\myAngle+180}%
        \pgfkeysalso{
        set text/.store in=\myText,
        set color/.store in=\myColor,
        set color=pink!50!white,
        set position angle/.store in=\myAngle,
        set position angle=90,
        set minimum size/.store in=\myMinimumSize,
        set minimum size=8mm,
        set distance/.store in=\myDistance,
        set distance=0mm,
        set extra style/.store in=\myExtraStyle,
        set extra style=,
        set extra style text/.store in=\myExtraStyleText,
        set extra style text=,
        set text depth/.store in=\myTextDepth,
        rotate text angle/.store in=\myRotationAngleText,
        rotate text angle=0,
        rotate text bottom/.style={
          rotate text angle=\myAngle-90,
          /utils/exec={\def\myAnchor{90}},
        },
        rotate text top/.style={
          rotate text angle=\myAngle+90,
          /utils/exec={\def\myAnchor{-90}},
        },
        set text depth=2mm,
        #1,
      }}
    {($(\myCurrentNode.\myAngle)+(\myAngle:\myDistance)$) pic{my decorator pic}}
    }
  },
}

\begin{tikzpicture}
  \node[C=4cm,
    add deco={set text=foo, set extra style={fill=red}},
    add deco={set text=foo, set position angle=-90, set extra style={fill=red}},
    add deco={set text=foo, set position angle=-90+25, rotate text bottom, set extra style={fill=red}, set extra style text={white}},
    add deco={set text=foo, set position angle=-90-25, rotate text top, set extra style={fill=yellow}, set extra style text={orange}},
    add deco={set text=foo, set position angle=90+25, rotate text top, set extra style={fill=green}, set extra style text={white}},
    add deco={set text=foo, set position angle=90-25, rotate text bottom, set extra style={fill=orange}, set extra style text={white}},
  ] (expl1) {Top label};
\node[C=4cm, add deco={set text=bar, set position angle=-90}, below=of expl1] (expl2) {Bottom label};
\node[C=4cm, add deco={set text=croco, set distance=10mm, set position angle=-90}, below=of expl2] (expl3) {Bottom label};
\node[C=2cm, add deco={set text=baz, set position angle=0}, below right=of expl1] {Right label};
\node[C=2cm, add deco={set text=bar, set position angle=180+45}, below left=of expl1] {Left label};
\end{tikzpicture}
\end{document}

Связанный контент