4

I'd like to make a similar matrix to the one pictured below, but as you can see, the spacing between the columns is inconsistent due to the length of the words.enter image description here

Here is the code I used:

\[
\bordermatrix{\text{} & \text{all} & \text{dog} & \text{dogs} & \text{eat} & \text{fleas} & \text{go} & \text{has} & \text{heaven} & \text{in} & \text{is} & \text{it} & \text{my} & \text{no} & \text{to} & \text{world} \cr
D_1 & 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 1& 0& 0& 0 \cr   
D_2 &1& 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 0& 1& 0 \cr
D_3 &0& 1& 0& 0& 1& 0& 1& 1& 1& 0& 0& 0& 1& 0& 0 \cr
D_4& 0& 2& 0& 1& 0& 0& 0& 0& 0& 1& 1& 0& 0& 0& 1}
\]
4

Create a macro, here \fxtxt, which places the overtext entries in a fixed width box of appropriate width, here chosen as 3.5ex.

\documentclass{article}
\usepackage{amsmath}
\newcommand\fxtxt[1]{\makebox[3.5ex]{#1}}
\begin{document}
\[
\bordermatrix{\fxtxt{} & \fxtxt{all} & \fxtxt{dog} & \fxtxt{dogs} & \fxtxt{eat} & \fxtxt{fleas} & \fxtxt{go} & \fxtxt{has} & \fxtxt{heaven} & \fxtxt{in} & \fxtxt{is} & \fxtxt{it} & \fxtxt{my} & \fxtxt{no} & \fxtxt{to} & \fxtxt{world} \cr
D_1 & 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 1& 0& 0& 0 \cr   
D_2 &1& 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 0& 1& 0 \cr
D_3 &0& 1& 0& 0& 1& 0& 1& 1& 1& 0& 0& 0& 1& 0& 0 \cr
D_4& 0& 2& 0& 1& 0& 0& 0& 0& 0& 1& 1& 0& 0& 0& 1}
\]
\end{document}

enter image description here

Follow up to OP query, merely redefining \fxtxt to place at 45 degree angle, with narrower width and left alignment:

\documentclass{article}
\usepackage{amsmath,rotating}
\newcommand\fxtxt[1]{\rotatebox[origin=left]{45}{\makebox[3ex][l]{#1}}}
\begin{document}
\[
\bordermatrix{\fxtxt{} & \fxtxt{all} & \fxtxt{dog} & \fxtxt{dogs} & \fxtxt{eat} & \fxtxt{fleas} & \fxtxt{go} & \fxtxt{has} & \fxtxt{heaven} & \fxtxt{in} & \fxtxt{is} & \fxtxt{it} & \fxtxt{my} & \fxtxt{no} & \fxtxt{to} & \fxtxt{world} \cr
D_1 & 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 1& 0& 0& 0 \cr   
D_2 &1& 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 0& 1& 0 \cr
D_3 &0& 1& 0& 0& 1& 0& 1& 1& 1& 0& 0& 0& 1& 0& 0 \cr
D_4& 0& 2& 0& 1& 0& 0& 0& 0& 0& 1& 1& 0& 0& 0& 1}
\]
\end{document}

enter image description here

  • 1
    This is a great solution, but the words "has" and "heaven" are too close together.vvHow would I rotate the top column so the labels are 45 degrees to the horizontal? – Demetri Pananos Jul 26 '16 at 19:55
  • @DemetriP Then increase the 3.5ex to something more suitable. – Steven B. Segletes Jul 26 '16 at 19:57
  • @DemetriP See follow up to answer. – Steven B. Segletes Jul 26 '16 at 20:01
  • That is splendid. Thank you for all your help. – Demetri Pananos Jul 26 '16 at 20:02
1

Another solution with blkarray and \makebox:

\documentclass{article}
\usepackage{mathtools}
\usepackage{blkarray}
 \newcommand\chd[1]{\makebox[1.33em]{#1}}%

\begin{document}

\[ \begin{blockarray}{*{16}{c}}
  & \chd{all} & \chd{dog} & \chd{dogs} & \chd{eat} & \chd{fleas} & \chd{go} & \chd{has} & \chd{heaven} & \chd{in}%
  & \chd{is} & \chd{it} & \chd{my} & \chd{no} & \chd{to} & \chd{world}\\
  \begin{block}{c(*{15}{c})}
    D_1 & 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 1& 0& 0& 0 \\
    D_2 &1& 0& 1& 0& 0& 1& 0& 1& 0& 0& 0& 0& 0& 1& 0 \\
    D_3 &0& 1& 0& 0& 1& 0& 1& 1& 1& 0& 0& 0& 1& 0& 0 \\
    D_4& 0& 2& 0& 1& 0& 0& 0& 0& 0& 1& 1& 0& 0& 0& 1 \\
  \end{block}
  \end{blockarray} \]

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