Tikz 向 Node 或者 Tikzstyle 添加 Draw 命令

Tikz 向 Node 或者 Tikzstyle 添加 Draw 命令

我正在尝试创建一个流程图,每个框包含两个附加框,左上角有一个白色框,右上角有一个带有块编号的白色框。

流程图设计

当在声明中定义主框时,有没有办法自动绘制白框\tikzstyle{}?还是我必须为每个块手动绘制它们?我目前的解决方法是将白框定义为节点,并将它们在 xy 坐标中移动以将它们对齐到我需要的位置。

这是我当前的代码:

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usepackage[letterpaper, margin=1.0cm,showframe, nohead, nofoot, nomarginpar]{geometry}
\usepackage[default]{lato}

% Define Styles
\tikzstyle{nominal} = [rectangle, minimum width=5.5cm, minimum height=3cm, draw=black, line width=0.5mm, fill=gray!40, text width=50mm]
\tikzstyle{white} = [rectangle, minimum width=0.5cm, minimum height=0.5cm, text centered, draw=black, line width=0.5mm, fill=white]
\tikzstyle{arrow} = [thick, ->, >=stealth]

\begin{document}
\vspace*{1em}

% Tikz Picture
\begin{tikzpicture}[node distance=2cm]

% Input Nodes
\node (1) [nominal ] {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\newline First block};
\node (2) [nominal, right of=1, xshift=4.5cm] {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\newline the second block};
\node (3) [nominal, below of=2, yshift=-2.5cm] {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\newline the third block};

% Draw White boxes
\node (w1_L) [white, below of=1, xshift=-2.5cm, yshift=3.25cm] {};
\node (w1_R) [white, below of=1, xshift=2.5cm, yshift=3.25cm] {1};
\node (w2_L) [white, below of=2, xshift=-2.5cm, yshift=3.25cm] {};
\node (w2_R) [white, below of=2, xshift=2.5cm, yshift=3.25cm] {2};
\node (w3_L) [white, below of=3, xshift=-2.5cm, yshift=3.25cm] {};
\node (w3_R) [white, below of=3, xshift=2.5cm, yshift=3.25cm] {3};

% Draw Arrows
\draw [arrow] (1) -- (2);
\draw [arrow] (1) |- (3);

\end{tikzpicture}

\end{document}

答案1

不需要特殊的宏来连接三个节点或一个,这个节点可以像具有两个内部的path picture常规节点一样绘制。nodelabels

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\tikzset{nominal/.style = {rectangle, minimum width=5.5cm, 
        minimum height=3cm, draw=black, line width=0.5mm, 
        fill=gray!40, text width=50mm},
    smallwhite/.style = {rectangle, minimum width=0.5cm, minimum height=0.5cm, 
        text centered, draw=black, line width=0.5mm, fill=white},
    arrow/.style = {thick, ->, >=stealth},
}

\begin{tikzpicture}[node distance=2cm and 3cm,
    ]
    \node[nominal, 
        label={[smallwhite, anchor=north east]north east:1}, 
        label={[smallwhite, anchor=north west]north west:A}] (one) {first block};

    \node[nominal, 
        label={[smallwhite, anchor=north east]north east:2}, 
        label={[smallwhite, anchor=north west]north west:B},
        right=of one] (two) {second block};

    \node[nominal, 
        label={[smallwhite, anchor=north east]north east:3}, 
        label={[smallwhite, anchor=north west]north west:C},
        below=of two] (three) {third block};

    \draw [arrow] (one) -- (two);
    \draw [arrow] (one) |- (three);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我假设左上角的子节点始终为空:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning}

\tikzset{
mainnode/.style = {rectangle, draw, line width=0.5mm, fill=gray!40,
                   text width=50mm, minimum height=30mm, inner sep=2.5mm, outer sep=0mm,
                   path picture={    % <-- added
                   \node [subnode, below right=0mm of path picture bounding box.north west] {};
                   \node [subnode, below  left=0mm of path picture bounding box.north east] {#1};
                                }%%
                   },
 subnode/.style = {rectangle, draw, line width=0.5mm, fill=white,
                   minimum height=7mm, text width=5mm, align=center,
                   inner sep=1mm, outer sep=0mm},
   arrow/.style = {thick, -Stealth}
        }

\begin{document}
    \begin{tikzpicture}[
node distance=1cm and 2cm
                        ]
% nodes
\node (n1) [mainnode=1]     {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             First block};
\node (n2) [mainnode=2,
            right=of n1]    {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             The second block};
\node (n3) [mainnode=3,
            below=of n2]    {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             The third block};
% arrows
\draw [arrow] (n1) -- (n2);
\draw [arrow] (n1) |- (n3);
    \end{tikzpicture}
\end{document}

编辑: 如果左上角子节点也有一些文本,那么您需要将定义更改mainnode

mainnode/.style args = {#1/#2}%
        {rectangle, draw, line width=0.5mm, fill=gray!40,
         text width=50mm, minimum height=30mm, inner sep=2.5mm, outer sep=0mm,
         path picture={    % <-- added
         \node [subnode, below right=0mm of path picture bounding box.north west] {#1};
         \node [subnode, below  left=0mm of path picture bounding box.north east] {#2};
                    }%%
       },

然后使用样式例如:

    \begin{tikzpicture}[
node distance=1cm and 2cm
                        ]
% nodes
\node (n1) [mainnode= /1]     {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             First block};
\node (n2) [mainnode= /2,
            right=of n1]    {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             The second block};
\node (n3) [mainnode=C/3,
            below=of n2]    {\hspace{0.5cm}\textbf{\_\_ / \_\_:\_\_}\\
                             The third block};
% arrows
\draw [arrow] (n1) -- (n2);
\draw [arrow] (n1) |- (n3);
    \end{tikzpicture}

您将获得:

在此处输入图片描述

答案3

嗯,@Zarko 解决方案更好......但这是另一个带有简单宏的解决方案。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\tikzset{nominal/.style = {rectangle, minimum width=5.5cm, 
        minimum height=3cm, draw=black, line width=0.5mm, 
        fill=gray!40, text width=50mm},
    smallwhite/.style = {rectangle, minimum width=0.5cm, minimum height=0.5cm, 
        text centered, draw=black, line width=0.5mm, fill=white},
    arrow/.style = {thick, ->, >=stealth},
}
\newcommand{\pnominal}[5][]{%
    \node (#2) [nominal, #1] {#3}; 
    \node at (#2.north east) (#2-left) [smallwhite, anchor = north east] {#4}; 
    \node at (#2.north west) (#2-right) [smallwhite, anchor = north west] {#5}; 
}
\begin{tikzpicture}[node distance=2cm and 3cm,
    ]
    \pnominal[]{one}{first block}{A}{1}
    \pnominal[right= of one]{two}{second block}{B}{2}
    \pnominal[below= of two]{three}{third block}{C}{3}

    \draw [arrow] (one) -- (two);
    \draw [arrow] (one) |- (three);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容