4

Suppose I have the following latex code which draws a matrix within a matrix as follows where the cell (1,2) of the outer matrix is a vector that defines the column labels and the cell (2,1) of the outer matrix is a vector that defines the row labels and the cell (2,2) contains the matrix:

\begin{matrix}
 & \begin{matrix}
\cdots & \rho_{ij} & \cdots & \rho_{ik} & \cdots & \gamma_i & \cdots & \gamma_j  & \cdots & \lambda 
\end{matrix}\\ 
\begin{matrix}
\cdots \\ 
\rho_{ij}\\ 
\cdots \\ 
\rho_{ik}\\ 
\cdots \\ 
\gamma_i \\ 
\cdots \\ 
\lambda \\ 
\end{matrix} & \begin{bmatrix}
\cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
\cdots & 1 & \cdots & 0 & \cdots & b_j \mu_j & \cdots & b_i \mu_i & \cdots & 0 \\ 
\cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
\cdots & 0 & \cdots & 1 & \cdots & b_j \mu_j & \cdots & 0 & \cdots & 0 \\ 
\cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
\cdots & b_j \mu_j & \cdots & b_k \mu_k & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i\\ 
\cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
\cdots & 0 & \cdots & 0 & \cdots & -\mu_i & \cdots & -\mu_j & \cdots & 0
\end{bmatrix}
\end{matrix}

The problem I have at hand is that the row labels (in cell (2,1)) and the column labels (in cell(1,2)) do not necessarily align with the matrix (in cell (2,2)).

How can I align them?

Werner
  • 603,163
uday
  • 153

2 Answers2

2

To stay true to your implementation, you'll have to use some way of specifying the width of the columns that have differing widths-than-headings. Easiest is to make a box where you find the widest element in the column to get the correct width, and set the heading within that box of said width:

enter image description here

\documentclass{article}
\usepackage{mathtools,calc}
\newcommand{\mathwd}[2]{\makebox[\widthof{$#1$}]{$#2$}}
\begin{document}

\[
  \begin{matrix}
    &
    \begin{matrix}
      \cdots & \mathwd{b_j\mu_j}{\rho_{ij}} & \cdots & \mathwd{b_k\mu_k}{\rho_{ik}} & \cdots & 
        \mathwd{b_j\mu_j}{\gamma_i} & \cdots & \mathwd{-\mu_j}{\gamma_j} & \cdots & \mathwd{-\mu_i}{\lambda}
    \end{matrix} \\
    \begin{matrix}
      \cdots \\ 
      \rho_{ij}\\ 
      \cdots \\ 
      \rho_{ik}\\ 
      \cdots \\ 
      \gamma_i \\ 
      \cdots \\ 
      \lambda \\ 
    \end{matrix} &
    \begin{bmatrix}
      \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
      \cdots & 1 & \cdots & 0 & \cdots & b_j \mu_j & \cdots & b_i \mu_i & \cdots & 0 \\ 
      \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
      \cdots & 0 & \cdots & 1 & \cdots & b_j \mu_j & \cdots & 0 & \cdots & 0 \\ 
      \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
      \cdots & b_j \mu_j & \cdots & b_k \mu_k & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i\\ 
      \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\\ 
      \cdots & 0 & \cdots & 0 & \cdots & -\mu_i & \cdots & -\mu_j & \cdots & 0
    \end{bmatrix}
  \end{matrix}
\]

\end{document}

Alternative inputs also exist in the form of bordermatrix, blkarray and kbordermatrix. See Array with labeling columns.

Werner
  • 603,163
1

With {bNiceMatrix} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[ \begin{bNiceMatrix}[first-col,first-row] & \cdots & \rho_{ij} & \cdots & \rho_{ik} & \cdots & \gamma_i & \cdots & \gamma_j & \cdots & \lambda \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \rho_{ij}& \cdots & 1 & \cdots & 0 & \cdots & b_j \mu_j & \cdots & b_i \mu_i & \cdots & 0 \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \rho_{ik} & \cdots & 0 & \cdots & 1 & \cdots & b_j \mu_j & \cdots & 0 & \cdots & 0 \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \gamma_i & \cdots & b_j \mu_j & \cdots & b_k \mu_k & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i\ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \lambda & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i & \cdots & -\mu_j & \cdots & 0 \end{bNiceMatrix} ]

\end{document}

Output of the first code

If you want the first column (outside the matrix) centered, you have to change slightly the code:

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[ \begin{NiceArray}[first-row]{c[*{10}{c}]} & \cdots & \rho_{ij} & \cdots & \rho_{ik} & \cdots & \gamma_i & \cdots & \gamma_j & \cdots & \lambda \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \rho_{ij}& \cdots & 1 & \cdots & 0 & \cdots & b_j \mu_j & \cdots & b_i \mu_i & \cdots & 0 \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \rho_{ik} & \cdots & 0 & \cdots & 1 & \cdots & b_j \mu_j & \cdots & 0 & \cdots & 0 \ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \gamma_i & \cdots & b_j \mu_j & \cdots & b_k \mu_k & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i\ \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots & \cdots\ \lambda & \cdots & 0 & \cdots & 0 & \cdots & -\mu_i & \cdots & -\mu_j & \cdots & 0 \end{NiceArray} ]

\end{document}

Output of the second code

F. Pantigny
  • 40,250