The idea of this solution is to put the numbers into the third row of the matrix. But since I don't want to drop the loop, the solution becomes more complicated then expected.
First, I make sure that nodes are drawn with borders only for the first two rows, hence the [cell/.style]
option and [row 1/.style]
-[row 3/.style]
.
Second, following an answer in Tikz foreach inside matrix I collect the numbers from 0 to 16 into a macro \mymatrixcontents
and use it as the third matrix row. The \catcode
command is necessary because without it PGF issues an error (alternatively, you might replace &
by, say, \&
and use option ampersand replacement=\&
).
The code:
\documentclass{article}
\usepackage[landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}[cell/.style={align=center,inner sep=0pt,text width=1cm,minimum size=1cm}]
\let\mymatrixcontent\empty
\catcode`\&=\active
\foreach \x in {0,...,16} {%
\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\x &}%
}
\gappto\mymatrixcontent{\\}
\tikzstyle{row 3}=[gray,draw=white]
\matrix[matrix of math nodes, anchor=south west,
row 1/.style={nodes={cell,draw=red}},
row 2/.style={nodes={cell,draw=red}},
row 3/.style={nodes={cell,gray}}
]{
A & B & D & B & A & C & E & G & E & H & E & C & F & I & F & C & A \\
0 & 1 & 2 & 1 & 0 & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 3 & 2 & 1 & 0 \\
\mymatrixcontent
};
\end{tikzpicture}
\end{document}
The output:
