循环中节点矩阵的附加样式不起作用

循环中节点矩阵的附加样式不起作用

关注此问题以编程方式定义矩阵节点,我能够更精确地说明我想要实现的目标。抱歉,我在第一个问题中回答得太笼统了。这是新的 MWE。阅读第一个问题 (这里) 是绝对必要的。

我的最终目标是在矩阵中定义节点定位,节点之间的距离并不相同。

我能够使用方法 1 定义具有附加样式的每个节点的尺寸,但希望使用方法 2 或 3(或类似方法)来实现。出于某种原因,循环无法正常用于附加样式:也许我需要在某处缓冲信息?

\documentclass[border=2mm,tikz]{standalone}

\usetikzlibrary{matrix}
\usepackage{xinttools}

\begin{document}
\tikzset{dimensionsBigBox/.style={
        minimum height=3cm,
        rectangle, 
        fill=none, draw=blue,
        text depth=0.5em, text height=1em,
}}

\tikzset{dimensionsSmallBox/.style={
        minimum height=1cm,
        rectangle, 
        fill=none, draw=red,
        text depth=0.5em, text height=1em,
}}

\tikzset{
mygridmatrix/.style={
    matrix of nodes, 
    row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
    nodes={draw}, 
    nodes in empty cells,
},
}


\iftrue
    % Method 1 : Block of hardcoded definition of the nodes
    \tikzset{mygridmatrix/.append style={row 1 column 1/.style={nodes={dimensionsSmallBox}}}}
    \tikzset{mygridmatrix/.append style={row 1 column 2/.style={nodes={dimensionsSmallBox}}}}
    \tikzset{mygridmatrix/.append style={row 1 column 3/.style={nodes={dimensionsSmallBox}}}}
    \tikzset{mygridmatrix/.append style={row 2 column 1/.style={nodes={dimensionsBigBox}}}}
    \tikzset{mygridmatrix/.append style={row 2 column 2/.style={nodes={dimensionsBigBox}}}}
    \tikzset{mygridmatrix/.append style={row 2 column 3/.style={nodes={dimensionsBigBox}}}}
    \tikzset{mygridmatrix/.append style={row 3 column 1/.style={nodes={dimensionsSmallBox}}}}
    \tikzset{mygridmatrix/.append style={row 3 column 2/.style={nodes={dimensionsSmallBox}}}}
    \tikzset{mygridmatrix/.append style={row 3 column 3/.style={nodes={dimensionsSmallBox}}}}
\fi

\iffalse
    % Method 2 : Append style and loop with xintFor ==> only apply the last style of the loop iteration
    %\xintForpair #1#2 in  {(1,dimensionsBigBox), (2,dimensionsSmallBox), (3,dimensionsBigBox),(4,dimensionsSmallBox), (5,dimensionsSmallBox), (6,dimensionsSmallBox), (7,dimensionsBigBox), (8,dimensionsBigBox), (9,dimensionsBigBox), (2,dimensionsSmallBox)} \do {
    \xintForpair #1#2 in  {(1,dimensionsBigBox), (2,dimensionsSmallBox), (3,dimensionsBigBox),(4,dimensionsSmallBox), (5,dimensionsSmallBox), (6,dimensionsSmallBox), (7,dimensionsBigBox), (8,dimensionsBigBox), (9,dimensionsBigBox)} \do {
        %Set style #2 for node  #1 \\
        \pgfmathsetmacro\row{int(div(#1-1,3)+1)}
        \pgfmathsetmacro\col{int(mod(#1-1,3)+1)}
        \tikzset{mygridmatrix/.append style={row \row\space column \col/.style={nodes={#2}}}}
        }
\fi



\iffalse
    % Method 3 : Append style and loop with foreach ==> doesn't apply any style
    \foreach \x/\y  in  {1/dimensionsBigBox, 2/dimensionsBigBox, 3/dimensionsBigBox, 4/dimensionsSmallBox, 5/dimensionsSmallBox, 6/dimensionsSmallBox, 7/dimensionsBigBox, 8/dimensionsBigBox, 9/dimensionsBigBox}{
        %Set style \y for node  \x \\
        \pgfmathsetmacro\row{int(div(\x-1,3)+1)}
        \pgfmathsetmacro\col{int(mod(\x-1,3)+1)}
        \tikzset{mygridmatrix/.append style={row \row\space column \col/.style={nodes={\y}}}}
    }
\fi

\begin{tikzpicture}
    \matrix (m) [mygridmatrix]
    {
        1&2&3\\
        4&5&6\\
        7&8&9\\
    };
\end{tikzpicture}


\end{document}

答案1

对于方法 2,正如 @jfbu 在其评论中所建议的,您需要强制扩展row \row\space column \col,为此您只需添加/.expanded,如下所示:

% Method 2 : Append style and loop with xintFor
\xintForpair #1#2 in {(1,dimensionsBigBox), (2,dimensionsSmallBox), (3,dimensionsBigBox),(4,dimensionsSmallBox), (5,dimensionsSmallBox), (6,dimensionsSmallBox), (7,dimensionsBigBox), (8,dimensionsBigBox), (9,dimensionsBigBox)} \do {
  %Set style #2 for node  #1 \\
  \pgfmathsetmacro\row{int(div(#1-1,3)+1)}
  \pgfmathsetmacro\col{int(mod(#1-1,3)+1)}
  \tikzset{mygridmatrix/.append style/.expanded={row \row\space column \col/.style={nodes={#2}}}}
}

要像方法 3 中那样使用foreach循环,您可以将样式保存在临时变量中并将其设置在循环之外,如下所示:

% Method 3 : Append style and loop with foreach
\xdef\temp{}
\foreach[count=\x from 0] \y in {dimensionsBigBox,dimensionsBigBox,dimensionsBigBox,dimensionsSmallBox,dimensionsSmallBox,dimensionsSmallBox,dimensionsBigBox,dimensionsBigBox,dimensionsBigBox}{
  %Set style \y for node  \x 
  \pgfmathsetmacro\row{int(div(\x,3)+1)}
  \pgfmathsetmacro\col{int(mod(\x,3)+1)}
  \xdef\temp{\temp,row \row\space column \col/.style={\y}}
}
\tikzset{mygridmatrix/.append style/.expanded={\temp}}

完整代码如下:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{matrix}
\usepackage{xinttools}

\begin{document}
\tikzset{
  dimensionsBigBox/.style={
    minimum height=3cm,
    rectangle,
    fill=none, draw=blue,
    text depth=0.5em, text height=1em,
  },
  dimensionsSmallBox/.style={
    minimum height=1cm,
    rectangle,
    fill=none, draw=red,
    text depth=0.5em, text height=1em,
  },
  mygridmatrix/.style={
    matrix of nodes,
    row sep=-\pgflinewidth, column sep=-\pgflinewidth,
    nodes={draw},
    nodes in empty cells,
  }
}


% Method 1 : Block of hardcoded definition of the nodes
\tikzset{mygridmatrix/.append style={row 1 column 1/.style={nodes={dimensionsBigBox}}}}
\tikzset{mygridmatrix/.append style={row 1 column 2/.style={nodes={dimensionsBigBox}}}}
\tikzset{mygridmatrix/.append style={row 1 column 3/.style={nodes={dimensionsBigBox}}}}
\tikzset{mygridmatrix/.append style={row 2 column 1/.style={nodes={dimensionsSmallBox}}}}
\tikzset{mygridmatrix/.append style={row 2 column 2/.style={nodes={dimensionsSmallBox}}}}
\tikzset{mygridmatrix/.append style={row 2 column 3/.style={nodes={dimensionsSmallBox}}}}
\tikzset{mygridmatrix/.append style={row 3 column 1/.style={nodes={dimensionsBigBox}}}}
\tikzset{mygridmatrix/.append style={row 3 column 2/.style={nodes={dimensionsBigBox}}}}
\tikzset{mygridmatrix/.append style={row 3 column 3/.style={nodes={dimensionsBigBox}}}}

\tikz\matrix (m) [mygridmatrix,label=above:Method 1 (hardcoded)] {1&2&3\\4&5&6\\7&8&9\\};


% Method 2 : Append style and loop with xintFor
\xintForpair #1#2 in {(1,dimensionsBigBox), (2,dimensionsBigBox), (3,dimensionsBigBox),(4,dimensionsSmallBox), (5,dimensionsSmallBox), (6,dimensionsSmallBox), (7,dimensionsBigBox), (8,dimensionsBigBox), (9,dimensionsBigBox)} \do {
    %Set style #2 for node  #1
  \pgfmathsetmacro\row{int(div(#1-1,3)+1)}
  \pgfmathsetmacro\col{int(mod(#1-1,3)+1)}
  \tikzset{mygridmatrix/.append style/.expanded={row \row\space column \col/.style={nodes={#2}}}}
}

\tikz\matrix (m) [mygridmatrix,label=above:Method 2 (using xintFor)] {1&2&3\\4&5&6\\7&8&9\\};

% Method 3 : Append style and loop with foreach
\xdef\temp{}
\foreach[count=\x from 0] \y in {dimensionsBigBox,dimensionsBigBox,dimensionsBigBox,dimensionsSmallBox,dimensionsSmallBox,dimensionsSmallBox,dimensionsBigBox,dimensionsBigBox,dimensionsBigBox}{
  %Set style \y for node  \x
  \pgfmathsetmacro\row{int(div(\x,3)+1)}
  \pgfmathsetmacro\col{int(mod(\x,3)+1)}
  \xdef\temp{\temp,row \row\space column \col/.style={\y}}
}
\tikzset{mygridmatrix/.append style/.expanded={\temp}}

\tikz\matrix (m) [mygridmatrix,label=above:Method 3 (using foreach)] {1&2&3\\4&5&6\\7&8&9\\};

\end{document}

在此处输入图片描述

答案2

这似乎是一场漫长的对话。;-) 这是另一次尝试。您只需使用\xintForpair新列表运行,就会得到一个不同的矩阵。

\documentclass[border=2mm,tikz]{standalone}

\usetikzlibrary{matrix}
\usepackage{xinttools}

\begin{document}
\tikzset{dimensionsBigBox/.style={
        minimum height=3cm,
        rectangle, 
        fill=none, draw=blue,
        text depth=0.5em, text height=1em,
}}

\tikzset{dimensionsSmallBox/.style={
        minimum height=1cm,
        rectangle, 
        fill=none, draw=red,
        text depth=0.5em, text height=1em,
}}

\tikzset{
mygridmatrix/.style={
    matrix of nodes, 
    row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
    nodes={draw}, 
    nodes in empty cells,
},
}
   \xintForpair #1#2 in  {(1,dimensionsBigBox), (2,dimensionsSmallBox), (3,dimensionsBigBox),(4,dimensionsSmallBox), (5,dimensionsSmallBox), (6,dimensionsSmallBox), (7,dimensionsBigBox), (8,dimensionsBigBox), (9,dimensionsBigBox)} \do {
        %Set style #2 for node  #1 \\
        \pgfmathsetmacro\row{int(div(#1-1,3)+1)}
        \pgfmathsetmacro\col{int(mod(#1-1,3)+1)}
        \tikzset{mygridmatrix-\row-\col/.style={#2}}
        }

\begin{tikzpicture}[every node/.append style={minimum width=6mm}]
\matrix[matrix of nodes] (m) {%
|[mygridmatrix-1-1,alias=topleft]|& |[mygridmatrix-1-2]| &|[mygridmatrix-1-3]|\\ 
|[mygridmatrix-2-1]|& |[mygridmatrix-2-2]| &|[mygridmatrix-2-3]|\\ 
|[mygridmatrix-3-3]|& |[mygridmatrix-3-2]| &|[mygridmatrix-3-3,alias=bottomright]|\\ };
\draw[blue,-latex] (topleft.center) -- (bottomright.center);
\end{tikzpicture}

\end{document}

相关内容