在流程图中添加彩色框

在流程图中添加彩色框

我正在尝试制作一个flowchartLatex我想把一些步骤放在一个带标签的彩色框中,例如确定性退火,我添加了scope部分但它与新的更改不兼容。

\documentclass[useAMS,usenatbib,usegraphicx]{mn2e}
\usepackage{verbatim}
\usepackage{bm}
\usepackage{subfigure}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows}


\tikzstyle{decision} = [diamond, draw, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt, minimum height=5em]
\tikzstyle{block} = [rectangle, draw, 
    text width=5em, text centered, rounded corners, minimum height=5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=2cm,
    minimum height=2em]
\begin{document}

\begin{figure}
\flushleft
\begin{tikzpicture}[node distance = 1.45cm, auto]
\tiny
    % Place nodes
    \node [cloud] (start) {Start};
    \node [block, below of=start, node distance=1.5cm] (kde) {\tiny Apply KDE};
    \node [block, below of=kde, node distance=1.5cm] (init) {\tiny Initialize $\beta = \beta_0$\\$\hat{M}= \hat{M_0}$};
    \node [block, below of=init] (Centroid) {\tiny Update $\mu_c$, $\mu_{c'}$\\ $\sigma_c$,$\sigma_{c'}$\\ $\theta$};
    \node [block, below of=Centroid,text width=2.8cm] (Softassign) {\tiny Update $Q_{ij}$\\$M^{0}_{ij}\leftarrow \exp(\beta Q_{ij})$};
    \node [block, below of=Softassign] (slack) {\tiny Update \\$\hat{M^{1}_{ij}}$ \\ $\hat{M^{0}_{ij}}$};
    \node [decision, below of=slack,text width=1.25cm] (Sinkhorn) {\tiny Convergence\\ of $\hat M$ \\ or $I > I_1$};
    \node [decision, left of=Sinkhorn, node distance=2.8cm,text width=1.25cm] (Convergence of match matrix) {\tiny Convergence\\ of $M$ \\ or $I > I_0$};
    \node [block, below of =Sinkhorn,text width=1.0cm,yshift=-0.5cm] (End of Deterministic Annealing) {\tiny $\beta\leftarrow \beta_r\beta$};
    \node [decision, right of=End of Deterministic Annealing, node distance=2.2cm] (Deterministic Annealing) {\tiny $\beta \geq \beta_f $};
    \begin{scope}
    \node[rectangle,draw,minimum width=1cm][fit = (Centroid)(Softassign)(slack)(Sinkhorn)(Convergence of match matrix)(End of Deterministic Annealing)(Deterministic Annealing),fill=red!20] (DA) {};
    \node [above right] at (DA.north west) { Deterministic Annealing};
    \end{scope}
    \begin{scope}
    \node[rectangle,draw,minimum width=1cm][fit = (Softassign)(slack)(Sinkhorn)(Convergence of match matrix),fill=green!20] (SA) {};
    \node [above left] at (SA.north east) { Softassign};
    \end{scope}

    \node [block, right of=slack, node distance=3.2cm] (SVD) {\tiny Update\\ translations\\ via SVD};
    \node [decision, right of=init, node distance=3.2cm,text width=1.8cm] (Zero Points) {\tiny $\Delta C_{\gamma}\ll 1$ \\ and \\ $\Delta C_{\zeta}\ll 1$};
    \node [cloud, right of=Zero Points, node distance=2.0cm] (stop) {Stop};
    % Draw edges
    \path [line] (start) -- (kde);
    \path [line] (kde) -- (init);
    \path [line] (init) -- (Centroid);
    \path [line] (Centroid) -- (Softassign);
    \path [line] (Softassign) -- (slack);
    \path [line] (slack) -- (Sinkhorn);
    \path [line] (Sinkhorn) --+(1.5,0) -- +(1.5,3) -- node {No}(slack);
    \path [line] (Sinkhorn) -- node[above] {Yes} (Convergence of match matrix);
    \path [line] (Convergence of match matrix) |-  node[above][left]  {Yes}(End of Deterministic Annealing);
    \path [line] (Convergence of match matrix) |- node[above] {No}(Centroid);
    \path [line] (End of Deterministic Annealing) -- (Deterministic Annealing);
    \path [line] (Deterministic Annealing) |- node[above] {No}(Centroid);
    \path [line] (Deterministic Annealing) -| node[above][right] {Yes}(SVD);
    \path [line] (SVD) -- (Zero Points);
    \path [line] (Zero Points) -- node [above]{No}(init);
%     \path [line,dashed] (expert) -- (init);
    \path [line] (Zero Points) -- node[above]{Yes}(stop);
\end{tikzpicture}
\caption{The heuristic colour calibration procedure flowchart. The parameters in flowchart are explained in Table \ref{tab:initial} with details.}
\label{fig:flowchart}
\end{figure}

\end{document}

我基本上希望在流程图中包含以下内容:

1. 文字标签及彩框不得相互重叠,且不得与线条或方框重叠。

2. 想办法让缺失的盒子再次出现。

更新:

在此处输入图片描述 有人能指出我的错误是什么吗?

答案1

首先,你的代码中缺少很多 TikZ 库,

\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows}

两个大彩色矩形覆盖所有其他节点的原因自然是因为你删除了 选项[on background layer]scope没有这个选项,它们只会绘制在之前的内容之上。

在下面的代码中,我稍微修改了物体的定位和绘制方式。我添加了positioning库,并使用了它的语法(below=of othernode而不是below of=othernode)。我还将SVD节点相对于DA节点放置,将Zero points节点相对于init和放置SVD,并使用\node [..] at (init -| SVD) (Zero Points)。这样,在放置这些物体时,反复试验的次数就会减少。

要使图表变窄,可以减小右下角菱形节点的宽度。如果将stop节点多于节点Zero points是一种选择,可以节省大量的水平空间。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,backgrounds,fit,arrows,positioning}
\tikzset{decision/.style={diamond, draw, 
    text width=4.5em, text badly centered, inner sep=0pt, minimum height=5em},
block/.style={rectangle, draw, 
    text width=5em, text centered, rounded corners},
line/.style={draw, -latex'},
cloud/.style={draw, ellipse}}
\begin{document}

\begin{tikzpicture}[auto,every node/.style={font=\tiny,align=center},node distance=0.5cm]
    % Place nodes
    \node [cloud] (start) {Start};
    \node [block, below=of start] (kde) { Apply KDE};
    \node [block, below=of kde] (init) { Initialize $\beta = \beta_0$\\$\hat{M}= \hat{M_0}$};
    \node [block, below=of init] (Centroid) { Update $\mu_c$, $\mu_{c'}$\\ $\sigma_c$,$\sigma_{c'}$\\ $\theta$};
    \node [block, below=of Centroid,text width=2.8cm] (Softassign) { Update $Q_{ij}$\\$M^{0}_{ij}\leftarrow \exp(\beta Q_{ij})$};
    \node [block, below=of Softassign] (slack) { Update \\$\hat{M^{1}_{ij}}$ \\ $\hat{M^{0}_{ij}}$};
    \node [decision, below=of slack,text width=1.25cm] (Sinkhorn) { Convergence\\ of $\hat M$ \\ or $I > I_1$};
    \node [decision, left=0.5cm of Sinkhorn,text width=1.25cm] (Convergence of match matrix) { Convergence\\ of $M$ \\ or $I > I_0$};
    \node [block, below=1cm of Sinkhorn,text width=1.0cm] (End of Deterministic Annealing) { $\beta\leftarrow \beta_r\beta$};
    \node [decision, right=0.5cm of End of Deterministic Annealing, node distance=2.2cm] (Deterministic Annealing) { $\beta \geq \beta_f $};

    \begin{scope}[on background layer]
    \node[rectangle,draw,minimum width=1cm][fit = (Centroid)(Softassign)(slack)(Sinkhorn)(Convergence of match matrix)(End of Deterministic Annealing)(Deterministic Annealing)] (DA) {};
    \filldraw [fill=red!20] ([xshift=-3pt]DA.south west) rectangle (DA.north east);
    \node [above right,font=\bfseries\tiny] at (DA.north west) {Deterministic Annealing};
    \node[rectangle,draw,minimum width=1cm][fit = (Softassign)(slack)(Sinkhorn)(Convergence of match matrix),fill=green!20] (SA) {};
    \node [above right,xshift=1cm] at (SA.north west) {Softassign};
    \end{scope}

    \node [block,text width=4em, right=0.1cm of DA] (SVD) { Update\\ translations\\ via SVD};
    \node [decision, text width=1.8cm] at (init -|SVD) (Zero Points) { $\Delta C_{\gamma}\ll 1$ \\ and \\ $\Delta C_{\zeta}\ll 1$};
    \node [cloud, right=0.4cm of Zero Points] (stop) {Stop};
    % Draw edges
    \path [line] (start) -- (kde);
    \path [line] (kde) -- (init);
    \path [line] (init) -- (Centroid);
    \path [line] (Centroid) -- (Softassign);
    \path [line] (Softassign) -- (slack);
    \path [line] (slack) -- (Sinkhorn);
    \path [line] (Sinkhorn) --+(1.3,0) |-  node[above] {No}(slack);
    \path [line] (Sinkhorn) -- node[above] {Yes} (Convergence of match matrix);
    \path [line] (Convergence of match matrix) |-  node[above][left]  {Yes}(End of Deterministic Annealing);
    \path [line] (Convergence of match matrix) |- node[above] {No}(Centroid);
    \path [line] (End of Deterministic Annealing) -- (Deterministic Annealing);
    \path [line] (Deterministic Annealing) |- node[above] {No}(Centroid);
    \path [line] (Deterministic Annealing) -| node[above][right] {Yes}(SVD);
    \path [line] (SVD) -- (Zero Points);
    \path [line] (Zero Points) -- node [above]{No}(init);
%     \path [line,dashed] (expert) -- (init);
    \path [line] (Zero Points) -- node[above]{Yes}(stop);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

不知怎的,我失去了纠正你那缺乏很多细节的(非)MWE 的心情。(你猜对了!我疯了 ;-)不过,这里有一个 MWE 显示了你想要实现的目标:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds,positioning}
\begin{document}
\begin{tikzpicture}

\node (dm1) at (0,3) {Some};
\node[below = 2cm of dm1] (dm2)  {Another node here};
\begin{scope}[on background layer]
\node[rectangle,draw,minimum width=2cm] [fit = (dm1) (dm2),fill=green!20,label=above:Some label] (bx4) {};
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容