Make each column of a table have same width

It's quite complicated to get the same width of all columns of an alignment without the user telling TeX what the desired column width or the preferred total width of the alignment is.

It's indeed possible by typesetting the alignment twice, the first one for gathering all the cells and measuring them. However, for a simple matrix as yours, it's probably easier to state a size, guessing at the widest entry:

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash$}p{#1}<{$}}

\begin{document}

$\begin{array}{C{1.8em}|C{1.8em}C{1.8em}|C{1.8em}C{1.8em}}
    & x & \multicolumn{1}{c}{y} & i & 0 \\ \hline
    x & 0 & \multicolumn{1}{c}{} & -49 &  \\
    y & & \multicolumn{1}{c}{0} &  &  \\ \cline{4-5}
    i & 49 & & 0 &-2\\ 
    0 & &  & 2 & 0
  \end{array}$

\end{document}

Just remember that, approximately, 1em is as wide as a capital M. As long as one entry in a column is not \multicolumn, you can use c for the argument of \multicolumn, because the effect will be exactly the same.


For fixed-column width with known column entries, I would suggest using \settowidth{<length register>}{<stuff>} to extract the width of the widest element in your array. Alternatively, the calc package provides \widthof{<stuff>} that provides a similar work-around:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\newcolumntype{C}[1]{>{\centering\arraybackslash$}p{#1}<{$}}

\begin{document}

\newlength{\mycolwd}% array column width
\settowidth{\mycolwd}{$-49$}% "width" of $-49$; largest element in array
\[
  \begin{array}{C{\mycolwd}*{4}{|C{\mycolwd}}}
      &  x & \multicolumn{1}{c}{y} &   i &  0 \\ \hline
    x &  0 & \multicolumn{1}{c}{}  & -49 &    \\
    y &    & \multicolumn{1}{c}{0} &     &    \\ \cline{4-5}
    i & 49 &   &   0 & -2 \\ 
    0 &    &   &   2 &  0
  \end{array}
\]

\end{document}

For example, using calc, one could use

  \begin{array}{C{\widthof{$-49$}}*{4}{|C{\widthof{$-49$}}}}

Note the use of *{<num>}{<col spec>} which repeats <col spec> a total of <num> times. It improves consistency and ease of updating.

Tags:

Arrays

Tables