我在 tikz 中绘制了一个图表,在单独的 tex 文件中,我通过输入和调用来调用它\TikzExampleLargeKtn{6}{1}{1}
(例如)。我没有使用,tkz-graph
但我愿意接受它。
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc}
\tikzstyle{vertex}=[circle, draw, fill=black, inner sep=0pt, minimum size=3pt] %style of nodes.
\newcommand{\vertex}{\node[vertex]} %shorthand in place of \node[vertex]
\newcounter{Angle}
\newcommand{\TikzExampleLargeKtn}[3]{
\begin{tikzpicture}[x=#2 cm, y=#3 cm]
%creating a complete graph on #1 vertices
\foreach \i in {1, 2,..., #1} {
\setcounter{Angle}{ (\i-1) * 360 / #1}
\vertex (c\i) at (\theAngle:1) [label=\theAngle:]{};
}
%drawing the edges, I known every edge is defined twice but okay
\path
\foreach \i in {1, 2,..., #1} {
\foreach \j in {1, 2,..., #1} {
(c\i) edge[color=blue] (c\j)
}
};
%Adding an ellipse shape
\draw[fill=gray, opacity=0.2] (-2,0) ellipse (3 and 2);
\end{tikzpicture}
}
输出是期望的:
但现在我想将此构造“附加”到另一个完全图的每个顶点。一般输出应如下所示,但“灰色/蓝色斑点”位于红色图的 6 个顶点中的每一个顶点以及与椭圆接触的顶点。
为便于参考,我以类似的方式创建了红色图表,例如
\begin{tikzpicture}[x=4 cm, y=4 cm]
\foreach \i in {1, 2,..., 6} {
\setcounter{Angle}{ (\i-1) * 360 / 6}
\vertex (c\i) at (\theAngle:1) [label=\theAngle:]{};
}
\path %I known every edge is defined twice but okay
\foreach \i in {1, 2,..., 6} {
\foreach \j in {1, 2,..., 6} {
(c\i) edge[color=red,line width=\widthedge pt] (c\j)}};
\end{tikzpicture}
有没有简单的方法可以做到这一点?我想我可能必须彻底检查一下我绘制这些图表的方式。我根据围绕 (0,0) 的简单旋转来绘制它们,但现在不可能了。
答案1
我认为谢布·格拉夫,我会pic
在这里寻找解决方案。您可以pic
为重复的图表(hex
在我的示例中)创建一个,并根据需要重复使用它。
例如:
\documentclass[tikz,border=2mm]{standalone}
\tikzset
{%
pics/hex/.style={% #1 = edge lenght = radius
code={%
\foreach\i in {1,...,6}
\coordinate (-\i) at (60*\i-60:#1);
\foreach\i in {1,...,6}
{%
\foreach\j in {\i,...,6}
\draw (-\i) -- (-\j);
\fill[draw=none] (-\i) circle (0.05*#1);
}
}},
}
\begin{document}
\begin{tikzpicture}
\foreach\i in {1,...,6}
{%
\pgfmathsetmacro\a{60*\i-60}
\begin{scope}[shift={(\a:3.5)},rotate=\a]
\draw[gray,fill=gray!20] (0,0) ellipse (1.5 and 1);
\pic[draw=blue] at (-1,0) {hex=0.5};
\end{scope}
}
\pic[draw=red,thick] {hex=2};
\end{tikzpicture}
\end{document}
答案2
与 Juan Cataño 的解决方案类似,但使用了两张图片
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric, backgrounds}
\tikzset{
pics/hexagon/.style n args={3}{
code= {
\node[regular polygon, regular polygon sides=6, minimum size=#2, shape border rotate=-60, anchor=east] (#1) {};
\foreach \i in {1,...,6} {
\foreach \j in {\i,...,6}{
\draw[#3] (#1.corner \i)--(#1.corner \j);}
\fill (#1.corner \i) circle (.5pt);
}
}
},
pics/cloud/.style n args={3}{
code= {
\node[regular polygon, regular polygon sides=6, minimum size=#2, shape border rotate=-60, anchor=east] (#1) {};
\foreach \i in {1,...,6} {
\foreach \j in {\i,...,6}{
\draw[#3] (#1.corner \i)--(#1.corner \j);}
\fill (#1.corner \i) circle (.5pt);
}
\begin{scope}[on background layer]
\node[ellipse, fill=gray, opacity=.2, anchor=east, minimum size=1.5cm, minimum height=1cm] at (#1.corner 1) {};
\end{scope}
}
},
}
\begin{document}
\begin{tikzpicture}
\pic (central) {hexagon={c}{2cm}{red}};
\pic[scale=-1, transform shape] (right) at (centralc.corner 1) {cloud={r}{.5cm}{blue}};
\pic[scale=-1, rotate=60, transform shape] (topright) at (centralc.corner 2) {cloud={tr}{.5cm}{blue}};
\pic[rotate=-60,transform shape] (topleft) at (centralc.corner 3) {cloud={tl}{.5cm}{blue}};
\pic (left) at (centralc.corner 4) {cloud={l}{.5cm}{blue}};
\pic[rotate=60, transform shape] (bottomleft) at (centralc.corner 5) {cloud={bl}{.5cm}{blue}};
\pic[scale=-1, rotate=-60, transform shape] (bottomright) at (centralc.corner 6) {cloud={br}{.5cm}{blue}};
\end{tikzpicture}
\end{document}