![帶標題的區塊矩陣](https://rvso.com/image/328812/%E5%B8%B6%E6%A8%99%E9%A1%8C%E7%9A%84%E5%8D%80%E5%A1%8A%E7%9F%A9%E9%99%A3.png)
我使用 easybmat 和 blkarary 建立了一個矩陣,我想標記矩陣上方的欄位。這是我正在使用的程式碼以及矩陣目前的樣子:
\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}
我希望當前矩陣中的第一行作為列的標題。誰能指出我做錯了什麼嗎?
答案1
這是我能想到的。我對這個解決方案唯一不喜歡的是,我必須\vphantom
在矩陣的最頂行使用,以便.north
錨點對齊。我不知道如何解決這個問題,同時仍然保持矩陣元素的基線對齊。如果其他人有解決方案,請隨時參與。
這個小煩惱已經解決了。 TikZ 中節點的高度可以使用選項覆蓋text height
。這比必須使用要好得多\vphantom
。
我並不完全熟悉easybmat
和blkarray
,所以我選擇使用 TikZ'matrix
庫允許在矩陣上輕鬆繪製附加特徵。這是您想要的程式碼:
\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}
為了具體說明頂行的問題,請比較以下兩個顯示節點邊界的圖片。
為了完整起見,原始碼有:
right delimiter={]},
] {
1 & \vphantom{1}x_{11} & 1 & \vphantom{1}\cdots & 0 \\
1 & x_{21} & 1 & \cdots & 0 \\
答案2
這是一個解決{bNiceMatrix}
方案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}