在以下绘制图形的答案中,有没有办法使用(x,y)坐标而不是角度来确定顶点的位置以绘制相同的图形?
https://tex.stackexchange.com/a/653829/148579
我期望绘制类似下图的图形:
编辑:编译错误:
我使用下面的代码document class
,编译由 Overleaf 完成。屏幕截图显示了编译后收到的错误:
如您所见,begin document
插入之后为:
答案1
这Sandy G 的回答已经回答了您的初始问题,但我想提供另一种使用 TikZ 绘制图片的方法。该密钥decide color
可能也适用于其他答案,因为您不需要在列表中隐式指定颜色。
在LuaLaTeX的支持下我们可以使用TikZgraphdrawing
及其force
库。
这force
图书馆()规定\usegdlibrary{force}
spring electrical layout
。
这graphs
图书馆()规定\usetikzlibrary{graphs}
typeset
钥匙用于定义依赖于节点名称的节点文本。
此名称还将用于decide color
应用适当的颜色样式:
decide color/.style 2 args={
/utils/TeX if=c#1
{% if the first letter is c
% and the number after is is smaller than 5 → bluelight
% otherwise → bluedark
/utils/if={#2<5}{bluelight}{bluedark}
}{% if the first letter is not c (i.e. p)
% and the number after it is smaller than 8 → light
% otherwuse → dark
/utils/if={#2<8}{light}{dark}
}
}
代码
% arara: lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\makeatletter
\pgfkeys{
/utils/if/.code n args={3}{%
\pgfmathparse{#1}\ifdim\pgfmathresult pt=0pt\relax
\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi
{\pgfkeysalso{#3}}{\pgfkeysalso{#2}}},
/utils/TeX if/.code n args={4}{%
\if#1#2\expandafter\pgfutil@firstoftwo\else\expandafter\pgfutil@secondoftwo\fi
{\pgfkeysalso{#3}}{\pgfkeysalso{#4}}}}
\makeatother
\tikzset{
mynode/.style={
circle, minimum size=10mm, draw, densely dashdotted, thick,
decide color/.expand once=#1},
decide color/.style 2 args={
/utils/TeX if=c#1% c1-c4 / c5- / ?1-?7 / ?8-
{/utils/if={#2<5}{bluelight}{bluedark}}
{/utils/if={#2<8}{light}{dark}}},
light/.style={fill=gray!20}, bluelight/.style={fill=blue!10},
dark/.style ={fill=gray!60}, bluedark/.style ={fill=blue!30}}
\begin{document}
\tikz\graph[
spring electrical layout, horizontal=c2 to p13,
node distance=1.5cm, typeset=$n_{\tikzgraphnodetext}$,
nodes={mynode=\tikzgraphnodetext}] {
% outer ring
c2 -- {p1, p11, p6};
p1 -- {p8, c6, p11};
p8 -- {p3, p10, c6};
p3 -- {p13, p15, p10};
p13 -- {p15, c7};
c7 -- {c3, c4, p15};
c3 -- {p14, c4};
p14 -- {p7, c4};
p7 -- {p9, p2, c4};
p9 -- {c5, p12, p2};
c5 -- {c1, p4, p12};
c1 -- {p6, p4};
p6 -- {p11, p4};
% inner ring
p11 -- {c6, p12, p4};
p5 -- {c6 -- {p10, p12}, p10 -- p15, p15 -- c4, c4 -- p2, p2 -- p12, p12 -- p4};
% last line is equivalent to:
% c6 -- {p10, p5, p12}; p10 -- {p15, p5}; p15 -- {c4, p5};
% c4 -- {p2, p5}; p2 -- {p12, p5}; p12 -- {p4, p5};
};
\end{document}
输出
答案2
这应该可以让你开始。制作两个 for 循环:第一个用于命名和绘制节点,第二个用于绘制边。节点列表中的每个条目都应具有以下形式<x>/<y>/<fillcolor>/<label>/<name>
。对于边列表,请使用第一个 for 循环中的名称。
\documentclass{article}
\usepackage{tikz}
\tikzset{mynode/.style={circle, minimum size=12mm, draw, densely dashdotted}}
\begin{document}
\colorlet{color1}{gray!20}
\colorlet{color2}{gray!60}
\colorlet{color3}{blue!20}
\begin{tikzpicture}
\foreach \x/\y/\col/\lab/\nam in
{0/0/color1/n_{p1}/p1, -1/3/color2/n_{p2}/p2, 1/5/color1/n_{p5}/p5, 4/4/color2/n_{c5}/c5, 5/1/color3/n_{c1}/c1, 3/0/color3/n_{p11}/p11, 2/2/color1/n_{c2}/c2}
{\node[mynode, fill=\col] (\nam) at (\x,\y){$\lab$};}
\foreach \p/\q in {p5/c5, c5/c1, c1/p11, p11/p1, p1/p2, p2/p5, c2/c5, c2/p11, c2/c1, c2/p1, c2/p2, c2/p5}
{\draw (\p)--(\q);}
\end{tikzpicture}
\end{document}