我正在尝试绘制一个类似于这个的图形Warrington, GS (2005). 概率的杂耍. 美国数学月刊, 112(2), 105-118,使用 LaTeX 中的 TiKz 包
我知道如何创建矩形作为节点,并用箭头连接节点。但是,我不知道如何在节点内绘制白色和黑色圆圈。这是我的一些代码:
\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=2.5,->,>=stealth',shorten >=1pt,auto,inner sep=2pt,semithick,bend angle=20]
\node[rectangle,draw] (A) at (0,0) {Rectangle};
\node[rectangle,draw] (B) at (1,0) {Rectangle};
\node[rectangle,draw] (C) at (2,0) {Rectangle};
\node[rectangle,draw] (D) at (3,0) {Rectangle};
\node[rectangle,draw] (E) at (4,0) {Rectangle};
\path (A) edge [bend right,looseness=0.9] node[swap] {$1$} (B)
(B) edge [bend right,looseness=0.9] node[swap] {$\dfrac{1}{4}$} (A)
edge [bend right,looseness=0.9] node[swap] {$\dfrac{3}{4}$} (C)
(C) edge [bend right,looseness=0.9] node[swap] {$\dfrac{1}{2}$} (B)
edge [bend right,looseness=0.9] node[swap] {$\dfrac{1}{2}$} (D)
(D) edge [bend right,looseness=0.9] node[swap] {$\dfrac{3}{4}$} (C)
edge [bend right,looseness=0.9] node[swap] {$\dfrac{1}{4}$} (E)
(E) edge [bend right,looseness=0.9] node[swap] {$1$} (D);
\end{tikzpicture}
\caption{Transition}
\end{center}
\end{figure}
答案1
这里有一个可能的解决方案,其中包含一些tabular
节点文本:
\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}
\tikzset{
mynode/.style ={
draw,
rectangle,
scale=.5
},
mydescr/.style={
swap,
text height=3.6ex,
text depth=2ex
},
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[->,>=Stealth,shorten >=1pt,auto,inner sep=2pt,semithick,bend angle=20]
\node[mynode] (A) {\begin{tabular}{*4C}
\smile & \bullet & \bullet & \bullet \\
\end{tabular}};
\node[mynode, right=of A] (B) {\begin{tabular}{*4C}
\bullet & \smile & \bullet & \bullet \\
\end{tabular}};
\node[mynode, right=of B] (C) {\begin{tabular}{*4C}
\bullet & \bullet & \smile & \bullet \\
\end{tabular}};
\node[mynode, right=of C] (D) {\begin{tabular}{*4C}
\bullet & \bullet & \bullet & \smile \\
\end{tabular}};
\node[mynode, right=of D] (E) {\begin{tabular}{*4C}
\smile & \bullet & \bullet & \bullet \\
\end{tabular}};
\path (A) edge [bend right,looseness=0.9] node[mydescr] {$1$} (B)
(B) edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{1}{4}$} (A)
edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{3}{4}$} (C)
(C) edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{1}{2}$} (B)
edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{1}{2}$} (D)
(D) edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{3}{4}$} (C)
edge [bend right,looseness=0.9] node[mydescr] {$\dfrac{1}{4}$} (E)
(E) edge [bend right,looseness=0.9] node[mydescr] {$1$} (D);
\end{tikzpicture}
\caption{Transition}
\end{figure}
\end{document}
答案2
另一种方法是使用\pic
。的参数定义pic
四个符号中的哪一个不绘制为项目符号。
我还稍微简化了箭头绘制。我没有bend right,looseness=1
为每个edge
和swap
指定每个节点,而是将这些选项添加到 中一次。我添加了库,\path
而不是,这样您就可以使用简写。node {foo}
quotes
edge["foo"]
\documentclass[border=10pt]{standalone}
\usepackage{tikz,amsmath}
\usetikzlibrary{quotes} % to use the "<label>" syntax for to-paths, e.g. (a) to["foo"] (b)
\tikzset{
juggle/.pic={
\foreach \x in {1,...,4}
{
\ifnum \x=#1
\draw (\x cm/3-4pt,0) to[bend right] ++(8pt,0);
\else
\fill (\x/3,0) circle[radius=2pt];
\fi
}
\node[draw,above right,name=-outline,minimum width=1.6cm,minimum height=5mm] at (0,-0.25) {};
}
}
\begin{document}
\begin{tikzpicture}[
arr/.style={
-stealth,
shorten >=3pt,
shorten <=3pt,
swap
}
]
\pic (A) at (0,0) {juggle=1};
\pic (B) at (2,0) {juggle=2};
\pic (C) at (4,0) {juggle=3};
\pic (D) at (6,0) {juggle=4};
\pic (E) at (8,0) {juggle=1};
\path [swap,-stealth,bend right=45] % set looseness here as well, if needed
(A-outline) edge ["$1$"] (B-outline)
(B-outline) edge ["$\dfrac{1}{4}$"] (A-outline)
edge ["$\dfrac{3}{4}$"] (C-outline)
(C-outline) edge ["$\dfrac{1}{2}$"] (B-outline)
edge ["$\dfrac{1}{2}$"] (D-outline)
(D-outline) edge ["$\dfrac{3}{4}$"] (C-outline)
edge ["$\dfrac{1}{4}$"] (E-outline)
(E-outline) edge ["$1$"] (D-outline);
\end{tikzpicture}
\end{document}
旧答案
\documentclass[border=2pt]{standalone}
\usepackage{tikz,amsmath}
\usetikzlibrary{quotes} % to use the "<label>" syntax for to-paths, e.g. (a) to["foo"] (b)
\tikzset{
juggle/.pic={
\foreach \x in {1,...,4}
{
\ifnum \x=#1
\draw (\x cm/2-4pt,0) to[bend right] ++(8pt,0);
\else
\fill (\x/2,0) circle[radius=2pt];
\fi
}
\node[draw,above right,name=-outline,minimum width=2cm,minimum height=5mm] at (0.25,-0.25) {};
}
}
\begin{document}
\begin{tikzpicture}[
arr/.style={
-stealth,
shorten >=3pt,
shorten <=3pt,
swap
}
]
\pic (A) at (0,0) {juggle=3};
\pic (B) at (4,0) {juggle=1};
\pic (C) at (2,1.5) {juggle=4};
\pic (D) at (2,-1.5) {juggle=2};
\draw [arr] (D-outline) to["1"] (C-outline);
\draw [arr] (A-outline) to["4"] (D-outline);
\draw [arr] (D-outline) to["4"] (B-outline);
\draw [arr] (B-outline) to["0"] (C-outline);
\draw [arr] (A-outline) to["2"] (C-outline);
\draw [arr] (C-outline.south west) to["4"] (A-outline.140);
\draw (C-outline) edge[every loop/.append style={-stealth},loop above,"3"] (C-outline);
\end{tikzpicture}
\end{document}