2

Right now, I have the code below that makes this

enter image description here

However, I'd like to do two things. One, change the indices to be on over the boxes instead of under. Two, add more rows so that this is a 2D array, hopefully with different amount of spacing in between the rows and columns.

Basically, I want this but where every row is different. enter image description here

Any help is appreciated!

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcounter{index}

\begin{document}

\begin{figure}
\centering
  \begin{tikzpicture}
    \setcounter{index}{0}
    \coordinate (s) at (0,0);
    \foreach \num in {3, 1, 4, 1, 5}{
      \node[minimum size=6mm, draw, rectangle] at (s) {\num};
      \node at ($(s)-(0,0.5)$) {\theindex};
      \stepcounter{index}
      \coordinate (s) at ($(s) + (1,0)$);
    }
  \end{tikzpicture}
  \label{fig:testArray}
\end{figure}

\end{document}
1
4

just a part of image for the case if the node contents is prescribed. with use of matrix:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\usepackage[active,floats, tightpage]{preview}
    \setlength\PreviewBorder{1em}

\begin{document}
    \begin{figure}
\centering
  \begin{tikzpicture}
\matrix (m) [matrix of nodes,
             nodes={minimum size=8mm},
             column sep=4mm]
{
0 & 1 & 2 & 3 & 4   \\
};
\matrix (n) [below=-3mm of m,
             matrix of nodes,
             nodes={draw, minimum size=8mm},
             column sep=4mm,
             row sep=2mm]
{
9 & 2 & 4 & 4 & 5   \\
8 & 3 & 3 & 5 & 5   \\
7 & 4 & 2 & 7 & 5   \\
6 & 5 & 1 & 1 & 5   \\
5 & 6 & 0 & 2 & 5   \\
};
\end{tikzpicture}
    \label{fig:testArray}
    \end{figure}
\end{document}

addendum: inspired by Ignasi answer and his question question. now with one matrix and indices as labels of nodes in the first row::

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}

\matrix [matrix of nodes,
         nodes = {draw,  minimum size = 2ex},
         column sep=1ex, row sep=1ex,
         row 1/.style={execute at begin cell= {|[label=\pgfmathparse{int(\the\pgfmatrixcurrentcolumn-1)}\pgfmathresult]|}}
         ]
{
9 & 2 & 4 & 4 & 5   \\
8 & 3 & 3 & 5 & 5   \\
7 & 4 & 2 & 7 & 5   \\
6 & 5 & 1 & 1 & 5   \\
5 & 6 & 0 & 2 & 5   \\
};
\end{tikzpicture}
\end{document}

enter image description here

6

This solution is similar to Zarko's one, but uses only one matrix. The first row is used for column enumerating and all other rows are the real matrix contents. As we want all nodes with border except for the first row, a special style has been defined: row 1/.style={nodes={draw=none}}.

Distance between columns and row is declared with col sep and row sep, but it can be changed with an special command: [value] in first row elements or after end of row declaration: \\. In following code there are some examples, two of them just to show its effect, but [-3mm] after first row has been used to compensate the size of first row nodes and make them look like a label.

\documentclass[tikz, border=1em]{standalone}
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,
             nodes={draw, minimum size=8mm},
             column sep=3mm,
             row sep=2mm,
             row 1/.style={nodes={draw=none}}]
{
0 & 1 & 2 &[5mm] 3 & 4   \\[-3mm]
9 & 2 & 4 & 4 & 5   \\
8 & 3 & 3 & 5 & 5   \\
7 & 4 & 2 & 7 & 5   \\[3mm]
6 & 5 & 1 & 1 & 5   \\
5 & 6 & 0 & 2 & 5   \\
};
\end{tikzpicture}
\end{document}

enter image description here

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.