我有这个状态机:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, graphs, graphdrawing, quotes, automata}
\usegdlibrary{circular}
\begin{document}
\tikz[>=stealth, shorten >=1pt] {
\graph[simple necklace layout, nodes = {state}, node sep = 1cm, grow=right] {
a[initial, initial text={}],
b, c, d[accepting], e, f,
a
->["$a$"] f
->[loop above, "$a$"] f
->[bend left, "$b$"] e
->[bend left, "$a$"] f,
e
-> ["$b$"] d
-> ["$a, b$", loop right] d,
c
->["$b$"] d,
c
->["$a$", bend left] b
->["$a$", loop below] b
->["$b$", bend left] c,
a
->["$b$"] b
;
}
}
\end{document}
我想“压平”图形布局,使其变成椭圆形,但不缩放整个图片(因为这会弄乱一切)。有办法吗?
答案1
可以通过选项移动节点yshift
。节点f
和e
向下和b
向上移动c
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, graphs, graphdrawing, quotes, automata}
\usegdlibrary{circular}
\begin{document}
\tikz[>=stealth, shorten >=1pt] {
\graph[simple necklace layout, nodes = {state}, node sep = 1cm,
grow=right] {
a[initial, initial text={}],
b[yshift=5mm], c[yshift=5mm], d[accepting],
e[yshift=-5mm], f[yshift=-5mm],
a
->["$a$"] f
->[loop above, "$a$"] f
->[bend left, "$b$"] e
->[bend left, "$a$"] f,
e
-> ["$b$"] d
-> ["$a, b$", loop right] d,
c
->["$b$"] d,
c
->["$a$", bend left] b
->["$a$", loop below] b
->["$b$", bend left] c,
a
->["$b$"] b
;
}
}
\end{document}
或者使用一些间隔更均匀的节点:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, graphs, graphdrawing, quotes, automata}
\usegdlibrary{circular}
\begin{document}
\tikz[>=stealth, shorten >=1pt] {
\graph[simple necklace layout, nodes = {state}, node sep = 1cm,
grow=right] {
a[initial, initial text={}],
b[yshift=5mm, xshift=1.5mm], c[yshift=5mm, xshift=-1.5mm],
d[accepting],
e[yshift=-5mm, xshift=-1.5mm], f[yshift=-5mm, xshift=1.5mm],
a
->["$a$"] f
->[loop above, "$a$"] f
->[bend left, "$b$"] e
->[bend left, "$a$"] f,
e
-> ["$b$"] d
-> ["$a, b$", loop right] d,
c
->["$b$"] d,
c
->["$a$", bend left] b
->["$a$", loop below] b
->["$b$", bend left] c,
a
->["$b$"] b
;
}
}
\end{document}
答案2
对于这种情况,我能够使用 找到解决方法layered layout
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, graphs, graphdrawing, quotes, automata}
\usegdlibrary{layered}
\begin{document}
\tikz[>=stealth, shorten >=1pt] {
\graph[layered layout, nodes = {state}, level sep = 1cm, sibling
distance = 2cm, grow=right] {
a[initial, initial text={}] -!- {
b -!- {
c
},
f -!- {
e
}
} -!-
d[accepting],
a
->["$a$"] f
->[loop above, "$a$"] f
->[bend left, "$b$"] e
->[bend left, "$a$"] f,
e
-> ["$b$"] d
-> ["$a, b$", loop right] d,
c
->["$b$"] d,
c
->["$a$", bend left] b
->["$a$", loop below] b
->["$b$", bend left] c,
a
->["$b$"] b
;
}
}
\end{document}
但是,如果顶点数为奇数,这会更加棘手。此外,这种布局不允许我直接控制节点距离。但我很满意。