11

Trying to create a matrix using the below markup:

\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}

The output that I get is as below:

enter image description here

As you can see, that the columns of the matrix are unevenly spaced. It would be great if I can get evenly spaced columns.

Any ideas?

5
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Aug 2 '15 at 20:55
  • 1
    You could put each of the labels into a fixed width box. \makebox[1cm][c]{\textit{reading}} – Thruston Aug 2 '15 at 20:56
  • Why do you use \[ ... \] inside of figure? – user31729 Aug 2 '15 at 20:57
  • It's possible to use a \BAnewcolumntype which does automatically has a width of 1cm etc. – user31729 Aug 2 '15 at 21:19
  • Could you show up please and give some feedback on the solutions here? – user31729 Aug 5 '15 at 12:34
12

Warning: I am no blkarray expert ;-)

However, it's possible to define a new column type (say B) for blockarray which uses the same width for all columns with this specifier.

Since there 7 columns with this feature, one could shorten the ccccccc and replace with *{7}B as in usual tabular environments.

I've to find out, whether \BAnewcolumntype supports on column type with an argument.

\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}

enter image description here

9
  • @MikeRenfro: It's not clear that the OP doesn't want to have the text in math mode, so this edit is a little bit too fast, in my point of view – user31729 Aug 2 '15 at 21:52
  • Fair enough. Feel free to revert back to the original. – Mike Renfro Aug 2 '15 at 21:53
  • @MikeRenfro: Nope, I keep it. Thanks anyway – user31729 Aug 2 '15 at 21:54
  • 1
    @ChristianHupfer I'm pretty sure the OP has the mental capacity to remove the \text - additionally, if the OP didn't want the text and indeed the headings as math, the OP should be shot. – Alec Teal Aug 3 '15 at 4:53
  • @AlecTeal: The point is that answers shouldn't be edited unless there is a severe error. Give edit or addition suggestions in a comment rather – user31729 Aug 3 '15 at 7:40
2

The tabstackengine package has a fixed width option for arrays, which I employ here. Because of that, I merely needed to make a single entry of the "1" array of the same width as the word "reading" and the rest was resolved. I accomplished that with \tmpbox.

The package also allows either text or math to be stacked, which I used here to stack the text as text and the numbers as math.

\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}

enter image description here

The inter-column gap is set to 1ex with \setstacktabbedgap{1ex}. If one wished the inter-row baselineskip to be larger, the line \setstackgap{L}{1.2\baselineskip} could be added to the preamble to achieve the following result:

enter image description here

Finally, one could left-align the right-hand column of words by adding a [l] optional argument to the \Centerstack.

1

A variant with one less column, and 7 blocks, with \Right{.}{text} at the end of the preamble of each block. I also use the eqparbox package to ensure equal width columns, without having to find by trials and errors what will be the right size:

\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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.