我需要使用 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}