展开第二个参数/反参数

展开第二个参数/反参数

我目前正在研究一种生成 B+ 树的自动化方法。

因此,我引入了一个新的宏来生成节点矩阵的内容,该矩阵还应自动生成标签。此宏调用另一个宏来执行此操作。我\expandafter之前添加了一个来扩展\x

但我无法让它与计数器一起正常工作\ga。计数器的值似乎在每种情况下都是 4。因此引用可以l1-n1-p4正常工作,但所有其他引用(即l1-n1-p2)都会导致错误。

这是我目前的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{etoolbox}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{matrix, fit, calc}

\begin{document}

\newcount\ga
\ga = 1%

\newcommand{\drawkeymatrixnode}[4] {
    |(l#4-n#3-p#2) [fill=gray!50]| \vphantom{1} \&
    \ifx&#1&
        % empty argument #1
        |(l#4-n#3-k#2) [text width = 0.5cm, align = center]| \vphantom{1} \&
    \else
        % #1 not empty
        |(l#4-n#3-k#2) [text width = 0.5cm, align = center]| #1 \&
    \fi
}

\newcommand{\drawpointermatrixnode}[3] {
    |(l#3-n#2-p#1) [fill=gray!50]| \vphantom{1} \&
}

\newcommand{\btreematrixnode}[3]{%
    % #1 ... the level of the node
    % #2 ... number of node of level #1
    % #3 ... values as comma separated list

    \ga = 1%
    \let\mymatrixnodecontent\empty
    \foreach \x in #3 {%
        \expandafter\gappto\expandafter\mymatrixnodecontent\expandafter{\expandafter\drawkeymatrixnode\expandafter{\x}{\the\ga}{#2}{#1}}%
        \global\advance\ga by1
    }
    \expandafter\gappto\expandafter\mymatrixnodecontent\expandafter{\expandafter\drawpointermatrixnode\expandafter{\the\ga}{#2}{#1}}%
    \gappto\mymatrixnodecontent{\\}%
}

\begin{center}
\begin{tikzpicture}[
        scale = 1.0,
        ->,
        >=stealth',
        level 1/.style = {
            sibling distance = 7cm,
            level distance = 1.5cm
        },
        level 2/.style = {
            sibling distance = 3.5cm,
            level distance = 1.5cm
        },
        edge from parent/.style = {}, % no connecting arrows
        bnode/.style = {
            matrix,
            matrix of nodes,
            ampersand replacement=\&,
            nodes = {draw, rectangle, minimum height = 0.5cm}
        }
    ] 

    \btreematrixnode{1}{1}{{1, 2, 3}}
    \let\cLOne\mymatrixnodecontent
    \btreematrixnode{2}{1}{{4, 5, 6}}
    \let\cLTwoNOne\mymatrixnodecontent
    \btreematrixnode{2}{2}{{7, 8, 9}}
    \let\cLTwoNTwo\mymatrixnodecontent

    \node[bnode] (root) {\cLOne}
    child {
        node[bnode] {\cLTwoNOne}
    }
    child {
        node[bnode] {\cLTwoNTwo}
    };

    % should be \draw (l1-n1-p2) -- (l2-n1-k2);
    \draw (l1-n1-p4) -- (l2-n1-k4);
\end{tikzpicture}
\end{center}
\end{document}

因此,我想分别生成l<levelnr>-n<nodenr>-k<keynr>和形式的标签l<levelnr>-n<nodenr>-p<pointernr>。因此,对于第一级节点 1 中包含 3 个键和 4 个指针的节点,我想获取 、l1-n1-p1l1-n1-k1l1-n1-p2l1-n1-k2l1-n1-p3l1-n1-k3l1-n1-p4似乎我只能l1-n1-p4生成,因为我收到no shape named l1-n1-p2 is known错误。

我已经阅读了一些关于“expandafter multiple parameters”的搜索线索,但我现在还不知道如何让它工作。

答案1

你必须扩展这个\the\ga论点。例如:

...
\newcommand{\btreematrixnode}[3]{%
    % #1 ... the level of the node
    % #2 ... number of node of level #1
    % #3 ... values as comma separated list
    \ga = 1%
    \let\mymatrixnodecontent\empty
    \foreach \x in #3 {%
        \edef\tmp{{\x}{\the\ga}}%
        \expandafter\gappto\expandafter\mymatrixnodecontent\expandafter
            {\expandafter\drawkeymatrixnode\tmp{#2}{#1}}%
        \global\advance\ga by1
    }
    \expandafter\gappto\expandafter\mymatrixnodecontent\expandafter
        {\expandafter\drawpointermatrixnode\expandafter{\the\ga}{#2}{#1}}%
    \gappto\mymatrixnodecontent{\\}%
}
...
    % should be \draw (l1-n1-p2) -- (l2-n1-k2);
    %    \draw (l1-n1-p4) -- (l2-n1-k4);
\draw (l1-n1-p2) -- (l2-n1-k2);
...

相关内容