使用 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

and.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}

相關內容