2

I was trying to use \newcommand inside tikz and matrix environment. See the following code:

\documentclass[border={0pt 0pt 0pt 0pt}]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning, shapes, shapes.geometric, shapes.arrows, }

\usepackage{array}

\newcommand{\Block}[3]{
    \ifnum #1=1
        {   \begin{tikzpicture}[auto, ]
            \node[draw, fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
            \end{tikzpicture}
        }
    %\else
    %   {   \begin{tikzpicture}[auto, ]
    %       \node[fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
    %       \end{tikzpicture}
    %   }
    \fi
}


\begin{document}

    \begin{tikzpicture} [
    auto,
    block1/.style    = { rectangle, , thick, draw, 
            text width=15em, text centered,
            rounded corners, minimum height=1em },  
    ]

%%%% WORKS
        \matrix [column sep=5.mm, row sep=12mm] {
            \node [block1] (p00) {00}; & \node [block1] (p01) {01}; & \node [block1] (p02) {02};  \\
            \node [block1] (p10) {10}; & \node [block1] (p12) {12}; & \node [block1] (p12) {12};  \\    
        };
    \\
%%%%%%% Does not work
%       \matrix [column sep=5.mm, row sep=12mm] {   
%           \Block {1}{p00}{00}; & \Block {1}{p01}{01}; & \Block {1}{p02}{02};  \\
%           \Block {1}{p11}{11}; & \Block {1}{p11}{11}; & \Block {1}{p12}{12};  \\  
%       };

    \end{tikzpicture}
\end{document}

The first matrix works as expected (see image), whereas the second doesn't (the positioning gets messed up). My question is, how should I modify the \newcommand such that it works (in the similar way of the former)?

hola
  • 4,026
  • 3
  • 35
  • 72

2 Answers2

2

Your problem is that you have a tikzpicture environment inside your \Block macro. Nesting tikzpictures is generally a bad idea, see e.g. Problem with overlay when a tikzpicture is inside another tikzpicture

If you remove \begin{tikzpicture}[auto] and \end{tikzpicture} from your macro definition, it works fine.

On a side note, perhaps you might be interested the matrix library, and the matrix of nodes option.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix}

\newcommand{\Block}[3]{
    \ifnum #1=1
        {   
%       \begin{tikzpicture}[auto, ]
            \node[draw, fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
%       \end{tikzpicture}
        }
    %\else
    %   {   \begin{tikzpicture}[auto, ]
    %       \node[fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
    %       \end{tikzpicture}
    %   }
    \fi
}


\begin{document}

    \begin{tikzpicture} [
    auto,
    block1/.style    = { rectangle, , thick, draw, 
            text width=15em, text centered,
            rounded corners, minimum height=1em },  
    ]

%%%% WORKS
        \matrix [column sep=5.mm, row sep=12mm] (m1) {
            \node [block1] (p00) {00}; & \node [block1] (p01) {01}; & \node [block1] (p02) {02};  \\
            \node [block1] (p10) {10}; & \node [block1] (p12) {12}; & \node [block1] (p12) {12};  \\    
        };
    \\
%%%%%%% Works
       \matrix [column sep=5.mm, row sep=12mm,below=of m1] (m2) {   
           \Block {1}{p00}{00}; & \Block {1}{p01}{01}; & \Block {1}{p02}{02};  \\
           \Block {1}{p11}{11}; & \Block {1}{p11}{11}; & \Block {1}{p12}{12};  \\  
       };

\matrix [matrix of nodes,nodes={block1},below=of m2,column sep=5.mm, row sep=12mm] (m3) {
   00 & 01 & 02 \\
   10 & 11 & 12 \\
};


    \end{tikzpicture}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
2

Although I don't know if the example you posted was the final desired result here you a more automatic way to type similar matrices using \pgfmatrixcurrentrow and \pgfmatrixcurrentcolumn. Of course it also uses a matrix of nodes as Torbjørn T. suggested.

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


\tikzset{
    Block1/.style = {%
        rectangle,
        draw,
        fill = gray,
        thick,
        align = center,
        rounded corners = 3pt,
        minimum height = 1em,
        minimum width = 6em,
        text = black,
        alias = p\the\numexpr\pgfmatrixcurrentrow-1\relax\the\numexpr\pgfmatrixcurrentcolumn-1\relax,
        node contents = \the\numexpr\pgfmatrixcurrentrow-1\relax\the\numexpr\pgfmatrixcurrentcolumn-1\relax,
    },
}

\begin{document}

\begin{tikzpicture} 

\matrix (A) [column sep=5mm, row sep=12mm, matrix of nodes, 
              nodes in empty cells, nodes=Block1,
              row 2/.style={nodes = {Block1, fill=red!30}}]
{
&&\\
&&\\
};

\draw (p00) |- ([shift={(3mm,3mm)}]p02.north east) |- (p12);

\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588