# Automated matrix padding in tikz

I encounter an error with the following code :

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.multipart, fit,backgrounds,calc}

\begin{document}

\node[matrix, column sep=1cm,] (postes){%
\foreach\x in{1,...,5}{%
\node{poste};&
}
\node{poste};\\
};

\end{tikzpicture}
\end{document}


In that case, I use a matrix in order to obtain a multi-column matrix with the foreach command but it seems that it is inappropriate. I was wondering if some simple solution for padding automatically a matrix in tikz existed.

• Your MWE is not complete. In it are missed used TikZ libraries, definition for \pposte . Nov 20, 2015 at 20:39

This is not a completely automatic solution like the one proposed in Tikz foreach inside matrix but node contents and nodes in empty cells options can help to provide a semiautomatic solution.

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}

\matrix (m) [draw, matrix of nodes, nodes in empty cells,
nodes={draw,
node contents={$A_{\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn}$}},
row sep=2mm, column sep=1mm]
{
&&&&&\\
&&&&&\\
};

\end{tikzpicture}
\end{document}


Meanwhile during wait on solution for automatic padding of matrix see if the next solution is work for you:

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[
node distance = 3mm,
every node/.style = {draw, inner sep=1mm},
]
\foreach \i in {1,2,...,5}
\node (n\i) at (\i,0) {$A_{1\i}$};
\node[fit=(n1) (n5)] {};
\end{tikzpicture}
\end{document}


Obtained picture is: