如何构建 tikz 流程图而不明确说明框位置?

如何构建 tikz 流程图而不明确说明框位置?

在 tikz 中,流程图通常是这样构建的:

\documentclass{article}

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


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

\begin{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [block] (init) {initialize model};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [block, below of=init] (identify) {identify candidate models};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {yes} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    \path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}


\end{document}

但是,当您并不真正关心元素的位置时,手动指定每个元素的“左侧/右侧/下方”可能会很烦人且令人厌烦。有没有办法让 tikz/latex 自动定位图表中的元素?

答案1

我认为tikz matrix你要找的是,请参阅第 20 章TikZ & PGF 手册

也可以看看(!包 pgf 错误:没有已知名为 m-2-2 的形状)寻找其他可能的解决方案应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?这解释了为什么最好不要使用tykzstyle

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix, shapes,arrows.meta}
\tikzset{
    block/.style={
        rectangle, draw, fill=blue!20, 
        text width=5em, text centered, rounded corners, minimum height=4em
    },
    cloud/.style={
        draw, ellipse,fill=red!20, node distance=3cm,
        minimum height=2em
    },  
    decision/.style={
        diamond, draw, fill=blue!20, 
        text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt
    },
    line/.style = {draw, -Latex},
}
\begin{document}
    \pagestyle{empty}
    \begin{center}
    \begin{tikzpicture}
    \matrix[
        row sep=7ex,
        column sep=3em,
        column 1/.style={nodes={anchor=east}}
        ] {
        \node [cloud] (expert) {expert}; &
        \node [block] (init) {initialize model}; &
        \node [cloud] (system) {system};\\
        & \node [block] (identify) {identify\\ candidate models};\\
        \node [block] (update) {update model}; & 
        \node [block] (evaluate) {evaluate\\ candidate models};\\
        & \node [decision] (decide) {is best candidate better?};\\
        & \node [block] (stop) {stop};  \\
        };
        % Draw edges
        \path [line] (init) -- (identify);
        \path [line] (identify) -- (evaluate);
        \path [line] (evaluate) -- (decide);
        \path [line] (decide) -| node [near start, above] {yes} (update);
        \path [line] (update) |- (identify);
        \path [line] (decide) -- node[right] {no}(stop);
        \path [line,dashed] (expert) -- (init);
        \path [line,dashed] (system) -- (init);
        \path [line,dashed] (system) |- (evaluate);
    \end{tikzpicture}
    \end{center}
\end{document}

在此处输入图片描述

当所有矩阵节点都具有相同的 时style,最好使用 ,matrix of nodes这样可以节省一些输入。节点的样式在矩阵选项中选择:nodes=...并且默认情况下,所有节点都以矩阵名称及其行和列位置命名。因此,可以将类似 的内容\node [cloud] (expert) {expert};替换为expert

当某个节点需要特定样式和/或名称时,使用的语法是|[style] (name)| contents。特定语法style将应用于.append style默认语法。

并且默认情况下,来自 a 的所有节点的matrix of nodes锚点都固定在它们的base而不是它们的中center

考虑到所有这些因素,上面的代码可以写成

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix, shapes,arrows.meta}
\tikzset{
    block/.style={
        rectangle, draw, fill=blue!20, 
        text width=5em, text centered, rounded corners, minimum height=4em
    },
    cloud/.style={
        draw, ellipse,fill=red!20, node distance=3cm,
        minimum height=2em, 
        text width={} %<------- to correct `text width` from `block`
    },  
    decision/.style={
        diamond, draw, fill=blue!20, 
        sharp corners, %<------- to correct `rounded corners` from `block`
        text width=4.5em, text badly centered, 
        node distance=3cm, inner sep=0pt
    },
    line/.style = {draw, -Latex},
}
\begin{document}
    \pagestyle{empty}
    \begin{center}
    \begin{tikzpicture}
    \matrix[
        matrix of nodes,   %<------ new option
        nodes={block, anchor=center},  %<------ default style for nodes 
        row sep=7ex,
        column sep=3em,
        column 1/.style={nodes={anchor=east}}
        ] {
        |[cloud] (expert)| expert &
        |(init)| initialize model &
        |[cloud] (system)| system\\
        & |(identify)| {identify\\ candidate models}\\
        |(update)| update model & 
        |(evaluate)| {evaluate\\ candidate models}\\
        & |[decision] (decide)| is best candidate better?\\
        & |(stop)| stop \\
        };
        % Draw edges
        \path [line] (init) -- (identify);
        \path [line] (identify) -- (evaluate);
        \path [line] (evaluate) -- (decide);
        \path [line] (decide) -| node [near start, above] {yes} (update);
        \path [line] (update) |- (identify);
        \path [line] (decide) -- node[right] {no}(stop);
        \path [line,dashed] (expert) -- (init);
        \path [line,dashed] (system) -- (init);
        \path [line,dashed] (system) |- (evaluate);
    \end{tikzpicture}
    \end{center}
\end{document}

相关内容