在 Tikz 流程图中添加带文本的括号

在 Tikz 流程图中添加带文本的括号

我从另一个问题,我想知道是否可以像这样添加括号来描述流程图中的步骤?:

在此处输入图片描述

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt,on grid]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em,on grid]
\tikzstyle{line} = [draw, -latex]

\begin{document}

  \begin{center}
    \resizebox{0.4 \linewidth}{!}{%
      \begin{tikzpicture}[node distance = 2cm, auto]
        \node[block]                                (init) {Init $n=0$, $T_0$, and $S_0$};
        \node[block, below= of init]                (nbrh) {$S_{n+1}=N(S_n)$};
        \node[decision, below= of nbrh]             (ovgt) {$f(S_{n+1}) \le f(S_n)$};
        \node[block, below=2.5cm of ovgt]           (accp) {Accept $S_{n+1}$};
        \node[decision, right= 3.5cm of ovgt]       (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
        \node[block, above=3cm of rand]             (rejj) {Reject $S_{n+1}$};
        \node[block, below= of accp]                (incr) {$T_{n+1} = K(T_n)$ and $n=n+1$};
        \node[decision, below=2.5cm of incr]              (stcd) {Stop?};  
        \node[block, right=3cm of stcd]                (stop) {Stop};


        \path[line] (init) --          (nbrh);
        \path[line] (nbrh) --          (ovgt);
        \path[line] (ovgt) -- node{yes}(accp);
        \path[line] (ovgt) -- node{no} (rand);
        \path[line] (rand) -- node{no} (rejj);
        \path[line] (rejj) --          (nbrh);
        \path[line] (rand) |- node{yes}(accp);
        \path[line] (accp) --          (incr);
        \path[line] (incr) --          (stcd);
        \path[line] (stcd) -- node{yes}(stop);
        \path[line] (stcd) -- ++(-2,0) |- node[pos=.25]{no}  (nbrh);
      \end{tikzpicture}%
    }%
  \end{center}


\end{document}

答案1

根据@js bibra 命题(+1),建议使用chainsquotes库,删除图像缩放resizebox和未使用的on grid绘图:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                decorations.pathreplacing,calligraphy,
                positioning,
                quotes,
                shapes.geometric
                }
\begin{document}
\begin{center}
    \begin{tikzpicture}[auto,
    node distance = 6mm and 15mm,
      start chain = A going below,
       arr/.style = {-Stealth},
every edge/.style = {draw, arr},
        BC/.style = {decorate,  % Brace Calligraphic
                     decoration={calligraphic brace, amplitude=3mm,
                                 raise=1mm, mirror},
                     very thick, pen colour={black}
                    },
       box/.style = {draw, rounded corners, fill=blue!20, align=center,
                     minimum height=4em, text width=6em},
  decision/.style = {diamond, aspect=1.5, draw, fill=blue!20,
                    inner xsep=-3pt, text width=5.4em, align=center},
                        ]
% nodes
    \begin{scope}[nodes={on chain=A, join=by arr}]
\node   [box]       {Init $n=0$, $T_0$, and $S_0$};     % name=A-1
\node   [box]       {$S_{n+1}=N(S_n)$};
\node   [decision]  {$f(S_{n+1}) \le f(S_n)$};
\node   [box]       {Accept $S_{n+1}$};
\node   [box]       {$T_{n+1} = K(T_n)$ and $n=n+1$};
\node   [decision]  {Stop?};                            % A-6
    \end{scope}
% nodes in right column
\node   [box, right=of A-2] (rej) {Reject $S_{n+1}$};
\node   [decision, at={(A-3 -| rej)}]   (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
\node   [box, at={(A-6 -| rej)}] (stop) {Stop};
% edge labels and connections not considered in join macro
\path   (A-3)   edge ["yes"] (A-4) 
        (A-3)   edge ["no"]  (rand)
        (rand)  edge ["no"]  (rej)
        (rej)   edge         (A-2)  
        (A-6)   edge ["yes"]  (stop);
\draw[arr]  (rand)  |- node[pos=0.75] {yes} (A-4);
\draw[arr]  (A-6.west) to ["no" '] ++(-1,0) |- (A-2);
% braces
\draw[BC]   ([xshift=-12mm] A-1.north west) coordinate (aux) -- 
                    node[midway,left=5mm]{$S_1$}
            (aux |- A-4.south);
\draw[BC]   (aux |- A-5.north) --
                    node[midway,left=5mm]{$S_2$}
            (aux |- A-6.south);
\end{tikzpicture}
    \end{center}
\end{document}

在此处输入图片描述

答案2

在代码末尾添加以下内容

 \draw[brace mirrored, ultra thick]($(init.north west)+(-2cm,0cm)$)--($(accp.south west)+(-2cm,0cm)$) 
      node[midway, left]{S1}
      ;
       \draw[brace mirrored, ultra thick]($(incr.north west)+(-2cm,0cm)$)--($(stcd.west)+(-2cm,-1cm)$) 
      node[midway, left]{S2}
      ;
  

在序言中,正确的语法是

\usetikzlibrary{tikzmark, positioning, fit, shapes.misc}
\usetikzlibrary{decorations.pathreplacing, calc}
\tikzset{brace/.style={decorate, decoration={brace}},
    brace mirrored/.style={decorate,decoration={brace,mirror}},
}

在此处输入图片描述

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt,on grid]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em,on grid]
\tikzstyle{line} = [draw, -latex]


\usetikzlibrary{tikzmark, positioning, fit, shapes.misc}
\usetikzlibrary{decorations.pathreplacing, calc}
\tikzset{brace/.style={decorate, decoration={brace}},
    brace mirrored/.style={decorate,decoration={brace,mirror}},
}
\begin{document}

  \begin{center}
    \resizebox{0.4 \linewidth}{!}{%
      \begin{tikzpicture}[node distance = 2cm, auto]
        \node[block]                                (init) {Init $n=0$, $T_0$, and $S_0$};
        \node[block, below= of init]                (nbrh) {$S_{n+1}=N(S_n)$};
        \node[decision, below= of nbrh]             (ovgt) {$f(S_{n+1}) \le f(S_n)$};
        \node[block, below=2.5cm of ovgt]           (accp) {Accept $S_{n+1}$};
        \node[decision, right= 3.5cm of ovgt]       (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
        \node[block, above=3cm of rand]             (rejj) {Reject $S_{n+1}$};
        \node[block, below= of accp]                (incr) {$T_{n+1} = K(T_n)$ and $n=n+1$};
        \node[decision, below=2.5cm of incr]              (stcd) {Stop?};  
        \node[block, right=3cm of stcd]                (stop) {Stop};


        \path[line] (init) --          (nbrh);
        \path[line] (nbrh) --          (ovgt);
        \path[line] (ovgt) -- node{yes}(accp);
        \path[line] (ovgt) -- node{no} (rand);
        \path[line] (rand) -- node{no} (rejj);
        \path[line] (rejj) --          (nbrh);
        \path[line] (rand) |- node{yes}(accp);
        \path[line] (accp) --          (incr);
        \path[line] (incr) --          (stcd);
        \path[line] (stcd) -- node{yes}(stop);
        \path[line] (stcd) -- ++(-2,0) |- node[pos=.25]{no}  (nbrh);
      
      \draw[brace mirrored, ultra thick]($(init.north west)+(-2cm,0cm)$)--($(accp.south west)+(-2cm,0cm)$) 
      node[midway, left]{S1}
      ;
       \draw[brace mirrored, ultra thick]($(incr.north west)+(-2cm,0cm)$)--($(stcd.west)+(-2cm,-1cm)$) 
      node[midway, left]{S2}
      ;
  

\end{tikzpicture}%
    }%
  \end{center}


\end{document}

答案3

我将节点放在本地作用域中,第一个节点称为“debut”,第二个节点称为“fin”。代码已注释%<--,如果您有任何疑问,请不要犹豫。

新答案:牙套对齐

截屏

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
%<-- decorations library
\usetikzlibrary{decorations.pathreplacing,calligraphy}
%<-- new syntax of style since Tikz 3.0
\tikzset{decision/.style={diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt,on grid},
        block/.style={rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em,on grid},
        line/.style={draw, -latex}}

\begin{document}

  \begin{center}
    \resizebox{0.4 \linewidth}{!}{%
      \begin{tikzpicture}[node distance = 2cm, auto,decoration={calligraphic brace,amplitude=5mm}]%<-- definition of the brace decoration
      \begin{scope}[local bounding box=debut]%<--local scope "debut"
      \node[block]                                (init) {Init $n=0$, $T_0$, and $S_0$};
        \node[block, below= of init]                (nbrh) {$S_{n+1}=N(S_n)$};
        \node[decision, below= of nbrh]             (ovgt) {$f(S_{n+1}) \le f(S_n)$};
        \node[block, below=2.5cm of ovgt]           (accp) {Accept $S_{n+1}$};
        \node[decision, right= 3.5cm of ovgt]       (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
      \end{scope}%<-- end of local scope "debut"

        \node[block, above=3cm of rand]             (rejj) {Reject $S_{n+1}$};
        
        \begin{scope}[local bounding box=fin]%<-- local scope "fin"
        
        \node[block, below= of accp]                (incr) {$T_{n+1} = K(T_n)$ and $n=n+1$};
        \node[decision, below=2.5cm of incr]              (stcd) {Stop?}; 
        \node[inner sep=0pt,outer sep=0pt] at (debut.south west|-incr.north west){};%<-- alignment of the "fin" scope to the previous "debut" scope.
        \end{scope}%<-- end of local scope "fin"

        \node[block, right=3cm of stcd]                (stop) {Stop};


        \path[line] (init) --          (nbrh);
        \path[line] (nbrh) --          (ovgt);
        \path[line] (ovgt) -- node{yes}(accp);
        \path[line] (ovgt) -- node{no} (rand);
        \path[line] (rand) -- node{no} (rejj);
        \path[line] (rejj) --          (nbrh);
        \path[line] (rand) |- node{yes}(accp);
        \path[line] (accp) --          (incr);
        \path[line] (incr) --          (stcd);
        \path[line] (stcd) -- node{yes}(stop);
        \path[line] (stcd) -- ++(-2,0) |- node[pos=.25]{no}  (nbrh);
        
        % brace décoration
        \draw[decorate,ultra thick,transform canvas={xshift=-20mm}] (debut.south west)--(debut.north west)node[midway,left,xshift=-1em]{$S_1$};
        \draw[decorate,ultra thick,transform canvas={xshift=-20mm}] (fin.south west)--(fin.north west)node[midway,left,xshift=-1em]{$S_1$};
      \end{tikzpicture}%
    }%
  \end{center}

\end{document}

旧答案

截屏

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\usetikzlibrary{decorations.pathreplacing,calligraphy}%<-- decorations library
\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt,on grid]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em,on grid]
\tikzstyle{line} = [draw, -latex]

\begin{document}

  \begin{center}
    \resizebox{0.4 \linewidth}{!}{%
      \begin{tikzpicture}[node distance = 2cm, auto,decoration={calligraphic brace,amplitude=5mm}]%<-- definition of the brace decoration
      \begin{scope}[local bounding box=debut]%<--local scope "debut"
      \node[block]                                (init) {Init $n=0$, $T_0$, and $S_0$};
        \node[block, below= of init]                (nbrh) {$S_{n+1}=N(S_n)$};
        \node[decision, below= of nbrh]             (ovgt) {$f(S_{n+1}) \le f(S_n)$};
        \node[block, below=2.5cm of ovgt]           (accp) {Accept $S_{n+1}$};
        \node[decision, right= 3.5cm of ovgt]       (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
      \end{scope}%<-- end of local scope "debut"
        
        \node[block, above=3cm of rand]             (rejj) {Reject $S_{n+1}$};
        
        \begin{scope}[local bounding box=fin]%<-- local scope "fin"
        \node[block, below= of accp]                (incr) {$T_{n+1} = K(T_n)$ and $n=n+1$};
        \node[decision, below=2.5cm of incr]              (stcd) {Stop?}; 
        \end{scope}%<-- end of local scope "fin"
 
        \node[block, right=3cm of stcd]                (stop) {Stop};


        \path[line] (init) --          (nbrh);
        \path[line] (nbrh) --          (ovgt);
        \path[line] (ovgt) -- node{yes}(accp);
        \path[line] (ovgt) -- node{no} (rand);
        \path[line] (rand) -- node{no} (rejj);
        \path[line] (rejj) --          (nbrh);
        \path[line] (rand) |- node{yes}(accp);
        \path[line] (accp) --          (incr);
        \path[line] (incr) --          (stcd);
        \path[line] (stcd) -- node{yes}(stop);
        \path[line] (stcd) -- ++(-2,0) |- node[pos=.25]{no}  (nbrh);
        
        % brace décoration
        \draw[decorate,ultra thick,transform canvas={xshift=-20mm}] (debut.south west)--(debut.north west)node[midway,left,xshift=-1em]{$S_1$};
        \draw[decorate,ultra thick,transform canvas={xshift=-20mm}] (fin.south west)--(fin.north west)node[midway,left,xshift=-1em]{$S_1$};
      \end{tikzpicture}%
    }%
  \end{center}

\end{document}

相关内容