2

I made a matrix of math nodes. I am able to draw a rectangle around desired nodes. But every node has content of different size, which deforms the shape of the rectangles.

I tried using x and y shift, and played with sep (row, column, inner) also, but could not get it done.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, decorations.pathreplacing}

\begin{document}

\section{Introduction}
$\widetilde{G}{\;=\;}$
   \begin{tikzpicture}[baseline=0ex]
   \matrix (G) [
       matrix of nodes, nodes in empty cells,
       left delimiter={[},right delimiter={]},
       every node/.style={font=\footnotesize}, inner sep=2.5pt,
       row sep=3.0pt,
       nodes={%
         execute at begin node=$,%
         execute at end node=$%
       }%
     ]
      {
1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0\\
0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1\\
0&\alpha^2&\alpha&\alpha^2&\alpha^2&\alpha^2&1&1&0&\alpha&1&0&\alpha^2&0&\alpha&1&\alpha^2&1&0&1&\alpha&0&1&\alpha&\alpha^2&\alpha&\alpha&\alpha&1&\alpha^2\\
\alpha^2&\alpha^2&\alpha^2&1&\alpha^2&0&1&0&\alpha&\alpha&0&1&0&\alpha^2&1&\alpha^2&1&\alpha&1&1&0&\alpha&\alpha&\alpha^2&\alpha&1&\alpha&0&\alpha^2&\alpha\\
\alpha^2&\alpha&0&\alpha&1&\alpha&\alpha^2&1&1&\alpha^2&1&0&\alpha&0&1&1&0&1&\alpha&1&\alpha^2&0&0&\alpha^2&\alpha^2&\alpha^2&\alpha&\alpha^2&\alpha&\alpha\\
\alpha&1&\alpha&\alpha&\alpha&\alpha^2&1&\alpha&\alpha^2&\alpha&0&1&0&\alpha&1&0&1&1&1&\alpha^2&0&\alpha^2&\alpha^2&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&0\\
\alpha&\alpha^2&\alpha&\alpha&1&\alpha^2&1&\alpha^2&\alpha&\alpha^2&1&0&1&0&0&\alpha&\alpha&\alpha&\alpha&\alpha^2&1&0&\alpha&\alpha&0&\alpha&1&\alpha^2&0&\alpha\\
\alpha^2&1&\alpha&0&\alpha^2&\alpha&\alpha^2&\alpha&\alpha^2&1&0&1&0&1&\alpha&\alpha&\alpha&0&\alpha^2&1&0&1&\alpha&0&\alpha&\alpha&\alpha^2&\alpha&\alpha&\alpha\\
\alpha^2&\alpha^2&1&\alpha^2&0&\alpha^2&0&1&\alpha&\alpha&1&0&\alpha^2&0&\alpha^2&1&\alpha&1&1&1&\alpha&0&\alpha^2&\alpha&1&\alpha&0&\alpha&\alpha&\alpha^2\\
\alpha^2&0&\alpha^2&\alpha&\alpha^2&\alpha^2&1&1&\alpha&0&0&1&0&\alpha^2&1&\alpha&1&\alpha^2&1&0&0&\alpha&\alpha&1&\alpha&\alpha^2&\alpha&\alpha&\alpha^2&1\\
\alpha^2&0&1&0&\alpha^2&0&\alpha&0&1&0&1&0&\alpha&0&\alpha^2&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha&0&1&0&1&0\\
0&\alpha^2&0&1&0&\alpha^2&0&\alpha&0&1&0&1&0&\alpha&0&\alpha^2&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha&0&1&0&1\\
0&\alpha&\alpha&\alpha^2&\alpha&\alpha&\alpha&\alpha&0&\alpha&1&0&1&0&1&\alpha^2&\alpha&\alpha^2&0&\alpha&1&0&\alpha&\alpha^2&1&\alpha^2&\alpha&\alpha&1&\alpha^2\\
\alpha&\alpha&\alpha^2&1&\alpha&0&\alpha&0&\alpha&\alpha&0&1&0&1&\alpha^2&\alpha&\alpha^2&1&\alpha&\alpha&0&1&\alpha^2&1&\alpha^2&\alpha&\alpha&0&\alpha^2&\alpha\\
\alpha&1&0&\alpha&\alpha^2&1&1&\alpha&1&\alpha^2&1&0&\alpha^2&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha&\alpha&0&0&1&1&1&\alpha&\alpha^2&\alpha&\alpha\\
1&\alpha^2&\alpha&\alpha&1&\alpha&\alpha&\alpha^2&\alpha^2&\alpha&0&1&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha&1&0&\alpha&1&1&1&0&\alpha^2&1&\alpha&0\\
1&\alpha&\alpha&\alpha&\alpha^2&\alpha&\alpha&1&\alpha&\alpha^2&1&0&\alpha&0&0&1&1&1&\alpha^2&1&\alpha^2&0&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha^2&0&\alpha\\
\alpha&\alpha^2&\alpha&0&\alpha&1&1&\alpha^2&\alpha^2&1&0&1&0&\alpha&1&1&1&0&1&\alpha&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha^2&\alpha&\alpha&\alpha\\
\alpha&\alpha&1&\alpha^2&0&\alpha&0&\alpha&\alpha&\alpha&1&0&1&0&\alpha&\alpha^2&1&\alpha^2&\alpha&\alpha&1&0&1&\alpha^2&\alpha&\alpha^2&0&\alpha&\alpha&\alpha^2\\
\alpha&0&\alpha^2&\alpha&\alpha&\alpha&\alpha&\alpha&\alpha&0&0&1&0&1&\alpha^2&1&\alpha^2&\alpha&\alpha&0&0&1&\alpha^2&\alpha&\alpha^2&1&\alpha&\alpha&\alpha^2&1\\
\alpha&0&1&0&\alpha&0&\alpha^2&0&1&0&1&0&\alpha^2&0&\alpha&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha^2&0&1&0&1&0\\
0&\alpha&0&1&0&\alpha&0&\alpha^2&0&1&0&1&0&\alpha^2&0&\alpha&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha^2&0&1&0&1\\
0&1&\alpha&\alpha^2&1&1&\alpha^2&\alpha^2&0&\alpha&1&0&\alpha&0&\alpha^2&\alpha&1&\alpha&0&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&1&\alpha&\alpha&1&\alpha^2\\
1&1&\alpha^2&1&1&0&\alpha^2&0&\alpha&\alpha&0&1&0&\alpha&\alpha&1&\alpha&\alpha^2&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&1&\alpha^2&\alpha&0&\alpha^2&\alpha\\
1&\alpha^2&0&\alpha&\alpha&\alpha^2&\alpha&\alpha^2&1&\alpha^2&1&0&1&0&\alpha&\alpha&0&\alpha&1&\alpha^2&1&0&0&\alpha&\alpha&\alpha&\alpha&\alpha^2&\alpha&\alpha\\
\alpha^2&\alpha&\alpha&\alpha&\alpha^2&1&\alpha^2&1&\alpha^2&\alpha&0&1&0&1&\alpha&0&\alpha&\alpha&\alpha^2&\alpha&0&1&\alpha&\alpha&\alpha&0&\alpha^2&1&\alpha&0\\
\alpha^2&1&\alpha&\alpha&\alpha&1&\alpha^2&\alpha&\alpha&\alpha^2&1&0&\alpha^2&0&0&\alpha^2&\alpha^2&\alpha^2&1&\alpha&\alpha&0&1&1&0&1&1&\alpha^2&0&\alpha\\
1&\alpha&\alpha&0&1&\alpha^2&\alpha&1&\alpha^2&1&0&1&0&\alpha^2&\alpha^2&\alpha^2&\alpha^2&0&\alpha&\alpha^2&0&\alpha&1&0&1&1&\alpha^2&\alpha&\alpha&\alpha\\
1&1&1&\alpha^2&0&1&0&\alpha^2&\alpha&\alpha&1&0&\alpha&0&1&\alpha&\alpha^2&\alpha&\alpha^2&\alpha^2&\alpha^2&0&\alpha&1&\alpha^2&1&0&\alpha&\alpha&\alpha^2\\
1&0&\alpha^2&\alpha&1&1&\alpha^2&\alpha^2&\alpha&0&0&1&0&\alpha&\alpha&\alpha^2&\alpha&1&\alpha^2&0&0&\alpha^2&1&\alpha^2&1&\alpha&\alpha&\alpha&\alpha^2&1\\
     };
      \def\bshrink{0.1}
      \def\rshrink{0.08}
       \def\xshrink{.1}
        \def\yshrink{.08}
      \pgfmathtruncatemacro{\nrows}{14}
      \pgfmathtruncatemacro{\ncols}{14}
      \pgfmathtruncatemacro{\m}{2}
       \foreach \x in {0, ..., \nrows} {
       \pgfmathtruncatemacro{\tempa}{\m*\x+1}
       \pgfmathtruncatemacro{\tempb}{\m*\x+\m}
       \pgfmathtruncatemacro{\tempe}{\m*\x+2}
       \foreach \y in {0, ...,\ncols} {
         \pgfmathtruncatemacro{\tempc}{\m*\y+1}
         \pgfmathtruncatemacro{\tempd}{\m*\y+\m}
         \draw[dotted, blue]
           ([xshift=-\xshrink ex, yshift=\yshrink ex] 
G-\tempc-\tempa.north west) --
           ([xshift=\xshrink ex, yshift=\yshrink ex] 
G-\tempc-\tempb.north east) --
           ([xshift=\xshrink ex, yshift=-\yshrink ex] 
G-\tempd-\tempb.south east) --
           ([xshift=-\xshrink ex, yshift=-\yshrink ex] 
G-\tempd-\tempa.south west) --
           cycle;
        \draw[red]
           ([xshift=-\bshrink ex, yshift=\rshrink ex] 
G-\tempc-\tempe.north west) --
           ([xshift=\bshrink ex, yshift=\rshrink ex] 
G-\tempc-\tempb.north east) --
           ([xshift=\bshrink ex, yshift=-\rshrink ex] 
G-\tempd-\tempb.south east) --
           ([xshift=-\bshrink ex, yshift=-\rshrink ex] 
G-\tempd-\tempe.south west) --
           cycle;
           }
           }
    \end{tikzpicture}
\end{document}

Please suggest which parameters should I change.

  • thanks @CarLaTeX. it works with text width=. – Rohit Bohara Jan 13 at 9:28
4

i would rewrote your matrix as follows:

  • in matrix's options would use matrix of math nodes
  • for nodes in matrix would use matrix's options and not define as every node/.style (by this nodes' contents are in math node)
  • red and blue dotted lines would draw as nodes border. these nodes would place by use of the fit library in one double loop

complete mwe:

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{fit, matrix}

\begin{document}

\section{Introduction}
$\widetilde{G}{\;=\;}$
   \begin{tikzpicture}[baseline]
   \matrix (G) [
       matrix of math nodes, 
       nodes={font=\footnotesize,
              text height=0.6em, minimum size=1em,
              anchor=base,inner sep=0pt},
       left delimiter={[},right delimiter={]},
       every even column/.style={column sep=2pt},
       row sep= \ifodd\pgfmatrixcurrentrow% as sugested @marmot in his answer on question
                                          % https://tex.stackexchange.com/questions/469954/
                    -\pgflinewidth%
                \else%
                    3pt%
                \fi,
     ]
      {
1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0\\
0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1&0&1\\
0&\alpha^2&\alpha&\alpha^2&\alpha^2&\alpha^2&1&1&0&\alpha&1&0&\alpha^2&0&\alpha&1&\alpha^2&1&0&1&\alpha&0&1&\alpha&\alpha^2&\alpha&\alpha&\alpha&1&\alpha^2\\
\alpha^2&\alpha^2&\alpha^2&1&\alpha^2&0&1&0&\alpha&\alpha&0&1&0&\alpha^2&1&\alpha^2&1&\alpha&1&1&0&\alpha&\alpha&\alpha^2&\alpha&1&\alpha&0&\alpha^2&\alpha\\
\alpha^2&\alpha&0&\alpha&1&\alpha&\alpha^2&1&1&\alpha^2&1&0&\alpha&0&1&1&0&1&\alpha&1&\alpha^2&0&0&\alpha^2&\alpha^2&\alpha^2&\alpha&\alpha^2&\alpha&\alpha\\
\alpha&1&\alpha&\alpha&\alpha&\alpha^2&1&\alpha&\alpha^2&\alpha&0&1&0&\alpha&1&0&1&1&1&\alpha^2&0&\alpha^2&\alpha^2&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&0\\
\alpha&\alpha^2&\alpha&\alpha&1&\alpha^2&1&\alpha^2&\alpha&\alpha^2&1&0&1&0&0&\alpha&\alpha&\alpha&\alpha&\alpha^2&1&0&\alpha&\alpha&0&\alpha&1&\alpha^2&0&\alpha\\
\alpha^2&1&\alpha&0&\alpha^2&\alpha&\alpha^2&\alpha&\alpha^2&1&0&1&0&1&\alpha&\alpha&\alpha&0&\alpha^2&1&0&1&\alpha&0&\alpha&\alpha&\alpha^2&\alpha&\alpha&\alpha\\
\alpha^2&\alpha^2&1&\alpha^2&0&\alpha^2&0&1&\alpha&\alpha&1&0&\alpha^2&0&\alpha^2&1&\alpha&1&1&1&\alpha&0&\alpha^2&\alpha&1&\alpha&0&\alpha&\alpha&\alpha^2\\
\alpha^2&0&\alpha^2&\alpha&\alpha^2&\alpha^2&1&1&\alpha&0&0&1&0&\alpha^2&1&\alpha&1&\alpha^2&1&0&0&\alpha&\alpha&1&\alpha&\alpha^2&\alpha&\alpha&\alpha^2&1\\
\alpha^2&0&1&0&\alpha^2&0&\alpha&0&1&0&1&0&\alpha&0&\alpha^2&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha&0&1&0&1&0\\
0&\alpha^2&0&1&0&\alpha^2&0&\alpha&0&1&0&1&0&\alpha&0&\alpha^2&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha&0&1&0&1\\
0&\alpha&\alpha&\alpha^2&\alpha&\alpha&\alpha&\alpha&0&\alpha&1&0&1&0&1&\alpha^2&\alpha&\alpha^2&0&\alpha&1&0&\alpha&\alpha^2&1&\alpha^2&\alpha&\alpha&1&\alpha^2\\
\alpha&\alpha&\alpha^2&1&\alpha&0&\alpha&0&\alpha&\alpha&0&1&0&1&\alpha^2&\alpha&\alpha^2&1&\alpha&\alpha&0&1&\alpha^2&1&\alpha^2&\alpha&\alpha&0&\alpha^2&\alpha\\
\alpha&1&0&\alpha&\alpha^2&1&1&\alpha&1&\alpha^2&1&0&\alpha^2&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha&\alpha&0&0&1&1&1&\alpha&\alpha^2&\alpha&\alpha\\
1&\alpha^2&\alpha&\alpha&1&\alpha&\alpha&\alpha^2&\alpha^2&\alpha&0&1&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha&1&0&\alpha&1&1&1&0&\alpha^2&1&\alpha&0\\
1&\alpha&\alpha&\alpha&\alpha^2&\alpha&\alpha&1&\alpha&\alpha^2&1&0&\alpha&0&0&1&1&1&\alpha^2&1&\alpha^2&0&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha^2&0&\alpha\\
\alpha&\alpha^2&\alpha&0&\alpha&1&1&\alpha^2&\alpha^2&1&0&1&0&\alpha&1&1&1&0&1&\alpha&0&\alpha^2&\alpha^2&0&\alpha^2&\alpha^2&\alpha^2&\alpha&\alpha&\alpha\\
\alpha&\alpha&1&\alpha^2&0&\alpha&0&\alpha&\alpha&\alpha&1&0&1&0&\alpha&\alpha^2&1&\alpha^2&\alpha&\alpha&1&0&1&\alpha^2&\alpha&\alpha^2&0&\alpha&\alpha&\alpha^2\\
\alpha&0&\alpha^2&\alpha&\alpha&\alpha&\alpha&\alpha&\alpha&0&0&1&0&1&\alpha^2&1&\alpha^2&\alpha&\alpha&0&0&1&\alpha^2&\alpha&\alpha^2&1&\alpha&\alpha&\alpha^2&1\\
\alpha&0&1&0&\alpha&0&\alpha^2&0&1&0&1&0&\alpha^2&0&\alpha&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha^2&0&1&0&1&0\\
0&\alpha&0&1&0&\alpha&0&\alpha^2&0&1&0&1&0&\alpha^2&0&\alpha&0&\alpha&0&\alpha^2&0&\alpha&0&\alpha^2&0&\alpha^2&0&1&0&1\\
0&1&\alpha&\alpha^2&1&1&\alpha^2&\alpha^2&0&\alpha&1&0&\alpha&0&\alpha^2&\alpha&1&\alpha&0&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&1&\alpha&\alpha&1&\alpha^2\\
1&1&\alpha^2&1&1&0&\alpha^2&0&\alpha&\alpha&0&1&0&\alpha&\alpha&1&\alpha&\alpha^2&\alpha^2&\alpha^2&0&\alpha^2&1&\alpha&1&\alpha^2&\alpha&0&\alpha^2&\alpha\\
1&\alpha^2&0&\alpha&\alpha&\alpha^2&\alpha&\alpha^2&1&\alpha^2&1&0&1&0&\alpha&\alpha&0&\alpha&1&\alpha^2&1&0&0&\alpha&\alpha&\alpha&\alpha&\alpha^2&\alpha&\alpha\\
\alpha^2&\alpha&\alpha&\alpha&\alpha^2&1&\alpha^2&1&\alpha^2&\alpha&0&1&0&1&\alpha&0&\alpha&\alpha&\alpha^2&\alpha&0&1&\alpha&\alpha&\alpha&0&\alpha^2&1&\alpha&0\\
\alpha^2&1&\alpha&\alpha&\alpha&1&\alpha^2&\alpha&\alpha&\alpha^2&1&0&\alpha^2&0&0&\alpha^2&\alpha^2&\alpha^2&1&\alpha&\alpha&0&1&1&0&1&1&\alpha^2&0&\alpha\\
1&\alpha&\alpha&0&1&\alpha^2&\alpha&1&\alpha^2&1&0&1&0&\alpha^2&\alpha^2&\alpha^2&\alpha^2&0&\alpha&\alpha^2&0&\alpha&1&0&1&1&\alpha^2&\alpha&\alpha&\alpha\\
1&1&1&\alpha^2&0&1&0&\alpha^2&\alpha&\alpha&1&0&\alpha&0&1&\alpha&\alpha^2&\alpha&\alpha^2&\alpha^2&\alpha^2&0&\alpha&1&\alpha^2&1&0&\alpha&\alpha&\alpha^2\\
1&0&\alpha^2&\alpha&1&1&\alpha^2&\alpha^2&\alpha&0&0&1&0&\alpha&\alpha&\alpha^2&\alpha&1&\alpha^2&0&0&\alpha^2&1&\alpha^2&1&\alpha&\alpha&\alpha&\alpha^2&1\\
     };
\foreach \i in {1,3,...,29}
{
\pgfmathtruncatemacro{\m}{\i+1}
\foreach \j in {1,3,...,29}
{
\pgfmathtruncatemacro{\n}{\j+1}
\node[draw=red, inner sep=0pt, fit=(G-\i-\n)(G-\m-\n)]{};
\node[draw=blue, dotted, inner sep=0pt, fit=(G-\i-\j)(G-\m-\n)]{};
}
}
    \end{tikzpicture}
\end{document}

enter image description here

  • @CarLaTeX,ups, i too much struggle why every even row/.style doesn't works that i overlooked this. i will correct asap. – Zarko Jan 13 at 13:51
  • @CarLaTeX, thank you very much for noticing my superficiality in my answer and also for up-voting my question. – Zarko Jan 13 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.