以程式設計方式繪製帶有箭頭連接的表格

以程式設計方式繪製帶有箭頭連接的表格

我需要產生一堆如下圖的數字。

我確信在 LaTeX 中可以實現這一點。我不確定是否可以創建一個函數,在給定表和連接的條目的情況下,以程式設計方式產生數字。

我嘗試過以下程式碼:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    box/.style={rectangle, draw=black, minimum height=1cm, minimum width=2cm, align=left},
    arrow/.style={-latex}
]
  % Boxes
  \node[box] (box1) {XXXXX\\xxxxx1\\xxxxx2\\xxxxx3\\xxxxx4};
  \node[box, right=2cm of box1] (box2) {YYYYY\\yyyyy1\\yyyyy2\\yyyyy3\\yyyyy4};
  \node[box, right=2cm of box2] (box3) {ZZZZZ\\zzzzz1\\zzzzz2\\zzzzz3\\zzzzz4\\zzzzz5};
  
  % Arrows
  \draw[arrow] (box2) -- (box1);
  \draw[arrow] (box3) -- (box2);

\end{tikzpicture}
\end{document}

我想像想要的函數接收這個輸入:

{{
table XXXXX,
xxx1
xxx2,
xxx3,
},
{
table YYYYY,
yyy1
yyy2,
yyy3,
},
{
connections:
XXXXX:1 -> YYYYY:2
}}

表的數量以及表條目的數量可以變化。在上面的範例中,表格的第 1 行XXXXX連接到表格的第 2 行YYYYY,箭頭方向由 給出->

先致謝!

在此輸入影像描述

答案1

這是第一個使用{NiceTabular}的草圖nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{arrows.meta}

\ExplSyntaxOn

% The following counter will count the occurrences of the command \Draw
\int_new:N \g__PaulS_int

\cs_new_protected:Npn \__PaulS_arrow:n #1 { \__PaulS_arrow_aux:w #1 \q_stop }

\cs_new_protected:Npn \__PaulS_arrow_aux:w #1 - #2 -> #3 - #4 \q_stop
  { 
    \int_compare:nNnTF { #1 } < { #3 } 
      { \draw [-LaTeX] (#1-#2.5-|#1-2) -- (#3-#4.5-|#3-1) ; }
      { \draw [-LaTeX] (#1-#2.5-|#1-1) -- (#3-#4.5-|#3-2) ; }
  }

\NewDocumentCommand { \Draw } { m m } 
  {
    \group_begin:
    \int_gincr:N \g__PaulS_int
    % the following counter will count the tables
    \int_zero_new:N \l__PaulS_int
    \clist_map_inline:nn { #1 }
      {
        \int_incr:N \l__PaulS_int
        \begin { NiceTabular } 
          [ 
            t , 
            name = PaulS - \int_use:N \g__PaulS_int - \int_use:N \l__PaulS_int
          ] 
          { c }
        \bfseries 
        \clist_use:nn { ##1 } { \\ }
        % we draw the rules in the current table 
        \CodeAfter
          \begin { tikzpicture }
            \draw (1-|1) rectangle (last-|last) ; 
            \draw (2-|1) -- (2-|last) ; 
          \end { tikzpicture }
        \end { NiceTabular }
        \hspace { 1cm }
      }
    % now, we draw the arrows
      \begin{tikzpicture} 
        [ 
          remember~picture, 
          overlay , 
          name~prefix = PaulS - \int_use:N \g__PaulS_int -
        ]
      \clist_map_function:nN { #2 } \__PaulS_arrow:n 
    \end{tikzpicture}
    \group_end:
  }

% the following lines are not necesssary with recent versions of LaTeX
\cs_set:Nn \int_if_zero:nT { \int_compare:nNnT { #1 } = 0 { #2 } }
\cs_set:Nn \int_if_zero:nTF { \int_compare:nNnT { #1 } = 0 { #2 } { #3 } }

\ExplSyntaxOff

\begin{document}

\NiceMatrixOptions{cell-space-limits=3pt}

\Draw 
  {
    { table XXXXX, xxx1 xxx2, xxx3 } ,
    { table YYYYY, yyy1 yyy2, yyy3 } , 
    { table ZZZ, zz1 zzz2, zzz4 , zzz5 } 
  }
  { 1-1->2-2 , 2-3->1-3 , 2-1->3-3 }

\end{document}

您需要多次編譯(因為使用帶有 key 的 TikZ 節點remember picture)。

上述程式碼的輸出

相關內容