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}

関連情報