ラテックスのマトリックス

ラテックスのマトリックス

以下のマークアップを使用してマトリックスを作成しようとしています:

\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専門家ではありません ;-)

blockarrayただし、この指定子を使用して、すべての列に同じ幅を使用する新しい列タイプ (たとえば B) を定義することは可能です。

この機能には 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には配列の固定幅オプションがあり、ここではこれを使用しています。そのため、単語「reading」と同じ幅の「1」配列のエントリを 1 つ作成するだけで、残りは解決されます。これは で実現しました\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]最後に、オプションの引数を に追加することで、右側の単語の列を左揃えにすることができます\Centerstack

答え3

列が 1 つ少なく、ブロックが 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} 

ここに画像の説明を入力してください

関連情報