TikZ:背景边框

TikZ:背景边框

我曾经TikZ创建过一些很棒的流程图,但我仍然很难理解如何将背景应用于我的流程图。具体来说,我想根据下图在某些节点周围设置一条边框,并在底部或顶部附加文本。目前,我的流程图看起来像以下示例和相关的 MWE:

当前状态:

在此处输入图片描述

梅威瑟:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,matrix,intersections,positioning,chains}

\begin{document}
\begin{tikzpicture}[node distance = 1.5cm]
% Styles
    \tikzstyle{start} = [rectangle, rounded corners, text width=3cm, minimum height=1cm,text centered, draw=black]
    \tikzstyle{process} = [rectangle, text width=3cm, minimum height=1cm, text centered, draw=black]
    \tikzstyle{arrow} = [thick,->,>=latex]
% Nodes
    \node (P1) [process] {Part 1A};
    \node (start) [start,left of=P1,xshift=-2.8cm] {Start};
    \node (P2) [process,below of=P1] {Part 2A};
    \node (P3) [process,below of=P2] {Part 3A};
    \node (P4) [process,below of=P3] {Part 1B};
    \node (P5) [process,below of=P4] {Part 2B};
    \node (P6) [process,below of=P5] {Part 3B};
    \node (stop) [start,right of=P6,xshift=2.8cm] {End};
% Arrows
    \draw [arrow] (start) -- (P1);
    \draw [arrow] (P1) -- (P2);
    \draw [arrow] (P2) -- (P3);
    \draw [arrow] (P3) -- (P4);
    \draw [arrow] (P4) -- (P5);
    \draw [arrow] (P5) -- (P6);
    \draw [arrow] (P6) -- (stop);
\end{tikzpicture}
\end{document}

期望状态:(我用颜料画出了边框。如果可能的话,我更喜欢正确对齐的虚线边框) 在此处输入图片描述

答案1

  • 利用tikzchains及其宏join
  • 添加 带有标签 TEXT PART A 和 TEXT PART B 的矩形(节点)tikzfit
  • 用于节点定位的语法由库提供positioningright=of start而不是错误的right of=start

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                chains,
                fit,            % new
                intersections,  % not used
                matrix,         % not used
                positioning,
                shapes          % not needed
                }

\begin{document}
    \begin{tikzpicture}[
    node distance = 6mm and 8mm,
      start chain = going below,
      base/.style = {draw, semithick, text width=3cm, minimum height=1cm, align=center},
       FIT/.style = {draw, inner sep=2.5mm},
   process/.style = {base, on chain, join=by -Stealth},
     start/.style = {base, rounded corners, on chain, join=by -Stealth},
                        ]
% Nodes
    \node (start)   [start]                     {Start};
    \node (P1)      [process,right=of start]    {Part 1A};
    \node (P2)      [process]                   {Part 2A};
    \node (P3)      [process]                   {Part 3A};
    \node (P4)      [process]                   {Part 1B};
    \node (P5)      [process]                   {Part 2B};
    \node (P6)      [process]                   {Part 3B};
    \node (stop)    [start,right=of P6]         {End};
\node [FIT, fit=(start) (P2),
       label={[anchor=south west, font=\bfseries]south west: TEXT PART A}] {};
\node [FIT, fit=(P3) (stop),
       label={[anchor=north east, font=\bfseries]north east: TEXT PART B}] {};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容