# Aligning two matrices's values (one on top of the other)

It's kinda hard to explain what I want so here's the code and the result:

\begin{gather*}
\begin{matrix}
{} & \begin{pmatrix} 0 & 1 & \cdots & n-1 \end{pmatrix} \\
\begin{pmatrix} 0 \\  1 \\ \vdots \\ n-1 \end{pmatrix} &
\begin{pmatrix}
f(0,0)&f(0,1)&\cdots&f(0,n-1)\\
f(1,0)&f(1,1)&\cdots&f(1,n-1)\\
\vdots&\vdots&\ddots&\vdots\\
f(n-1,0)&f(n-1,1)&\cdots&f(n-1,n-1)
\end{pmatrix}
\end{matrix}
\end{gather*}


What I wanted is for the upper matris to be equally formatted as the big one below. So "0" shoud be aligned with "f(0,0)", "1" with "f(0,1)" and so on. I don't think I can use arrays since that wouldn't be able to use brackets. A solution would be appreciated, thanks.

A quick fix; use \makebox[\widthof{<last row cell>}]{<first row cell>}. This requires the calc package to compute \widthof.

 \begin{gather*}
\begin{matrix}
{} & \begin{pmatrix}\makebox[\widthof{$f(n-1,0)$}]{0} & \makebox[\widthof{$f(n-1,1)$}]{1} & \cdots & \makebox[\widthof{$f(n-1,n-1)$}]{$n-1$}\end{pmatrix} \\[.2em]
\begin{pmatrix} 0 \\  1 \\ \vdots \\ n-1 \end{pmatrix} &
\begin{pmatrix}
f(0,0)&f(0,1)&\cdots&f(0,n-1)\\
f(1,0)&f(1,1)&\cdots&f(1,n-1)\\
\vdots&\vdots&\ddots&\vdots\\
f(n-1,0)&f(n-1,1)&\cdots&f(n-1,n-1)
\end{pmatrix}
\end{matrix}
\end{gather*}


• You might want to use \mathmakebox from mathtools (\widthof is included). This is the approach used by \vdotswithin. – John Kormylo Jun 11 '17 at 19:25