使用 Tikz 和 Matrix,foreach 循环中生成的节点不接受“风锚”(北、南等)

使用 Tikz 和 Matrix,foreach 循环中生成的节点不接受“风锚”(北、南等)

这是一个具有 5x3 个节点的小矩阵。我的真实矩阵要大得多,我更喜欢为每个单元格指定一个数字(从 1 到 15 - 是的,我知道 0 到 14 会更容易)。我已经用 foreach 对单元格进行了编号以说明编号。

我想要的是给一些水平分隔线着色,给出左侧单元格的编号和右侧单元格的编号。当矩阵坐标用静态数字给出时,它可以完美地工作(例如,7 和 9 之间的红线)。如果我尝试在 foreach 循环中动态生成单元格编号(例如,1 和 3 之间的蓝线),它不起作用。显然,偏移量(北、南、东……)被忽略了。你可以想象,在向这里寻求帮助之前,我做了很多变体。

您知道我的错误出在哪里吗?

提前致谢。

在此处输入图片描述

这是我的 MWE

\documentclass[a4paper, 11pt]{report}
% --- DOCUMENT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{caption}

\begin{document}

\begin{figure}[!h]\centering
\usetikzlibrary{matrix,calc}

\begin{tikzpicture}[ampersand replacement=\&]
  \tikzset{
    table nodes/.style={rectangle, draw, align=center, minimum height=5mm,
      text depth=0.5ex,text height=1ex,inner xsep=0pt,outer sep=0pt
      },      
    table/.style={matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
      nodes={table nodes},
      execute at empty cell={\node[draw=none]{}; }
      }
    }
  \matrix at (0,0) [table,text width=5mm,name=MX] {%
    \ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\};
  \foreach \i [evaluate={\r=int((\i-1)/3+1); \c={int(mod(\i-1,3)+1)} }] in {1,...,15} {
    \node at ($(MX-\r-\c)$) {\footnotesize\i};
    }
  \foreach \b/\e [evaluate={\r=int((\b-1)/3+1); \c={int(mod(\b-1,3)+1)}; \d={int(mod(\e-1,3)+1)} }] in {1/3} { %
    \draw[red,semithick] ($(MX-\r-\c)$) circle(2pt);  <--- THIS WORKS
    \draw[blue,semithick] ($(MX-\r-\c)$.south) -- ($(MX-\r-\d)$.south);  <--- THIS DOESN'T WORK
    %\coordinate (N) at  ($(MX-\r-\c)$);
    %\draw[blue,semithick] (N.south) circle(2pt);  <--- THIS DOESN'T WORK
    %\draw[blue,semithick] (N.south) circle(2pt);  <--- THIS DOESN'T WORK
    }
  \draw[red,semithick] (MX-3-1.south west) -- (MX-3-3.south east); % <--- THIS WORKS
  % useasboundingbox
  \useasboundingbox[blue] ($(MX-1-1.north west)+(135:10pt)$) rectangle ($(MX-5-3.south east)+(315:10pt)$);
\end{tikzpicture}
\caption{Matrix example}
\end{figure}
\end{document}

答案1

和在括号外.south west.south east而不是在括号内

在此处输入图片描述

\documentclass[a4paper, 11pt]{report}
% --- DOCUMENT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{caption}

\begin{document}
    
    \begin{figure}[!h]\centering
        \usetikzlibrary{matrix,calc}
        
        \begin{tikzpicture}[ampersand replacement=\&]
        \tikzset{
            table nodes/.style={rectangle, draw, align=center, minimum height=5mm,
                text depth=0.5ex,text height=1ex,inner xsep=0pt,outer sep=0pt
            },      
            table/.style={matrix of nodes, row sep=-\pgflinewidth, column sep=- 
        \pgflinewidth,
                nodes={table nodes},
                execute at empty cell={\node[draw=none]{}; }
            }
        }
        \matrix at (0,0) [table,text width=5mm,name=MX] {%
            \ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\\ \&\ \&\ \\};
        \foreach \i [evaluate={\r=int((\i-1)/3+1); 
            \c={int(mod(\i-1,3)+1)} }] in {1,...,15} {
            \node at ($(MX-\r-\c)$) {\footnotesize\i};
        }
        \foreach \b/\e [evaluate={\r=int((\b-1)/3+1); 
            \c={int(mod(\b-1,3)+1)}; 
            \d={int(mod(\e-1,3)+1)} }] in {1/3} { %
            \draw[red,line width=1pt] (MX-\r-\c) circle(2pt); % <--- THIS WORKS
            \draw[blue,line width=2pt] (MX-\r-\c.south west) -- (MX-\r-\d.south east); 
           % <--- THIS ALSO WORKS NOW  the .south west was outside the brackets 
            %instead of inside
            %\coordinate (N) at  ($(MX-\r-\c)$);
            %\draw[blue,semithick] (N.south) circle(2pt);  <--- THIS DOESN'T WORK
            %\draw[blue,semithick] (N.south) circle(2pt);  <--- THIS DOESN'T WORK
        }
        \draw[red,line width=2pt] (MX-3-1.south west) -- (MX-3-3.south east); % <--- 
        %THIS WORKS
        % useasboundingbox
        \useasboundingbox[blue, line width=2pt] ($(MX-1-1.north west)+(135:10pt)$) 
         rectangle ($(MX-5-3.south east)+(315:10pt)$);
        \end{tikzpicture}
        \caption{Matrix example}
    \end{figure}
\end{document}

相关内容