tikZ中的算法流程图

tikZ中的算法流程图

下面的流程图由代表数据和功能的块组成,这些块按顺时针方向运行,而这一系列块的外面是解释性步骤。如何在 LaTeX 中编写此代码,同时使块中的文本和周围的步骤都可编辑?Tikz 是可行的方法吗?用作研究文章中的图表。

在此处输入图片描述

答案1

布局很简单,插入文本很麻烦。所以我添加了很多 bla bla。您只需要矩阵和一些阴影样式等。要添加周围的文本,请将图表放在 a 中local bounding box,并使用节点的垂直或水平位置来放置文本。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,matrix,patterns.meta,positioning}
\begin{document}
\begin{tikzpicture}[node font=\sffamily,>=Triangle,
    Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
    line width=1.2pt,
    dash pattern=on 0.01\pgflinewidth off #1\pgflinewidth,line cap=round,
    shorten >=0.5em,shorten <=0.5em},
    Dotted/.default=6,
    mmat/.style={matrix of nodes,nodes in empty cells,
        row sep=-\pgflinewidth,nodes={minimum width=3em,
            draw,anchor=center,
            text depth=0.25ex,text height=0.8em}},
    dotcell/.style={minimum height=4em,path picture={
        \draw[Dotted] (path picture bounding box.north)--
        (path picture bounding box.south);}},
    hatchcell/.style={pattern={Lines[angle=45,distance={3pt}]}},
    vcell/.style={xshift=-1ex,hatchcell},
    block/.style={draw,align=center}    
    ]
 \begin{scope}[local bounding box=diagram]
  \node[mmat] (vmat1) {|[vcell]|1 \\
     2 \\ |[dotcell]|\\  10\\ }; 
  %
  \node[mmat,below=2em of vmat1] (vmat2) {1 \\
     |[vcell]| 2 \\ |[dotcell]|\\  10\\ };   
  %
  \node[mmat,below=3em of vmat2] (vmat3) {1 \\
     2 \\ |[dotcell]|\\  |[vcell]|  10\\ };  
  %  
  \foreach \X in {1,2,3}
  {\node[right=3em of vmat\X,block](b\X){regression\textsubscript{a}\\
     regression\textsubscript{b}\\ regression\textsubscript{c}};
  \draw[->] (vmat\X-3-1.east|-b\X) -- (b\X);}
  %
  \node[right=3em of b1.south east,anchor=south west,mmat,
     column 1/.style={nodes={hatchcell}}] (hmat1) {
   1 & $Z_{1\mathsf{a}}$ & $Z_{1\mathsf{b}}$ & $Z_{1\mathsf{c}}$ \\
   2 & $Z_{2\mathsf{a}}$ & $Z_{2\mathsf{b}}$ & $Z_{2\mathsf{c}}$ \\
  }; 
  \draw[->] (b1.east|-hmat1-1-1.west) -- (hmat1-1-1.west);
  %
  \node[right=3em of b2.north east,anchor=north west,mmat,
     column 1/.style={nodes={hatchcell}}] (hmat2) {
   10 & $Z_{10\mathsf{a}}$ & $Z_{10\mathsf{b}}$ & $Z_{10\mathsf{c}}$ \\
  }; 
  \foreach \X [count=\Y] in {a,b,c}
  {\node[below=\Y*2em of hmat2-1-\the\numexpr\Y+1\relax,block] 
     (CV\Y) {CV Risk\textsubscript{\X}};
  \draw[->] (hmat2-1-\the\numexpr\Y+1\relax) -- (CV\Y); }
  \draw (CV1.west) -- ++ (-1ex,0) |- (CV3);
  \draw[->] (b2.east) -- ++(1em,0) |- (hmat1-2-1.west);
  \draw[->] (b3.east) -- ++(2em,0) |- (hmat2-1-1.west);
  %
  \node[block,below=12em of hmat2](F){Family of weighted\\
     combinations}; 
  \draw[->] (CV2.south-|F) -- (F);
  %
  \node[block,below=4em of F.south west,xshift=-3em] (P)
  {Some long formula with P}; 
  \draw[<-] (P.east) -- ++ (1em,0) coordinate (aux) -- (F.south-|aux);
  \node[block,left=3em of P,text width=8em] (PP)  {\vphantom{P}}; 
  \draw[->] (P) -- (PP);
  %
  \node[left=3em of vmat2.north west,block] (A){Collection of\\ algorithms}; 
  \node[left=3em of vmat2.south west,block] (MD){Mortality\\ data};  
  \draw[->] (A.east) -- ++ (1.2em,0) |- (vmat1-3-1);
  \draw[->] (A.east) -- ++ (1.2em,0) |- (vmat3-3-1);
  \draw[->] (MD.east) -- ++ (1.2em,0) |- (vmat2-3-1);
  \path (current bounding box.south west) + (-1ex,-1ex)
  (current bounding box.north east) + (1ex,1ex);
 \end{scope} 
 \node[text width=7em,above] at (MD|-diagram.north){1.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=7em,above] at (vmat1|-diagram.north){2.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=7em,above] at (b1|-diagram.north){3.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=12em,above] at (hmat1|-diagram.north){4.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=8em,right] at (hmat2-|diagram.east){5.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=8em,right] at (F-|diagram.east){6.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=9em,below] at (P|-diagram.south){7.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=9em,below] at (PP|-diagram.south){8.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
\end{tikzpicture}
\end{document}

在此处输入图片描述

此版本适用于(一些)过时的 TeX 安装。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,matrix,patterns,positioning}
\begin{document}
\begin{tikzpicture}[node font=\sffamily,>=Triangle,
    Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
    line width=1.2pt,
    dash pattern=on 0.01\pgflinewidth off #1\pgflinewidth,line cap=round,
    shorten >=0.5em,shorten <=0.5em},
    Dotted/.default=6,
    mmat/.style={matrix of nodes,nodes in empty cells,
        row sep=-\pgflinewidth,nodes={minimum width=3em,
            draw,anchor=center,
            text depth=0.25ex,text height=0.8em}},
    dotcell/.style={minimum height=4em,path picture={
        \draw[Dotted] (path picture bounding box.north)--
        (path picture bounding box.south);}},
    hatchcell/.style={pattern=north east lines},
    vcell/.style={xshift=-1ex,hatchcell},
    block/.style={draw,align=center}    
    ]
 \begin{scope}[local bounding box=diagram]
  \node[mmat] (vmat1) {|[vcell]|1 \\
     2 \\ |[dotcell]|\\  10\\ }; 
  %
  \node[mmat,below=2em of vmat1] (vmat2) {1 \\
     |[vcell]| 2 \\ |[dotcell]|\\  10\\ };   
  %
  \node[mmat,below=3em of vmat2] (vmat3) {1 \\
     2 \\ |[dotcell]|\\  |[vcell]|  10\\ };  
  %  
  \foreach \X in {1,2,3}
  {\node[right=3em of vmat\X,block](b\X){regression\textsubscript{a}\\
     regression\textsubscript{b}\\ regression\textsubscript{c}};
  \draw[->] (vmat\X-3-1.east|-b\X) -- (b\X);}
  %
  \node[right=3em of b1.south east,anchor=south west,mmat,
     column 1/.style={nodes={hatchcell}}] (hmat1) {
   1 & $Z_{1\mathsf{a}}$ & $Z_{1\mathsf{b}}$ & $Z_{1\mathsf{c}}$ \\
   2 & $Z_{2\mathsf{a}}$ & $Z_{2\mathsf{b}}$ & $Z_{2\mathsf{c}}$ \\
  }; 
  \draw[->] (b1.east|-hmat1-1-1.west) -- (hmat1-1-1.west);
  %
  \node[right=3em of b2.north east,anchor=north west,mmat,
     column 1/.style={nodes={hatchcell}}] (hmat2) {
   10 & $Z_{10\mathsf{a}}$ & $Z_{10\mathsf{b}}$ & $Z_{10\mathsf{c}}$ \\
  }; 
  \foreach \X [count=\Y] in {a,b,c}
  {\node[below=\Y*2em of hmat2-1-\the\numexpr\Y+1\relax,block] 
     (CV\Y) {CV Risk\textsubscript{\X}};
  \draw[->] (hmat2-1-\the\numexpr\Y+1\relax) -- (CV\Y); }
  \draw (CV1.west) -- ++ (-1ex,0) |- (CV3);
  \draw[->] (b2.east) -- ++(1em,0) |- (hmat1-2-1.west);
  \draw[->] (b3.east) -- ++(2em,0) |- (hmat2-1-1.west);
  %
  \node[block,below=12em of hmat2](F){Family of weighted\\
     combinations}; 
  \draw[->] (CV2.south-|F) -- (F);
  %
  \node[block,below=4em of F.south west,xshift=-3em] (P)
  {Some long formula with P}; 
  \draw[<-] (P.east) -- ++ (1em,0) coordinate (aux) -- (F.south-|aux);
  \node[block,left=3em of P,text width=8em] (PP)  {\vphantom{P}}; 
  \draw[->] (P) -- (PP);
  %
  \node[left=3em of vmat2.north west,block] (A){Collection of\\ algorithms}; 
  \node[left=3em of vmat2.south west,block] (MD){Mortality\\ data};  
  \draw[->] (A.east) -- ++ (1.2em,0) |- (vmat1-3-1);
  \draw[->] (A.east) -- ++ (1.2em,0) |- (vmat3-3-1);
  \draw[->] (MD.east) -- ++ (1.2em,0) |- (vmat2-3-1);
  \path (current bounding box.south west) + (-1ex,-1ex)
  (current bounding box.north east) + (1ex,1ex);
 \end{scope} 
 \node[text width=7em,above] at (MD|-diagram.north){1.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=7em,above] at (vmat1|-diagram.north){2.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=7em,above] at (b1|-diagram.north){3.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=12em,above] at (hmat1|-diagram.north){4.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=8em,right] at (hmat2-|diagram.east){5.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=8em,right] at (F-|diagram.east){6.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=9em,below] at (P|-diagram.south){7.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
 \node[text width=9em,below] at (PP|-diagram.south){8.\ bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
 bla bla};
\end{tikzpicture}
\end{document}

相关内容