
嘗試使用以下標記建立矩陣:
\begin{figure}
\centering
\[
\begin{blockarray}{cccccccc}
I & like & nlp & mlpr & enjoy & reading & books \\
\begin{block}{[ccccccc]c}
1 & 1 & 1 & 1 & 1 & 1 & 1 & I \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & like \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & nlp \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & mlpr \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & enjoy \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & reading \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & books \\
\end{block}
\end{blockarray}
\]
\caption{A caption to the entire figure}
\end{figure}
我得到的輸出如下:
正如您所看到的,矩陣的列間隔不均勻。如果我能得到均勻間隔的列,那就太好了。
有任何想法嗎?
答案1
警告:我不是blkarray
專家;-)
但是,可以定義一個新的列類型(例如 B),blockarray
使用此說明符對所有列使用相同的寬度。
由於有 7 列具有此功能,因此可以像通常的表格環境中一樣縮短ccccccc
並替換為。*{7}B
我必須找出是否\BAnewcolumntype
支援帶有參數的列類型。
\documentclass{article}
\usepackage{mathtools}
\usepackage{blkarray}
\BAnewcolumntype{B}{>{\centering}p{1cm}}
\begin{document}
\begin{figure}
\centering
$
\begin{blockarray}{cccccccc}
\text{I} & \text{like} & \text{nlp} & \text{mlpr} & \text{enjoy} & \text{reading} & \text{books} \\
\begin{block}{[*{7}B]c}
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{I} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{like} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{nlp} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{mlpr} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{enjoy} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{reading} \\
1 & 1 & 1 & 1 & 1 & 1 & 1 & \text{books} \\
\end{block}
\end{blockarray}
$
\caption{A caption to the entire figure}
\end{figure}
\end{document}
答案2
該tabstackengine
包有一個固定寬度的數組選項,我在這裡使用它。因此,我只需要在“1”數組中創建一個與“reading”一詞寬度相同的條目,剩下的就解決了。我用 實現了這一點\tmpbox
。
該包還允許堆疊文本或數學,我在這裡使用它來將文本堆疊為文本,將數字堆疊為數學。
\documentclass{article}
\usepackage{tabstackengine}
\setstackEOL{\cr}
\setstacktabbedgap{1ex}
\fixTABwidth{T}
\begin{document}
\begin{figure}
\centering
\[
\stackText
\setbox0=\hbox{reading}
\savestack{\tmpbox}{\makebox[\wd0]{$1$}}
\savestack{\toprow}{\tabbedCenterstack{I & like & nlp & mlpr & enjoy & reading & books}}
\ensurestackMath{
\stackon{\bracketMatrixstack{
\tmpbox & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1 \cr
1 & 1 & 1 & 1 & 1 & 1 & 1
}}{\toprow}}
\Centerstack{ I \cr like \cr nlp \cr mlpr \cr enjoy \cr reading \cr books}
\]
\caption{A caption to the entire figure}
\end{figure}
\end{document}
列間隙設定為 1ex \setstacktabbedgap{1ex}
。如果希望行間基線跳躍更大,\setstackgap{L}{1.2\baselineskip}
可以將該行添加到前導碼中以實現以下結果:
[l]
最後,可以透過在 .txt 檔案中新增一個可選參數來左對齊右側的單字列\Centerstack
。
答案3
少一列和 7 個區塊的變體,\Right{.}{text}
每個區塊的前導碼末尾都有。我還使用該eqparbox
套件來確保列的寬度相等,而不必通過試驗和錯誤來找到正確的尺寸:
\documentclass{article}
\usepackage{mathtools}
\usepackage{blkarray}
\usepackage{eqparbox}
\BAnewcolumntype{B}{>{\centering}p{1cm}}
\begin{document}
\begin{figure}
\centering\makeatletter\setlength\tabcolsep{-10pt}\BA@colsep =1pt\makeatother
$
\begin{blockarray}{*{7}{c}}
\eqmakebox[C]{I} & \eqmakebox[C]{like} & \eqmakebox[C]{nlp} & \eqmakebox[C]{mlpr} & \eqmakebox[C]{enjoy} & \eqmakebox[C]{reading} & \eqmakebox[C]{books} \\
\begin{block}{*{7}{c}\Right{.}{I}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{like}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{nlp}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{mlpr}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{enjoy}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{reading}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\begin{block}{*{7}{c}\Right{.}{books}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\end{block}
\end{blockarray}
$
\caption{A caption to the entire figure}
\end{figure}
\end{document}