![Matriz de bloco com títulos](https://rvso.com/image/328812/Matriz%20de%20bloco%20com%20t%C3%ADtulos.png)
Criei uma matriz usando easybmat e blkarrary e gostaria de rotular as colunas acima da matriz. Este é o código que estou usando e a aparência da matriz no momento:
\usepackage{easybmat}
\usepackage{amsmath}
\usepackage{multirow,bigdelim}
\usepackage{blkarray}
\begin{document}
\[ \mathbb{X} = \begin{array}{c@{}c}
\left[
\begin{blockarray}{cccccc}
\boldsymbol{\beta} & x_1 & z_1 & \dots & z_{k-1}\\
\begin{BMAT}[3pt]{ccccc}{ccccccccc}
1 & x_{11} & 1 & \dots & 0 \\
1 & x_{21} & 1 & \dots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & \vdots & 1 & \dots & 0\\
\vdots & & & & \vdots \\
1 & \vdots & 0 & \dots & 0\\
1 & \vdots & 0 & \dots & 0\\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & x_{n1} & 0 & \dots & 0\\
\end{BMAT}
\end{blockarray}
\right]
&
\begin{array}{l}
\\[-17mm] \rdelim\}{4}{6mm}[$ \hspace{2mm} Category \hspace{2mm} 1$] \\ \\
\\[17mm] \rdelim\}{4}{6mm}[$\hspace{2mm} Category \hspace{2mm} k$] \\ \\
\end{array} \\[-1ex]
\end{array}
\]
\end{document}
Gostaria que a primeira linha da matriz atual fosse os títulos das colunas. Alguém pode apontar o que estou fazendo de errado, por favor?
Responder1
Aqui está o que eu poderia inventar. A única coisa que não gosto nesta solução é que estou tendo que usar \vphantom
na linha superior da matriz para que as .north
âncoras se alinhem. Não tenho certeza de como consertar isso enquanto mantenho a linha de base dos elementos da matriz alinhada. Se alguém tiver uma solução, sinta-se à vontade para contribuir.
Este pequeno incômodo foi corrigido. A altura de um nó no TikZ pode ser substituída com a text height
opção. Isso é muito melhor do que ter que usar \vphantom
.
Não estou completamente familiarizado com easybmat
and blkarray
, então optei por usar TikBiblioteca Z' matrix
que permite que recursos adicionais sejam desenhados facilmente em uma matriz. Aqui está o código para o que você deseja:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{
matrix,
positioning,
decorations,
decorations.pathreplacing
}
\begin{document}
\begin{equation*}
\mathbb{X} =
\begin{tikzpicture}[baseline=(m.center)]
\matrix (m) [
matrix of math nodes,
left delimiter={[},
right delimiter={]},
row 1/.style={nodes={text height=1ex}}
] {
1 & x_{11} & 1 & \cdots & 0 \\
1 & x_{21} & 1 & \cdots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & \vdots & 1 & \cdots & 0 \\
\vdots & & & & \vdots \\
1 & \vdots & 0 & \cdots & 0 \\
1 & \vdots & 0 & \cdots & 0 \\
\vdots & \vdots & \vdots & \cdots & \vdots \\
1 & x_{n1} & 0 & \cdots & 0 \\
};
\node [above=1ex of m-1-1] {\(\boldsymbol{\beta}\)};
\node [above=1ex of m-1-2] {\(x_{1}\)};
\node [above=1ex of m-1-3] {\(z_{1}\)};
% \node [above=1ex of m-1-4] {\\(\cdots\)};
\node [above=1ex of m-1-5] {\(z_{k-1}\)};
\draw [decoration={brace}, decorate]
([xshift=3ex]m-1-5.north east) -- ([xshift=3ex]m-4-5.south east)
node [pos=0.5, right=1ex] {Category 1};
\draw [decoration={brace}, decorate]
([xshift=3ex]m-6-5.north east) -- ([xshift=3ex]m-9-5.south east)
node [pos=0.5, right=1ex] {Category \(k\)};
\end{tikzpicture}
\end{equation*}
\end{document}
Para ilustrar especificamente o problema com a linha superior, compare as duas imagens a seguir que mostram os limites dos nós.
Para fins de integridade, o código original tinha:
right delimiter={]},
] {
1 & \vphantom{1}x_{11} & 1 & \vphantom{1}\cdots & 0 \\
1 & x_{21} & 1 & \cdots & 0 \\
Responder2
Aqui está uma solução com {bNiceMatrix}
of nicematrix
.
\documentclass{article}
\usepackage{amssymb} % for \mathbb
\usepackage{nicematrix}
\begin{document}
\begin{equation*}
\mathbb{X} =
\begin{bNiceMatrix}[first-row,last-col=6]
\beta & x_1 & z_1 & & z_{k-1} \\
1 & x_{11} & 1 & \cdots & 0 & \Block{4-1}{\quad \text{Category 1}}\\
1 & x_{21} & 1 & \cdots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & \vdots & 1 & \cdots & 0 \\
\vdots & & & & \vdots \\
1 & \vdots & 0 & \cdots & 0 & \Block{4-1}{\quad \text{Category } k}\\
1 & \vdots & 0 & \cdots & 0 \\
\vdots & \vdots & \vdots & \cdots & \vdots \\
1 & x_{n1} & 0 & \cdots & 0 \\
\CodeAfter [sub-matrix/xshift=2mm]
\SubMatrix{.}{1-1}{4-5}{\}}
\SubMatrix{.}{6-1}{9-5}{\}}
\end{bNiceMatrix}
\end{equation*}
\end{document}