在选定的节点集上绘制 Tikz 函数域/余域

在选定的节点集上绘制 Tikz 函数域/余域

我正在尝试绘制一个流程图,以概念化决策过程,其中 1)发起者首先选择与谁合作,2)与这个人或那个人一起做什么任务。

我发现这个话题它解决了我所要做的事情,但它的\.fit功能适用于所有三组节点,而我只打算在 B 组和 C 组上定义块环境(因为我的 A 组只包含一个节点)。此外,对于 C 组中列出的某些任务,我拥有相当长的名称/文本,因此所创建的块环境\.fit不会增长到这些长文本的长度。最终结果是块会切断这些长文本。

为了让您了解这个想法,这里有一个Tex我从先前的主题中修改的示例代码:

        \documentclass{article}
        \usepackage{tikz}
        \usetikzlibrary{arrows,positioning,shapes,fit,calc}
        \pgfdeclarelayer{background}
        \pgfsetlayers{background,main}
        \begin{document}
        \begin{tikzpicture}[
        every node/.style={on grid},
        setA/.style={fill=black, circle, inner sep=1.2pt},
        setC/.style={fill=red, rectangle,inner sep=2pt},
        every fit/.style={draw, fill=white,rectangle,rounded corners=5pt, text width=80pt},>=latex]

         % set A
        \node [setA, label=left:a] (a) {};
        \node[above=of a,anchor=south] {};

        % set B
        \node[inner sep=0pt,right=5cm of a] (x) {Person x};
        \node[below = of x] (y) {Person y};
        \node[inner sep=0pt,below = of y] (z) {Person z};
        \node[above=of x,anchor=south] {$B$};

        % set C
        \node[setC,label=right:$m$,right = 6cm of x] (m) {Task a: xxxxxx};
        \node[setC,label=right:$n$,below = of m] (n) {Task b: yyyyy};
        \node[setC,label=right:$p$,below = of n] (p) {Task c: zzzzz};
        \node[above=of m,anchor=south] {$C$};

       % the arrows
       \draw[->,shorten >= 3pt] (a) -- node[label=above:$f$] {} (x);
       \draw[->,shorten >= 3pt] (b) -- node[label=above:$f$] {} (x);
       \draw[->] (c) -- node[label=above:$g$] {} (y);
       \draw[->,shorten <= 3pt] (x) -- node[label=above:$h$] {} (m);
       \draw[->] (n) -- node[label=above:$u$] {} (y);

       % the boxes around the sets
      \begin{pgfonlayer}{background}
      \node[fit= (a)  (c) ] {};
      \node[fit= (x) (z) ] {};
      \node[fit= (m) (p)] {};
      \end{pgfonlayer}
      \end{tikzpicture}
      \end{document}

产生了以下结果

enter image description here

虽然我想要的输出应该是这样的

enter image description here

但困难在于我不知道如何关闭集合 A 上的块环境并扩展集合 C 上的块的宽度以适应文本的额外长度。(# remove drawonevery fit\.style将关闭所有集合上的块环境,有没有办法仅在块环境中包含集合 B 和 C?)

我参考过这个邮政overleaf但他的代码的复杂性超出了我的理解范围。

有人愿意看看并告诉我如何处理现有代码以产生所需的输出吗?

谢谢。

答案1

复制第二幅图像的代码如下:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                chains,
                fit,
                positioning,
                quotes,
                }
\begin{document}
    \begin{tikzpicture}[
node distance = 4mm and 24mm,
  start chain = going below,
   dot/.style = {circle, fill=black, node contents={}},
  setC/.style = {rectangle, inner sep=2pt, text depth=1ex,
                 anchor=west, on chain},
   FIT/.style = {rectangle, draw, %rounded corners=5pt,
                 inner xsep=8pt, inner ysep=4pt, font=\sffamily,
                 fit=#1},
   Arr/.style = {-Triangle, shorten >=3pt},
every edge quotes/.append style = {font=\footnotesize, sloped, pos=0.6}
                        ]
% from right to left
% set C
\node[setC] (m) {Task a: xxxxxx};
\node[setC] (n) {Task b: yyyyy};
\node[setC] (p) {Task c: zzzzz};
\node[FIT=(m) (p), label=$C$]   {};
% set B
\node[setC, left=of m]  (x) {Person x};
\node[setC]             (y) {Person y};
\node[setC]             (z) {Person z};
\node[FIT=(x) (z), label=$B$]   {};
% set A
\node [setC,left=of y]  (a) {Initiator};
% the arrows
\draw[Arr]   (a.east) edge["$f$"] (x.west)
             (a.east) edge["$g$"] (y.west)
             (x.east) edge["$h$"] (p.west)
             (n.west)  to ["$u$"] (y.east);
\end{tikzpicture}
\end{document}

enter image description here

此图像与您使用代码生成的图像不同(如果它能起作用)。

因为从你的问题来看(对我来说),你想要什么,我的主要努力是使你的代码更简单,更一致和更简短。为此,我使用tikzchainsquotes

如果您喜欢在“C”中使用红色填充和标签的节点,这很容易添加。对于代码中的节点“a”,我准备了样式dot但没有使用它。

答案2

我不确定这是否是一个答案,但它至少是一个可以无错误编译且看起来比期望的输出多一点的代码。

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{arrows,positioning,shapes,fit,calc}
    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}
    \begin{document}
    \begin{tikzpicture}[
    every node/.style={on grid},
    setA/.style={fill=black, circle, inner sep=1.2pt},
    setC/.style={fill=red, rectangle,inner sep=2pt},
    every fit/.style={draw, fill=white,rectangle,rounded corners=5pt, text width=80pt},>=latex]

     % set A
    \node [setA, label=left:a] (a) {};
    \node[above=of a,anchor=south] {};

    % set B
    \node[inner sep=0pt,right=5cm of a] (y) {Person y};
    \node[above = of y] (x) {Person x};
    \node[inner sep=0pt,below = of y] (z) {Person z};
    \node[above=of x,anchor=south] {$B$};

    % set C
    \node[setC,label={[name=ml] right:$m$},right = 6cm of x] (m) {Task a: xxxxxx};
    \node[setC,label={[name=nl] right:$n$},below = of m] (n) {Task b: yyyyy};
    \node[setC,label={[name=pl] right:$p$},below = of n] (p) {Task c: zzzzz};
    \node[above=of m,anchor=south] {$C$};

   % the arrows
   \draw[->,shorten >= 3pt] (a) -- node[label=above:$f$] {} (x);
   \draw[->,shorten >= 3pt] (a) -- node[label=below:$f$] {} (y);

% \draw[->] (c) -- 节点[标签=上面:$g$] {} (y); \draw[->,shorten <= 3pt] (x) -- 节点[标签=上面:$h$] {} (m); \draw[->] (n) -- 节点[标签=上面:$u$] {} (y);

   % the boxes around the sets
  \begin{pgfonlayer}{background}

% \node[fit= (a) (c) ] {}; \node[fit= (x) (z) ] {}; \node[fit= (m) (ml) (p)] {}; \end{pgfonlayer} \end{tikzpicture} \end{document}

enter image description here

相关内容