[Tex/LaTex] tabular inside tikz matrix

matricestablestikz-pgf

The matrix is not being layed out correctly. It should be a 2×3 matrix. What's wrong with the code?

MWE:

\documentclass{article}

\usepackage[table]{xcolor}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\newcommand{\agente}[3] {  % \belongs
\node[draw,inner sep=0] {
    \tabcolsep=0cm
    \begin{tabular}{m{1em}m{1em}}
    #1 & #2 \\
    #3 & \\
    \end{tabular}
}; \\
}
\def\semagente{\agente{}{}{}}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\tikzset{
}
\matrix[ampersand replacement=\&]{
    \semagente \& \agente{\cellcolor{green} 5}{\cellcolor{orange} 2}{\cellcolor{red} 1} \& \semagente \\
    \semagente \& \agente{\cellcolor{green} 5}{\cellcolor{orange} 2}{\cellcolor{red} 1} \& \semagente \\
};
\end{tikzpicture}
\end{figure}

\end{document}

output

ps: if you know a better way to have a 2×2 matrix inside a big matrix like this, let me know too 😛

Best Answer

Remove the extra \\ after the node closing semicolon and it works

\PassOptionsToPackage{table}{xcolor}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand{\agente}[3] {%
\node[draw,inner sep=0] {\tabcolsep=0cm%
    \begin{tabular}{m{1em}m{1em}}
    #1 & #2 \\
    #3 & \\
    \end{tabular}
};}
\def\semagente{\agente{}{}{}}
\begin{document}
\begin{tikzpicture}
\matrix[ampersand replacement=\&]{
    \semagente \& \agente{\cellcolor{green} 5}{\cellcolor{orange} 2}{\cellcolor{red} 1} \& \semagente \\
    \semagente \& \agente{\cellcolor{green} 5}{\cellcolor{orange} 2}{\cellcolor{red} 1} \& \semagente \\
};
\end{tikzpicture}
\end{document}

enter image description here