我正在尝试使用 TikZ 创建与以下图表等效的图表:
我对如何在 TikZ 中创建单个组件/子图有扎实的了解。假设我将它们每个都保存在相应的文件中。例如,如下initial-state.tex
所示:
\documentclass[12pt,tikz]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage[T1]{fontenc}
\usepackage{stix}
\usepackage[english]{babel}
\usepackage[kerning,spacing,babel,final]{microtype}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,matrix}
\tikzset{
>=stealth,
auto,
font=\scriptsize,
point/.style={circle,draw,very thick,align=center,
anchor=center},
Point/.style={double,circle,draw,very thick,align=center,
anchor=center},
transform shape,
}
\begin{document}
\begin{tikzpicture}
\centering
\matrix (m) [matrix of nodes, row sep=0.5cm,column sep=1.25cm] {
&
\node (1) [point] {$AB$};
& &
\node (2) [point] {$B$}; \\
\node (3) [Point] {$ABC$};
& &
\node (4) [point] {$BC$};
& \\ &
\node (5) [point] {$A$};
& &
\node (6) [point] {}; \\
\node (7) [point] {$AC$};
& &
\node (8) [point] {$C$};
& \\
};
\path[solid]
(1) edge [loop above,very thick] (1)
(2) edge [loop above,very thick] (2)
(3) edge [loop above,very thick] (3)
(4) edge [loop above,very thick] (4)
(5) edge [loop below,very thick] (5)
(6) edge [loop below,very thick] (6)
(7) edge [loop below,very thick] (7)
(8) edge [loop below,very thick] (8)
(1) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (2)
(1) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (3)
(2) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (4)
(2) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (6)
(3) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (4)
(3) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (7)
(4) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (8)
(6) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (8)
(7) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (8)
;
\path[dotted]
(1) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (5)
(5) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (6)
(5) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (7)
;
\end{tikzpicture}
\end{document}
图中其他子图也类似。我该如何绘制更大的图形?具体来说:
- 我如何定位两个较大子图之间的边缘?
- 如何才能在边缘放置两个标签:一个是图形本身,另一个是简单的文本标签,如附图所示?
我尝试阅读 TikZ 文档,但无法找到如何像示例中那样沿边定位节点。我可以使用其他工具(例如 OmniGraffle fairy)轻松完成此操作,但如果我可以在 TikZ 中完成所有这些操作,我更喜欢使用 TikZ。
答案1
我会将两个\matrix
es 放在同一个tikzpicture
名为m
和中m2
,然后您可以轻松地用 绘制连接箭头\draw [->] (m) -- (m2);
。
您可以在路径上放置多个节点。例如,
\draw (a) -- (b)
node[above,midway] {foo}
node[below,midway] {bar};
下面的代码就是所使用的基本原理。
我还建议使用循环来绘制每个矩阵中的连接。您可以根据需要使用或忽略此方法。
您还指定了matrix of nodes
(我将其更改为matrix of math nodes
),但您没有使用它。使用该选项,并nodes={point}
设置节点的一般样式,您可以A
在单元格中写入,而不是\node [point] {A};
。此类节点会自动命名为<matrix name>-<row number>-<column number>
,例如m-3-2
。
\documentclass[12pt,tikz]{standalone}
%\usepackage{amsmath} %loaded by mathtools
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage[T1]{fontenc}
\usepackage{stix}
\usepackage[english]{babel}
\usepackage[kerning,spacing,babel,final]{microtype}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,matrix}
\tikzset{
>=stealth,
auto,
font=\scriptsize,
point/.style={circle,draw,very thick,align=center,
anchor=center},
Point/.style={double,circle,draw,very thick,align=center,
anchor=center},
transform shape,
}
\begin{document}
\begin{tikzpicture}
% left matrix
\matrix (m) [matrix of math nodes, nodes={point}, row sep=0.5cm,column sep=1.25cm] {
& AB & & B \\
|[Point]|ABC & & BC & \\
& A & & {} \\
AC & & C & \\
};
% right matrix
\matrix (m2) [right=6cm of m, matrix of math nodes, nodes={point}, row sep=0.5cm,column sep=1.25cm] {
& AB & & B \\
|[Point]|ABC & & BC & \\
& A & & \\
AC & & C & \\
};
% draw the lines that only occur in the left matrix
\draw
(m-3-4) edge [loop below,very thick] (m-3-4)
(m-1-4) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (m-3-4)
(m-3-4) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (m-4-3);
\draw[dotted]
(m-3-2) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (m-3-4);
% draw the lines that occur in both
\foreach \MAT in {m,m2}
{
\foreach \cell in {1-2,1-4,2-1,2-3}
\draw (\MAT-\cell) edge [loop above,very thick] (\MAT-\cell);
\foreach \cell in {3-2,4-1,4-3}
\draw (\MAT-\cell) edge [loop below,very thick] (\MAT-\cell);
\draw
(\MAT-1-2) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (\MAT-1-4)
(\MAT-1-2) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (\MAT-2-1)
(\MAT-1-4) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (\MAT-2-3)
(\MAT-2-1) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (\MAT-2-3)
(\MAT-2-1) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (\MAT-4-1)
(\MAT-2-3) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (\MAT-4-3)
(\MAT-4-1) [<->,very thick] edge node[above right, pos=0.75,
minimum size=0pt] {$A$} (\MAT-4-3)
;
\draw[dotted]
(\MAT-1-2) [<->,very thick] edge node[above left, pos=0.4,
minimum size=0pt] {$B$} (\MAT-3-2)
(\MAT-3-2) [<->,very thick] edge node[above, minimum size=0pt] {$C$} (\MAT-4-1)
;
}
% connecting arrow
\draw [-stealth,shorten <=10mm,shorten >=10mm,ultra thick] (m) -- (m2)
node[midway,below] {declare}
node[midway,above=1cm,draw,rounded corners,ultra thick,align=center,label=below:$c_p$] (x)
{ $p : \varphi$ \\ $ s : c$ };
% loop above node
\draw (x) edge [loop above,very thick] node[above]{$A,B,C$} (x);
\end{tikzpicture}
\end{document}