How can I make a similar matrix with even column spacing?

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.

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


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}


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}


• 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? Jul 26, 2016 at 19:55
• @DemetriP Then increase the 3.5ex to something more suitable. Jul 26, 2016 at 19:57
• @DemetriP See follow up to answer. Jul 26, 2016 at 20:01
• That is splendid. Thank you for all your help. Jul 26, 2016 at 20:02

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}