用于生成 TikZ 流程图的自定义数据结构

用于生成 TikZ 流程图的自定义数据结构

我正在尝试使用 TikZ 流程图可视化并描述流程的步骤。我希望使解决方案尽可能具有可扩展性,最好有一些与流程的每个步骤相关联的数据结构,并以此为基础创建流程图。类似于以下列表:

\begin{process}
   \item[short_description, output, long_details]
   \item[short_description, output, long_details]
\end{process}

结果如下:

在此处输入图片描述

理想情况下使用流程图包装(但让我们一步一步解决问题......)

到目前为止,我已经能够理清单行连接块序列的绘制,但我无法尝试引入一些要迭代的数据结构。在 TeX 中构建这种数据结构的最佳方法是什么?

\documentclass[10pt]{report}

\usepackage{blindtext}
\usepackage[a4paper, margin={1in}]{geometry}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{block} = [rectangle, minimum width=2cm, minimum height=1cm, align=center, text centered, text width=2cm, draw=black]
\tikzstyle{arrow} = [thick,->,>=stealth]


\begin{document}

\begin{tikzpicture}[node distance=1cm]

% Create a sequence of blocks  with arrows
\foreach \x [remember=\x as \lastx (initially 0)] in {0,...,5}{
    \if\x0
        \node (n\x) [block]  {n\x};
    \else 
        \node (n\x) [block, right = of n\lastx]  {n\x};
        \draw [arrow] (n\lastx) -- (n\x) node[midway, above]() {$s(\lastx)$};
    \fi
}

\end{tikzpicture}

\hfill

Long description:
\begin{enumerate} 
    \item [n0] Long description of what n0 does
    \item [n1] Long description of what n1 does

\end{enumerate}

\end{document}

答案1

我不确定这是否正是您想要的,但使用chains库可以实现这一点。定义一个宏\process,以逗号分隔的列表作为参数。每个条目应具有以下形式<short desc>/<input>/<long description>。请注意,箭头是输入join箭头,而不是输出箭头,因此第一个被忽略。由于选项与 配合使用的方式,这样做更容易chains

第一个链是带有连接的流程图;第二个链有描述。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains, arrows.meta, quotes}

\tikzset{block/.style={draw, minimum width=2cm, minimum height=1cm}}

\newcommand{\process}[1]{
    \begin{tikzpicture}[start chain=1, start chain=2, node distance=1mm and 1cm,
            every join/.style={thick, -Stealth}]
        \node[on chain=2] at (-1,-.7){}; 
        \foreach \S/\O/\L in {#1}{
            \node[on chain=1, block, join=by "\O"]{\S};
            \node[on chain=2 going {below=of \tikzchainprevious.south west, anchor=north west}]{\S\;\L};
        }
    \end{tikzpicture}
}

\begin{document}

\process{
    n0/ignored/Long description of what n0 does,
    n1/$s(0)$/Long description of what n1 does,
    n2/$s(1)$/Long description of what n2 does,
    n3/$s(2)$/Long description of what n3 does
}

\end{document}

相关内容