0

This is a small matrix with 5x3 nodes. My true matrix is much bigger, and I preferred to give a number to each cell (from 1 to 15 - yes, I know 0 to 14 would be easier). I already numbered the cells with a foreach to illustrate the numbering.

What I want is to color some horizontal separation lines, giving the number of the cell at the left and the number of the cell at the right. It works perfectly when the matrix coordinates are given with static numbers (for instance, the red line between 7 and 9). It doesn't work if I try to dynamically generate the cell numbers in a foreach loop (for instance the blue line between 1 and 3). Apparently, the offset (north, south, east...) is ignored. As you can imagine, I made a lot of variants before asking for help here.

Would you have any idea where would be my mistake?

Thanks in advance.

enter image description here

This is my 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}
2
  • 2
    Why are you using calc syntax instead of \draw[blue,semithick] (MX-\r-\c.south) -- (MX-\r-\d.south);? Commented Aug 28, 2020 at 15:57
  • Because I (quite stupidly) reused a template coming from another Tikz code and because I didn't imagine that calc could be disruptive :-( Lesson learned... Thx ! Commented Aug 28, 2020 at 18:19

1 Answer 1

2

The .south west and .south east was outside the brackets instead of inside

enter image description here

\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}
1
  • 1
    Many thanks. Sometimes, the simplest solution is not in sight anymore. Commented Aug 28, 2020 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .