如何绘制这个 TikZ 图形?

如何绘制这个 TikZ 图形?

考虑:

\documentclass[12pt, a4paper]{report}
    \begin{document}
    \usepackage{tikz}
    \usetikzlibrary{arrows.meta, positioning, quotes}
\begin{figure} \centering
  \begin{tikzpicture}[
node distance = 33mm and 33mm,
     N/.style = {draw, minimum size=12mm, inner sep=0pt},
            > = {Stealth[scale=0.6]},
 every edge/.append style = {draw, ->},
every edge quotes/.append style = {font=\small\linespread{0.75}\selectfont,
                                   inner sep=5pt, sloped}
                        ]
\node [N] (A) {$S$};
\node [N, right=of A] (B) {$A$};
\node [N,below=of B] (C) {$I$};
\node [N,left=of C] (D) {$R$};
%
\draw [<-] (A) -- ++(-1,0) node[left] {$b\left(1-\nu\right)$};
\draw [<-] (D) -- ++(-1,0) node[left] {$b\nu$};
\draw [->] (A) -- ++(0,1) node[above] {$\mu S$};
\draw [->] (B) -- ++(0,1) node[above] {$\mu A$};
\draw [->] ([xshift=-3.5mm] C.south) -- ++(0,-0.4) node[below] {$\alpha I$};
\draw [->] ([xshift=+3.5mm] C.south) -- ++(0,-0.4) node[below] {$\mu I$};
\draw [->] (D) -- ++(0,-1) node[below] {$\mu R$};
\draw[->] (B)--(D) node[midway,sloped,above]{$\gamma_A$};
%
\end{tikzpicture}
\setlength{\textfloatsep}{10pt plus 1.0pt minus 2.0pt}
\caption{Flow diagram for the SAIRS model.}
\end{figure}
  \end{document}

我想要的是:

在此处输入图片描述

我希望它类似于这种格式。/使用 tikzpicture 布局:

\documentclass[12pt, a4paper]{report}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, quotes}    

\begin{document}
\begin{figure} \centering
\begin{tikzpicture}[
node distance = 11mm and 33mm,
     N/.style = {draw, minimum size=12mm, inner sep=0pt},
            > = {Stealth[scale=0.6]},
every edge/.append style = {draw, ->},
every edge quotes/.append style = {font=\small\linespread{0.75}\selectfont,
                                       inner sep=5pt, sloped}
                            ]
\node [N] (A) {$S$};
\node [N,above right=of A] (B) {$I_1$};
\node [N,below right=of A] (C) {$I_2$};
\node [N,above right=of C] (D) {$J$};
\node [N,right=of D] (E)  {$A$};
%
\draw [<-] (A) -- ++(-1,0) node[left] {$\mu$};
\draw [->] (A) -- ++(0,-1) node[below] {$\nu S$};
\draw [->] ([xshift=3.75mm] B.south) -- ++(0,-0.4) node[below] {$\nu I_1$};
\draw [->] (C) -- ++(0,-1) node[below] {$\nu I_2$};
\draw [->] (D) -- ++(0,-1) node[below] {$\nu J$};
\draw [->] ([xshift=-3.5mm] E.south) -- ++(0,-0.4) node[below] {$\alpha A$};
    \draw [->] ([xshift=+3.5mm] E.south) -- ++(0,-0.4) node[below] {$\nu A$};
    %
\draw   ([yshift=+1mm] A.east)
                edge [ultra thick, draw=gray,"\slash\slash\slash" anchor=center,
                      "$\begin{gathered}
                            p\beta_1 S I_2\\
                            q\beta_2 S J\\
                            r\beta_3 S A
                        \end{gathered}$" ] (B.west)
            ([yshift=-1mm] A.east)
                edge [ultra thick, draw=gray,"\slash\slash\slash" anchor=center,
                      "$\begin{gathered}
                            (1-p)\beta_2 S I_2\\
                            (1-q)\beta_2 S J\\
                            (1-r)\beta_3 S A
                        \end{gathered}$" '] (C.west)
           (B) edge [rotate=90,"$\epsilon I_1$"] (C)
            (D.west) edge ["$\xi_1 J$"  ] ( B.east)
            ( C.east) edge ["$p_1 I_2$"  ] (D.west)
            ([yshift=-2mm] D.west) edge ["$\xi_2 J$" '] ([yshift=-2mm] C.east)
        (D) edge ["$p_2 J$"] (E);
\end{tikzpicture}
\end{figure}
\end{document}

编辑:

为什么这个代码不起作用:

\documentclass[12pt, a4paper]{report}
        \begin{document}
        \usepackage{tikz}
        \usetikzlibrary{arrows.meta, positioning, quotes}
    \begin{figure} \centering
      \begin{tikzpicture}[
    node distance = 33mm and 33mm,
         N/.style = {draw, minimum size=12mm, inner sep=0pt},
                > = {Stealth[scale=0.6]},
     every edge/.append style = {draw, ->},
    every edge quotes/.append style = {font=\small\linespread{0.75}\selectfont,
                                       inner sep=5pt, sloped}
                            ]
    \node [N] (A) {$S$};
    \node [N, right=of A] (B) {$A$};
    \node [N,below=of B] (C) {$I$};
    \node [N,left=of C] (D) {$R$};
    %
    \draw [<-] (A) -- ++(-1,0) node[left] {$b\left(1-\nu\right)$};
    \draw [<-] (D) -- ++(-1,0) node[left] {$b\nu$};
    \draw [->] (A) -- ++(0,1) node[above] {$\mu S$};
    \draw [->] (B) -- ++(0,1) node[above] {$\mu A$};
    \draw [->] ([xshift=-3.5mm] C.south) -- ++(0,-0.4) node[below] {$\alpha I$};
    \draw [->] ([xshift=+3.5mm] C.south) -- ++(0,-0.4) node[below] {$\mu I$};
    \draw [->] (D) -- ++(0,-1) node[below] {$\mu R$};
    %
    \draw
     (A) edge [rotate=90,"$\rho S$"] (D)
     (A) edge ["$(\beta_A A+\beta_I I)S$"]
     (B) edge["$\sigma A$"](C)
     ([yshift=-2mm] B.west) edge ["$\gamma_A A$" '] ([yshift=-2mm] D.east)
     (C) edge["$\gamma_I$"](D)
     (C) edge[bend left,"$\eta$"](D)
     (D) edge ["$\xi R$"] (A)
    \end{tikzpicture}
    \setlength{\textfloatsep}{10pt plus 1.0pt minus 2.0pt}
    \caption{Flow diagram for the SAIRS model.}
    \end{figure}
\end{document}

答案1

稍微修改了一下@Luis Turcio 的回答(+1):

  • 由于使用了稍微不同(更短)的语法,代码更短一些
  • 主节点为方形(正如 OP 所希望的那样)
  • 节点间距离更大
\documentclass[margin=3mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz-cd}
\tikzcdset{arrow style=tikz,            % <--- new
           diagrams={>=Straight Barb}
           }

\begin{document}
    \begin{tikzcd}[%nodes in empty cells,
                   cells={nodes={draw,anchor=center}},
                   sep=huge]
|[draw=none]|
   & |[draw=none]|\mu 
        & |[draw=none]|\mu       \\
|[draw=none]|\rar["b(1-\nu)"] 
    & S \uar
        \rar["(B_A H B_I I)"]
        \dar[shift right,"\rho"'] 
        & A \uar
            \dar["\delta"]
            \dlar["\delta_a" ']  \\
|[draw=none]|\rar["b\nu"] 
    & R \dar
        \uar[shift right,"\varepsilon" '] 
        & I \dar
            \lar[shift right,"\delta_I" ']
            \lar[shift left,bend left,"\tau"] \\
|[draw=none]|
    & |[draw=none]|\mu 
        & |[draw=none]|\mu+\alpha
    \end{tikzcd}
\end{document}

在此处输入图片描述

关于您在问题编辑部分中的 MWE:

  • 必须先加载包\begin{document}
  • 绘制箭头的第二个命令没有被终止\\
  • 绘制箭头的第二行没有定义目标坐标(因此边缘位置错误)

更正此问题后,您的代码可以顺利编译。但结果可能不是您想要的:

在此处输入图片描述

看来您在选择箭头坐标时犯了错误。

附录: 我认为你的代码应该是这样的:

\documentclass[12pt, a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, quotes}

\begin{document}
    \begin{figure} \centering
    \begin{tikzpicture}[
node distance = 33mm and 33mm,
     N/.style = {draw, minimum size=12mm, inner sep=0pt},
            > = Stealth,
       every edge/.append style = {draw, ->},
every edge quotes/.append style = {font=\small\linespread{0.75}\selectfont,
                                   inner sep=5pt}
                            ]
\node [N]               (A) {$S$};
\node [N,right=of A]    (B) {$A$};
\node [N,below=of B]    (C) {$I$};
\node [N, left=of C]    (D) {$R$};
    %
\draw [<-] (A) -- ++(-1,0) node[left] {$b\left(1-\nu\right)$};
\draw [<-] (D) -- ++(-1,0) node[left] {$b\nu$};
\draw [->] (A) -- ++( 0,1) node[above] {$\mu S$};
\draw [->] (B) -- ++( 0,1) node[above] {$\mu A$};
\draw [->] ([xshift=-3.5mm] C.south) -- ++(0,-0.4) node[below] {$\alpha I$};
\draw [->] ([xshift=+3.5mm] C.south) -- ++(0,-0.4) node[below] {$\mu I$};
\draw [->] (D) -- ++(0,-1) node[below] {$\mu R$};
    %
\draw
     (A.260) edge ["$\rho S$" '] (D.100)
     (A) edge ["$(\beta_A A+\beta_I I)S$"]  (B)
     (B) edge ["$\delta$" '] (C)
     (B) edge["$\sigma A$"] (D)
     (C) edge["$\gamma_I$"](D)
     (C) edge[bend left,"$\eta$"](D)
     (D.80) edge ["$\xi R$" '] (A.280)
     ;
    \end{tikzpicture}
%    \setlength{\textfloatsep}{10pt plus 1.0pt minus 2.0pt} % is really needed?
    \caption{Flow diagram for the SAIRS model.}
    \end{figure}
\end{document}

这使:

在此处输入图片描述

答案2

此类图表很容易tikz-cd

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[cells={nodes={draw=black,circle,anchor=center}},column sep=large]
& |[draw=none]|\mu & |[draw=none]|\mu\\
|[draw=none]|{}\ar{r}{b(1-\nu)} & S\ar{u}\ar{r}{(B_A H B_I I)}\ar[shift right]{d}[swap]{\rho} & A\ar{u}\ar{d}{\delta}\ar{dl}[swap]{\delta_a}\\
|[draw=none]|{}\ar{r}{b\nu} & R\ar{d}\ar[shift right]{u}[swap]{\varepsilon} & I\ar[shorten > = -1.9ex]{d}\ar[shift right]{l}[swap]{\delta_I}\ar[shift left,bend left=20]{l}{\tau}\\
& |[draw=none]|\mu & |[draw=none]|\mu+\alpha
\end{tikzcd}
\end{document}

在此处输入图片描述

相关内容