在 Latex 中建立流程圖

在 Latex 中建立流程圖

在此輸入影像描述

如何在 Latex 中建立這樣的流程圖?到目前為止,我還沒有能夠實現任何接近此的目標。我的程式碼如下。

    \documentclass{article}
    \usepackage{tikz}
    
    \begin{document}
    
    \begin{tikzpicture}[node distance=2cm, auto,
      level1/.style={rectangle, draw, fill=red!20, rounded corners, text width=4em, text centered, minimum height=2em},
      level2/.style={rectangle, draw, fill=blue!20, rounded corners, text width=4em, text centered, minimum height=2em},
      level3/.style={rectangle, draw, fill=green!20, rounded corners, text width=4em, text centered, minimum height=2em}]
    
      % Root node
      \node [level1] (root) {Root};
    
      % Level 2 nodes
      \node [level2, right of=root] (node1) {Node 1};
      \node [level2, right of=node1] (node2) {Node 2};
      \node [level2, right of=node2] (node3) {Node 3};
      \node [level2, right of=node3] (node4) {Node 4};
      \node [level2, right of=node4] (node5) {Node 5};
    
      % Level 3 nodes
      \node [level3, below of=node1, yshift=-1cm] (child1) {Child 1};
      \node [level3, below of=node2, yshift=-1cm] (child2) {Child 2};
      \node [level3, below of=node3, yshift=-1cm] (child3) {Child 3};
      \node [level3, below of=node4, yshift=-1cm] (child4) {Child 4};
      \node [level3, below of=node5, yshift=-1cm] (child5) {Child 5};
    
      % Arrows
      \foreach \from in {root}
        \foreach \to in {node1, node2, node3, node4, node5}
          \draw [->] (\from) -- (\to);
      \foreach \from in {node1, node2, node3, node4, node5}
        \foreach \to in {child1, child2, child3}
          \draw [->] (\from) -- (\to);
    
    \end{tikzpicture}
    
    \end{document}

結果

答案1

這是一個forest解決方案。

在此輸入影像描述

\documentclass{article}

\usepackage{forest}
\useforestlibrary{edges}

\begin{document}

\begin{forest}
forked edges,
for tree={
    grow'=0,
    align=center,
    minimum width=3cm,
    anchor=center,
    inner xsep=3mm,
    rounded corners,
    font=\small,
    if level=1{fill=orange, text=white}
        {if level=2{fill=brown, text=white}{draw=red}}
}
[Root node, rotate=90, fill=red, text=white
    [level 1 node
        [level 2 node
            [Some very long reference]
        ]
        [level 2 node
            [Some very long reference\\with two lines]
        ]
    ]
    [level 1 node
        [level 2 node
            [Another even longer reference\\with two lines]
        ]
        [level 2 node
            [Some very long reference\\with two lines]
        ]
        [level 2 node\\has two lines
            [Some reference[Another reference]]
            [Some reference\\with two lines[Another reference\\with two lines]]
        ]
    ]
    [level 1 node
        [level 2 node
            [A very very very long long long reference\\with two lines]
        ]
        [level 2 node
            [A very very very long long long reference]
        ]
    ]
    [level 1 node
        [level 2 node
            [A very very very long long long reference]
        ]
        [level 2 node
            [A long reference]
        ]
    ]
    [level 1 node
        [level 2 node
            [A very very very long long long reference]
        ]
        [level 2 node
            [A very very very long long long reference]
        ]
    ]
]
\end{forest}

\end{document}

答案2

有很多方法可以做到這一點,例如使用childpgfmanual 中的 -approach、使用 package forest、使用 amatrix放置節點、使用 tiklzlibrarypositioning等。

當你是初學者時,我建議你更多​​地逐個物件地進行,並且具有高度的控制力。所以基本上如下:

  • 合理放置節點
  • 繪製所需的連接

我在評論中記錄了相關步驟,並專注於中間有趣的部分。我建議出於訓練原因嘗試從頭開始重複這些,並看看程式碼如何從簡單演變為精緻。我建議在 pgfmanual 中並行尋找這些命令。

完成放置和連接後,第一個代碼將顯示結果。第二個是美化,即引進顏色等。

有幾個參數可供調整。除了分支之外,它們都在地化在樣式部分。所以那裡的改變幾乎總是正確的。分支的可以而且應該移至\newcommand以便進一步重構。

完成節點和連接

向後開始,從最右邊的子項開始直到根。

旋轉根節點很棘手,所以我暫時保留它。

對於連接來說,有兩件事:

  • 註解掉的試驗,檢查良好的程式碼,並使用節點對(好吧,它們的名稱)重構為循環
  • 分支同樣,引入一個中間點作為相對座標

資源1

% ~~~ makes development much easier ~~~~~~~~~~~~~~~~~~
\documentclass[10pt,border=3mm,tikz]{standalone}

% ~~~ PROCESS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%       care for positioning text, first
% 1)    start with node at (0,0), ignoring all colors etc.
% 2)    put next node above it, introduce style up
% 3)    continue, introduce style and nodes for dn
% 4)    introduce brown child level as nodes + style lft
% 4b)   making nodes width the same, i.e. adjust style lft
% 5)    introducing orange children, style lftb reuses lft
% 6)    introducing ROOT and extra
% 7)    adding simple and branched connections


\begin{document}
 \begin{tikzpicture}[% ~~~ introducing styles as suitable
        up/.style={draw,anchor=south west,yshift=2mm},
        dn/.style={draw,anchor=north west,yshift=-2mm},
        lft/.style={draw,anchor=east,xshift=-4mm,minimum width=2cm},
        lftb/.style={lft,minimum width=32mm,yshift=-4mm},
        rt/.style={draw,anchor=west,xshift=4mm},
        root/.style={draw,xshift=-10mm,yshift=-9mm,
                     rotate=0,anchor=east},
    ]
 
    % ~~~ starting backwards with "rightmost" child level ~~~
    % ~~~ upwards
    \node[draw] (D0) at (0,0)           {Denoisung};
    \node[up]   (D1) at (D0.north west) {HDR};
    \node[up]   (D2) at (D1.north west) {Semantic NeRF};

    % ~~~ downwards
    \node[dn]   (D-1) at (D0.south west) {GIRAFFE};
    \node[dn]   (D-2) at (D-1.south west){Dream Fusion};
    
    % ~~~ brown children ~~~~~
    \node[lft]  (C-1) at (D-2.west)      {Diffusion};
    \node[lft]  (C1)  at (D1.west)       {Semantics};
    \node[lft]  (C2)  at (D2.west)       {Editing};
    % ~~~ this one is special ~~~~~~~~~
    \node[lft,yshift=-4mm] (C0)  at (D0.west) {Functional};
    
    % ~~~ orange children ~~~~~~~~~~    
    \node[lftb] (B0) at (C0.west)        {Generative Models};
    \node[lftb] (B1) at (C2.west)        {Image Processing};
    
    % ~~~ ROOT ~~~~~~~~~~
    \node[root] (RT) at (B1.west) {Applications};
    
    % ~~~ extra ~~~~~~~~~
    \node[rt] (X0) at (D0.east) {RawNeRF};
    
    % ~~~ SIMPLE connections ~~~~~~~~~
%   \draw (C2) -- (D2); 
%   \draw (C1) -- (D1); 
%   now refactored, i.e. as loop
    \foreach \a/\b in {C2/D2, C1/D1, D0/X0, C-1/D-2}
        \draw (\a) -- (\b);
    
    % ~~~ branched connections ~~~~~~~~
%   \draw (C0.east) -- +(.2,0) |-  (D0.west);
%   \draw (C0.east) -- +(.2,0) |-  (D-1.west);
%   now refactored, i.e. as loop
    \foreach \a/\b in  {C0/D0, C0/D-1,
                        B1/C2, B1/C1, B0/C0, B0/C-1,
                        RT/B1, RT/B0}
        \draw (\a.east) -- +(.2,0) |- (\b.west);    
    
 \end{tikzpicture}
\end{document}

美容

現在這幾乎很無聊:

  • 定義各個層級的樣式
  • 將所述樣式新增至相關節點

當您在圖中新增更多試驗時,實際刪除試驗可能是個好主意。為此,只需遵循以下模式:

  • 新節點,現在具有正確的樣式組合和新節點名稱
  • \draw在兩個循環中添加所需的連接
  • 採取小步驟,及早發現並解決問題

建議重新思考一下,如果引入多行文字是個好主意。儘管這是可能的,但您很可能必須重新調整幾種風格,甚至可能改變一些方法。通常:少即是多 - 礦石多,少…

資源2

% ~~~ makes development much easier ~~~~~~~~~~~~~~~~~~
\documentclass[10pt,border=3mm,tikz]{standalone}

% ~~~ PROCESS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%       now let's take care of colors and shapes
% 8)    define styles nd etc. AND add to the relevant nodes


\begin{document}
 \begin{tikzpicture}[% ~~~ introducing styles as suitable
        up/.style={draw,anchor=south west,yshift=2mm},
        dn/.style={draw,anchor=north west,yshift=-2mm},
        lft/.style={draw,anchor=east,xshift=-4mm,
                    minimum width=2cm},
        lftb/.style={lft,minimum width=32mm,yshift=-4mm},
        rt/.style={draw,anchor=west,xshift=4mm},
        root/.style={draw,xshift=-10mm,yshift=-9mm,
                     rotate=0,anchor=east},
        % ~~~ now the colors etc. ~~~~~~~~~~
        nd/.style={draw=red,rounded corners},
        nc/.style={draw=none,rounded corners,fill=brown!30},
        nb/.style={draw=none,rounded corners,fill=orange!30},
        nr/.style={draw=none,rounded corners,fill=red,
                   text=white,},% not recommended !
    ]
 
    % ~~~ starting backwards with "rightmost" child level ~~~
    % ~~~ upwards
    \node[draw,nd] (D0) at (0,0)           {Denoisung};
    \node[up,nd]   (D1) at (D0.north west) {HDR};
    \node[up,nd]   (D2) at (D1.north west) {Semantic NeRF};

    % ~~~ downwards
    \node[dn,nd]   (D-1) at (D0.south west) {GIRAFFE};
    \node[dn,nd]   (D-2) at (D-1.south west){Dream Fusion};
    
    % ~~~ brown children ~~~~~
    \node[lft,nc]  (C-1) at (D-2.west)      {Diffusion};
    \node[lft,nc]  (C1)  at (D1.west)       {Semantics};
    \node[lft,nc]  (C2)  at (D2.west)       {Editing};
    % ~~~ this one is special ~~~~~~~~~
    \node[lft,yshift=-4mm,nc] (C0)  at (D0.west) {Functional};
    
    % ~~~ orange children ~~~~~~~~~~    
    \node[lftb,nb] (B0) at (C0.west)         {Generative Models};
    \node[lftb,nb] (B1) at (C2.west)         {Image Processing};
    
    % ~~~ ROOT ~~~~~~~~~~
    \node[root,nr] (RT) at (B1.west) {Applications};
    
    % ~~~ extra ~~~~~~~~~
    \node[rt,nd] (X0) at (D0.east) {RawNeRF};
    
    % ~~~ SIMPLE connections ~~~~~~~~~
%   \draw (C2) -- (D2); 
%   \draw (C1) -- (D1); 
%   now refactored, i.e. as loop
    \foreach \a/\b in {C2/D2, C1/D1, D0/X0, C-1/D-2}
        \draw (\a) -- (\b);
    
    % ~~~ branched connections ~~~~~~~~
%   \draw (C0.east) -- +(.2,0) |-  (D0.west);
%   \draw (C0.east) -- +(.2,0) |-  (D-1.west);
%   now refactored, i.e. as loop
    \foreach \a/\b in  {C0/D0, C0/D-1,
                        B1/C2, B1/C1, B0/C0, B0/C-1,
                        RT/B1, RT/B0}
        \draw (\a.east) -- +(.2,0) |- (\b.west);    
    
 \end{tikzpicture}
\end{document}

相關內容