![TikZ:带有重叠正方形的编号圆](https://linux22.com/image/472184/TikZ%EF%BC%9A%E5%B8%A6%E6%9C%89%E9%87%8D%E5%8F%A0%E6%AD%A3%E6%96%B9%E5%BD%A2%E7%9A%84%E7%BC%96%E5%8F%B7%E5%9C%86.png)
我需要使用 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}