在圖表中水平分組容器中的兩個區塊

在圖表中水平分組容器中的兩個區塊

如何將 C 和 D 水平(而不是垂直)分組在一起(如下圖所示),同時仍保持容器的中心對齊?

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,fit}
\begin{document}
\pagestyle{empty}


% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=yellow, 
    text width=5em, text centered, rounded corners, minimum height=2em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [cylinder, draw, shape aspect=.5, shape border rotate=90, minimum height=1cm] (A) {A};
    \node [block, below =of A] (B) {B};
    \node [block, below =of B] (C) {C};
    \node [block, below =of C] (D) {D};
    \node [draw, fit= (C) (D)] (G) {};
    \node [block, below =of D] (E) {E};
    \node [text centered, below =of E] (F) {F};
    % Draw edges
    \path [line] (A) -- (B);
    \path [line] (B) -- (G);
    \path [line] (G) -- (E);
    \path [line] (E) -- (F);
\end{tikzpicture}


\end{document}

答案1

如果 C 和 D 具有相同的寬度和高度,您可以這樣做:

\documentclass[tikz, border=10pt, multi]{standalone}
\usetikzlibrary{shapes.geometric,arrows,positioning,fit}
\begin{document}
% Define block styles
\tikzset{%
  block/.style = {rectangle, draw, fill=yellow, text width=5em, text centered, rounded corners, minimum height=2em},
  line/.style = {draw, -latex'},
}
\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [cylinder, draw, shape aspect=.5, shape border rotate=90, minimum height=1cm] (A) {A};
    \node [block, below=of A] (B) {B};
    \node [block, below=of B, anchor=north east, xshift=-2.5mm] (C) {C};
    \node [block, below=of B, anchor=north west, xshift=2.5mm] (D) {D};
    \node [draw, fit= (C) (D)] (G) {};
    \node [block, below=of G] (E) {E};
    \node [text centered, below =of E] (F) {F};
    % Draw edges
    \path [line] (A) -- (B);
    \path [line] (B) -- (G);
    \path [line] (G) -- (E);
    \path [line] (E) -- (F);
\end{tikzpicture}
\end{document}

調整對齊

請注意,它\tikzstyle已被棄用,因此我更新了語法。arrows也已棄用,但我沒有更改它,因為更新到arrows.meta可能會為您帶來略有不同的結果。

答案2

節點matrix可以替代fit,內部節點大小之間的關係並不重要,您不必擔心手動定位。

\documentclass[tikz, border=10pt, multi]{standalone}
\usetikzlibrary{shapes.geometric,arrows,positioning}
\begin{document}
% Define block styles
\tikzset{%
  block/.style = {rectangle, draw, fill=yellow, text width=5em, text centered, rounded corners, minimum height=2em},
  line/.style = {draw, -latex'},
}
\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [cylinder, draw, shape aspect=.5, shape border rotate=90, minimum height=1cm] (A) {A};
    \node [block, below=of A] (B) {B};
    \matrix (CD) [draw, below=of B, column sep=2mm]{
        \node [block] (C) {C}; &
        \node [block] (D) {D}; \\
     };
    \node [block, below=of CD] (E) {E};
    \matrix (CD2) [draw, below=of E, column sep=2mm]{
        \node [block, minimum width=3cm, minimum height=1cm, anchor=center] (C) {C}; &
        \node [block, minimum size=2cm, anchor=center] (D) {D}; \\
     };
    \node [text centered, below =of CD2] (F) {F};
    % Draw edges
    \path [line] (A) -- (B);
    \path [line] (B) -- (CD);
    \path [line] (CD) -- (E);
    \path [line] (E) -- (CD2);
    \path [line] (CD2) -- (F);
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容