如何为“节点键矩阵”定义 TikZ 样式?

如何为“节点键矩阵”定义 TikZ 样式?

我想定义一种风格matrix of node keys

\input tikz
\usetikzlibrary{matrix}
\tikzpicture
\matrix [matrix of node keys]
  {red,node contents=a & blue,node contents=b \\};
\endtikzpicture
\bye

编译结果与

\input tikz
\usetikzlibrary{matrix}
\tikzpicture
\matrix [matrix of nodes]
  {|[red]|a & |[blue]|b \\};
\endtikzpicture
\bye

我现在正在研究tikzlibrarymatrix.code.tex:在我看来,大部分代码都用于解析可选语法。由于我不需要这些,所以可能有更聪明的方法来实现我想要的。事实上,手册中实现库的基本版本的示例matrix只是

\begin{tikzpicture}
  [matrix of nodes/.style={
    execute at begin cell=\node\bgroup,
    execute at end cell=\egroup;%
  }]
  \matrix [matrix of nodes]
  {
    8 & 1 & 6 \\
    3 & 5 & 7 \\
    4 & 9 & 2 \\
  };
\end{tikzpicture}

一个非常幼稚的尝试可能会

\input tikz
\let\bsquare[
\let\esquare]
\tikzpicture[matrix of node keys/.style={
  execute at begin cell=\node\bsquare,
  execute at end cell=\esquare;,
}]
\matrix [matrix of node keys]
  {red,node contents=a & blue,node contents=b \\};
\endtikzpicture
\bye

但它失败了,因为的使用\tikz@fig@scan@options与其定义不匹配(它期望形式[#1])。

最安全和最合理的定义方法是什么matrix of node keys

答案1

这对你有用吗?

\makeatletter
\tikzset{matrix of node keys/.code={
    \pgfkeysalso{
        matrix,%
        cells={anchor=base},%
        execute at begin cell=\matrix@ofnodekeys@start@cell,
        execute at empty cell=\tikz@lib@matrix@empty@cell
    }
}}

\bgroup
\catcode`&=\active
\gdef\monk@nil{\pgfutil@gobble{unique expansion text}}
    \gdef\matrix@ofnodekeys@slsl{
    \pgfutil@ifnextchar\monk@nil
        {\expandafter\pgfmatrixendrow\pgfutil@gobble}
        {\pgfmatrixendrow}
}
\gdef\matrix@ofnodekeys@start@cell{
    \let\\\matrix@ofnodekeys@slsl
    \pgfutil@ifnextchar\relax{}{\matrix@ofnodekeys@start@cell@}
}
\gdef\matrix@ofnodekeys@start@cell@#1\\{
    %\def\temp{#1}\show\temp
    \pgfutil@ifnextchar\monk@nil
        {\matrix@ofnodekeys@start@cell@@ #1\\}
        {\matrix@ofnodekeys@start@cell@@ #1&\monk@nil\\\monk@nil}
}
\gdef\matrix@ofnodekeys@start@cell@@#1&{
  \node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,#1]{};
  \pgfutil@ifnextchar\monk@nil{\pgfutil@gobble}{&}
}
\egroup
\makeatother

这是一个完全普通的 TeX 示例(使用pgfutil宏不是绝对必要的):

\input tikz

\catcode`@=11 %\makeatletter

\tikzset{matrix of node keys/.code={
    \pgfkeysalso{
        matrix,%
        cells={anchor=base},%
        execute at begin cell=\matrix@ofnodekeys@start@cell,
        execute at empty cell=\tikz@lib@matrix@empty@cell
    }
}}

\bgroup
\catcode`&=\active
\gdef\monk@nil{\pgfutil@gobble{unique expansion text}}
\gdef\matrix@ofnodekeys@slsl{
    \pgfutil@ifnextchar\monk@nil
        {\expandafter\pgfmatrixendrow\pgfutil@gobble}
        {\pgfmatrixendrow}
}
\gdef\matrix@ofnodekeys@start@cell{
    \let\\\matrix@ofnodekeys@slsl
    \pgfutil@ifnextchar\relax{}{\matrix@ofnodekeys@start@cell@}
}
\gdef\matrix@ofnodekeys@start@cell@#1\\{
    \pgfutil@ifnextchar\monk@nil
        {\matrix@ofnodekeys@start@cell@@ #1\\}
        {\matrix@ofnodekeys@start@cell@@ #1&\monk@nil\\\monk@nil}
}
\gdef\matrix@ofnodekeys@start@cell@@#1&{
  \node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,#1]{};
  \pgfutil@ifnextchar\monk@nil{\pgfutil@gobble}{&}
}
\egroup  
\catcode`@=12 %\makeatother

\tikzpicture [
  every odd row/.style={red},
  every even column/.style={font=\bf}
]
\matrix [matrix of node keys] {
  node contents=a & node contents=b & fill=orange,node contents=c \\
  blue,node contents=d & rectangle,draw,node contents=e & magenta,node contents=f \\
  rotate=60,node contents=g & node contents=h & node contents=i \\
};
\endtikzpicture

\bye

输出:

幼儿保育矩阵

相关内容