tikZ 中的演算法流程圖

tikZ 中的演算法流程圖

下面的流程圖由代表資料和函數的區塊組成,這些區塊以順時針方向運行,而在這一系列區塊之外是解釋性步驟。您將如何在 LaTeX 中進行編碼,同時使區塊中的文字和周圍的步驟都可編輯? Tikz 是要走的路嗎?用作研究文章中的圖形。

在此輸入影像描述

答案1

排版很簡單,輸入文字很麻煩。所以我加了很多bla bla。您只需要矩陣和一些用於陰影線等的樣式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}

相關內容