我正在写关于队列理论的论文,在 LaTeX 中使用 TikZ 库时遇到了很多困难。我设法得到了第一行,但不知何故我无法继续下去。有人能帮我把下面的图片编码成 LaTeX 吗?
到目前为止我已经做到了:
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\node[state] (0) {0};
\node[state, right=of 0] (1) {1};
\node[state, right=of 1] (2) {2};
\node[state, right=of 2] (3) {3};
\node[draw=none, right=of 3] (4) {$\cdots$};
\draw[every loop]
(0) edge[bend right, auto=right] node {$\lambda$} (1)
(1) edge[bend right, auto=right] node {$\mu$} (0)
(1) edge[bend right, auto=right] node {$\lambda$} (2)
(2) edge[bend right, auto=right] node {$\mu$} (1)
(2) edge[bend right, auto=right] node {$\lambda$} (3)
(3) edge[bend right, auto=right] node {$\mu$} (2)
(3) edge[bend right, auto=right] node {$\lambda$} (4)
(4) edge[bend right, auto=right] node {$\mu$} (3);
\end{tikzpicture}
\caption{De M/M/1-wachtrij.}
\end{figure}
但那只是一行版本。我怎样才能向下添加多行?我对 LaTeX 还很陌生,所以非常欢迎任何帮助或提示!
答案1
编译需要一段时间。但在这里我将各种tikz
组件堆叠在一起。
\documentclass{article}
\usepackage{tikz,tabstackengine,amsmath}
\usetikzlibrary{automata,positioning}
\stackMath
\setstackgap{S}{-4pt}
\newcommand\rlarrow[2]{\smash{\vcenter{\hbox{\kern-5pt%
\begin{tikzpicture}
\node (0) {};
\node[right=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {$#1$} (1)
(1) edge[bend left, auto=left] node {$#2$} (0);
\end{tikzpicture}%
\kern-5pt}}}}
\newcommand\rarrow[1]{\kern-5pt\smash{%
\begin{tikzpicture}
\node (0) {};
\node[right=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {$#1$} (1);
\end{tikzpicture}%
}\kern-5pt}
\newcommand\uarrow[1]{\makebox[0pt]{%
\begin{tikzpicture}
\node (0) {};
\node[below=of 0] (1) {};
\draw[every loop]
(1) edge[bend left, auto=left] node {\makebox[0pt][r]{$#1$}} (0);
\end{tikzpicture}%
\kern5pt}}
\newcommand\darrow[1]{\makebox[0pt]{\kern5pt%
\begin{tikzpicture}
\node (0) {};
\node[below=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {\makebox[0pt][l]{$#1$}} (1);
\end{tikzpicture}%
}}
\begin{document}
\tabbedShortstack{
\fbox{0}&\rarrow{\lambda}&\fbox{1}&\rarrow{\lambda}&\fbox{2}&\rarrow{\lambda}&
\fbox{3}&\rarrow{\lambda}&\fbox{4}&\rarrow{\lambda}&\fbox{5}&\,\cdots\\
\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&\darrow{3\theta}&&\darrow{4\theta}&&
\darrow{5\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{\mu}&\fbox{2}&
\rlarrow{\lambda}{\mu}&\fbox{3}&\rlarrow{\lambda}{\mu}&\fbox{4}&
\rlarrow{\lambda}{\mu}&\fbox{5}&\,\cdots\\
\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&
\darrow{3\theta}&&\darrow{4\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{2\mu}&\fbox{3}&\rlarrow{\lambda}{2\mu}&\fbox{4}&
\rlarrow{\lambda}{2\mu}&\fbox{5}&\,\cdots\\
\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
\darrow{2\theta}&&\darrow{3\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{3\mu}&\fbox{4}&
\rlarrow{\lambda}{3\mu}&\fbox{5}&\,\cdots\\
\uarrow{4\sigma}&&\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
\darrow{2\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{4\mu}&\fbox{4}&
\rlarrow{\lambda}{4\mu}&\fbox{5}&\,\cdots\\
\vdots&&\vdots&&\vdots&&\vdots&&\vdots&&\vdots&
}
\end{document}
如果我将 TABstack 更改为非常定制的,我可以在大约一半的时间内完成编译array
。
\documentclass{article}
\usepackage{tikz,amsmath,array}
\usetikzlibrary{automata,positioning}
\newcommand\rlarrow[2]{\smash{\vcenter{\hbox{\kern-5pt%
\begin{tikzpicture}
\node (0) {};
\node[right=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {$#1$} (1)
(1) edge[bend left, auto=left] node {$#2$} (0);
\end{tikzpicture}%
\kern-5pt}}}}
\newcommand\rarrow[1]{\kern-5pt\smash{%
\begin{tikzpicture}
\node (0) {};
\node[right=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {$#1$} (1);
\end{tikzpicture}%
}\kern-5pt}
\newcommand\uarrow[1]{\makebox[0pt]{%
\begin{tikzpicture}
\node (0) {};
\node[below=of 0] (1) {};
\draw[every loop]
(1) edge[bend left, auto=left] node {\makebox[0pt][r]{$#1$}} (0);
\end{tikzpicture}%
\kern5pt}}
\newcommand\darrow[1]{\makebox[0pt]{\kern5pt%
\begin{tikzpicture}
\node (0) {};
\node[below=of 0] (1) {};
\draw[every loop]
(0) edge[bend left, auto=left] node {\makebox[0pt][l]{$#1$}} (1);
\end{tikzpicture}%
}}
\begin{document}
\arraycolsep=0pt
\renewcommand\arraystretch{0}
$\begin{array}{cccccccccccc}
\fbox{0}&\rarrow{\lambda}&\fbox{1}&\rarrow{\lambda}&\fbox{2}&\rarrow{\lambda}&
\fbox{3}&\rarrow{\lambda}&\fbox{4}&\rarrow{\lambda}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&\darrow{3\theta}&&\darrow{4\theta}&&
\darrow{5\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{\mu}&\fbox{2}&
\rlarrow{\lambda}{\mu}&\fbox{3}&\rlarrow{\lambda}{\mu}&\fbox{4}&
\rlarrow{\lambda}{\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&
\darrow{3\theta}&&\darrow{4\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{2\mu}&\fbox{3}&\rlarrow{\lambda}{2\mu}&\fbox{4}&
\rlarrow{\lambda}{2\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
\darrow{2\theta}&&\darrow{3\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{3\mu}&\fbox{4}&
\rlarrow{\lambda}{3\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{4\sigma}&&\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
\darrow{2\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
\rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{4\mu}&\fbox{4}&
\rlarrow{\lambda}{4\mu}&\fbox{5}&\,\cdots\\[-2pt]
\vdots&&\vdots&&\vdots&&\vdots&&\vdots&&\vdots&
\end{array}$
\end{document}
答案2
您可以从下面的代码开始,您只需要一个matrix
和一些foreach
循环来绘制这种方案。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}
\tikzset{
state/.style = {draw, minimum size=1cm, anchor=center},
array/.style = {matrix of nodes, nodes=state, row sep=1cm, column sep=1cm},
}
\matrix[array] (A) {
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
};
\foreach \j in {1,...,5}{
\node[right= 2mm of A-\j-6] {\dots};
\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
\draw (A-\j-\lasti) edge[bend left] node[above] {$\lambda$} (A-\j-\i);}
}
\foreach \i in {1,...,6}
\node[below= 2mm of A-5-\i] {\vdots};
\end{tikzpicture}
\end{document}
更新:
我设法找到了一种关系来定义foreach
lambda、sigma 和 theta 的循环边缘标签。但 mu 标签已应用于手动定义的循环。现在你的家庭作业就是理解它们。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}
\tikzset{
state/.style = {draw, minimum size=1cm, anchor=center},
array/.style = {matrix of nodes, nodes=state, row sep=1cm, column sep=1cm},
}
\matrix[array] (A) {
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
0 & 1 & 2 & 3 & 4 & 5 \\
};
% end row dots and \lambdas
\foreach \j in {1,...,5}{
\node[right= 2mm of A-\j-6] {\dots};
\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
\draw[->] (A-\j-\lasti) edge[bend left] node[above] {$\lambda$} (A-\j-\i);}
}
%Sigma
\foreach \i [count= \ni] in {2,...,5}{
\foreach \j [remember=\j as \lastj (initially \ni), count=\nj, evaluate=\nj as \text using {ifthenelse(\nj==1,,\nj)} ] in {\i,...,5}
\draw[<-] (A-\lastj-\ni) edge[bend right] node[left] {$\text\sigma$} (A-\j-\ni);
}
%Theta
\foreach \i [count= \ni, remember=\i as \lasti (initially 1)] in {2,...,5}{
\foreach \j [count=\nj, evaluate=\nj as \text using {ifthenelse(\nj==1,,\nj)} ] in {\i,...,6}
\draw[->] (A-\lasti-\j) edge[bend left] node[right] {$\text\theta$} (A-\i-\j);
}
%mus second row
\foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
\draw[<-] (A-2-\lasti) edge[bend right] node[below] {$\mu$} (A-2-\i);}
%third row mus
\foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$2\mu$,$2\mu$,$2\mu$}{
\draw[->] (A-3-\nexti) edge[bend left] node[below] {\i} (A-3-\ni);}
%fourth row mus
\foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$3\mu$,$3\mu$,$3\mu$}{
\draw[->] (A-4-\nexti) edge[bend left] node[below] {\i} (A-4-\ni);}
%fifth row mus
\foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$3\mu$,$4\mu$,$4\mu$}{
\draw[->] (A-5-\nexti) edge[bend left] node[below] {\i} (A-5-\ni);}
\foreach \i in {1,...,6}
\node[below= 2mm of A-5-\i] {\vdots};
\end{tikzpicture}
\end{document}
答案3
唯一\foreach
的解决方案。
我认为也为 s 找到了一条规则\mu
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{%
state/.style={draw, rectangle, minimum height=2em, minimum width=2em},
>=stealth
}
\begin{document}
\begin{figure}
\resizebox{\textwidth}{!}{%
\begin{tikzpicture}
% nodes, dots and lamba arrows
\foreach \linenum in {1,2,...,5}{%
\foreach \nodenum in {0,1,...,5}{%
\node[state] at (2.5*\nodenum,2-2.5*\linenum) (N-\linenum-\nodenum) {\nodenum};
}
\foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1)] in {0,1,...,4}{%
\draw[->] (N-\linenum-\nodenum) edge[bend left, auto=left] node {$\lambda$} (N-\linenum-\nextnode);
}
\node[right=1em of N-\linenum-5] {\Huge \dots};
}
\foreach \nodenum in {0,1,...,5}{%
\node[below=-1ex of N-5-\nodenum, scale=2] {$\vdots$};
}
% mu arrows
\foreach \linenum [evaluate=\linenum as \k using {ifthenelse(\linenum==2,,int(\linenum-1))},
evaluate=\linenum as \startnode using int(\linenum-2)] in {2,3,4,5} {%
\foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1)] in {\startnode,...,4} {%
\draw[<-] (N-\linenum-\nodenum) edge[bend right, auto=right] node {$\mathstrut\k\mu$} (N-\linenum-\nextnode);
}
}
\foreach \linenum [evaluate=\linenum as \endnode using int(\linenum-3)] in {4,5} {%
\foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1),
evaluate=\nodenum as \k using {ifthenelse(\nodenum==0,,int(\nodenum+1))}] in {0,...,\endnode} {%
\draw[<-] (N-\linenum-\nodenum) edge[bend right, auto=right] node {$\mathstrut\k\mu$} (N-\linenum-\nextnode);
}
}
% sigma arrows
\foreach \linenum [evaluate=\linenum as \i using int(\linenum+1),
evaluate=\linenum as \endnode using int(\linenum-1)] in {1,2,3,4} {%
\foreach \nodenum [evaluate=\nodenum as \k using {ifthenelse(int(\linenum-\nodenum)==1,,int(\linenum-\nodenum))}] in {0,...,\endnode}{%
\draw[->] (N-\i-\nodenum) edge[bend left, auto=left] node {$\mathstrut\k\sigma$} (N-\linenum-\nodenum);
}
}
% theta arrows
\foreach \linenum [evaluate=\linenum as \nextline using int(\linenum+1)] in {1,2,...,4} {%
\foreach \nodenum [evaluate=\nodenum as \k using {ifthenelse(int(\linenum)==int(\nodenum),,int(\nodenum-\linenum+1))}] in {\linenum,...,5} {%
\draw[->] (N-\linenum-\nodenum) edge[bend left, auto=left] node {$\mathstrut\k\theta$} (N-\nextline-\nodenum);
}
}
\end{tikzpicture}
}
\caption{De M/M/1-wachtrij.}
\end{figure}
\end{document}
答案4
和tikz-cd
\documentclass{article}
\usepackage{tikz-cd}
\newcommand{\rgarr}[2]{\arrow[#1,bend left,shift right=1ex,"#2"]}
\newcommand{\rgbox}[1]{\fbox{$#1$}}
\newcommand{\rgdot}[1]{\arrow[#1,dotted,no head]}
\begin{document}
\begin{tikzcd}[column sep=large,row sep=large]
\rgbox{0} \rgarr{r}{\lambda}
& \rgbox{1} \rgarr{r}{\lambda} \rgarr{d}{\theta}
& \rgbox{2} \rgarr{r}{\lambda} \rgarr{d}{2\theta}
& \rgbox{3} \rgarr{r}{\lambda} \rgarr{d}{3\theta}
& \rgbox{4} \rgarr{r}{\lambda} \rgarr{d}{4\theta}
& \rgbox{5} \rgarr{d}{5\theta} \rgdot{r} &[-3em] {}
\\
\rgbox{0} \rgarr{r}{\lambda} \rgarr{u}{\sigma}
& \rgbox{1} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{d}{\theta}
& \rgbox{2} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{d}{2\theta}
& \rgbox{3} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{d}{3\theta}
& \rgbox{4} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{d}{4\theta}
& \rgbox{5} \rgarr{l}{\mu} \rgarr{d}{5\theta} \rgdot{r} & {}
\\
\rgbox{0} \rgarr{r}{\lambda} \rgarr{u}{2\sigma}
& \rgbox{1} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{u}{\sigma}
& \rgbox{2} \rgarr{r}{\lambda} \rgarr{l}{2\mu} \rgarr{d}{\theta}
& \rgbox{3} \rgarr{r}{\lambda} \rgarr{l}{2\mu} \rgarr{d}{2\theta}
& \rgbox{4} \rgarr{r}{\lambda} \rgarr{l}{2\mu} \rgarr{d}{3\theta}
& \rgbox{5} \rgarr{l}{2\mu} \rgarr{d}{4\theta} \rgdot{r} & {}
\\
\rgbox{0} \rgarr{r}{\lambda} \rgarr{u}{3\sigma}
& \rgbox{1} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{u}{2\sigma}
& \rgbox{2} \rgarr{r}{\lambda} \rgarr{l}{2\mu} \rgarr{u}{\sigma}
& \rgbox{3} \rgarr{r}{\lambda} \rgarr{l}{3\mu} \rgarr{d}{\theta}
& \rgbox{4} \rgarr{r}{\lambda} \rgarr{l}{3\mu} \rgarr{d}{2\theta}
& \rgbox{5} \rgarr{l}{3\mu} \rgarr{d}{3\theta} \rgdot{r} & {}
\\
\rgbox{0} \rgarr{r}{\lambda} \rgarr{u}{4\sigma} \rgdot{d}
& \rgbox{1} \rgarr{r}{\lambda} \rgarr{l}{\mu} \rgarr{u}{3\sigma} \rgdot{d}
& \rgbox{2} \rgarr{r}{\lambda} \rgarr{l}{2\mu} \rgarr{u}{2\sigma} \rgdot{d}
& \rgbox{3} \rgarr{r}{\lambda} \rgarr{l}{3\mu} \rgarr{u}{\sigma} \rgdot{d}
& \rgbox{4} \rgarr{r}{\lambda} \rgarr{l}{4\mu} \rgdot{d}
& \rgbox{5} \rgarr{l}{4\mu} \rgdot{r} \rgdot{d} & {}
\\[-2em]
{} & {} & {} & {} & {} & {}
\end{tikzcd}
\end{document}