Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a table as follows:

  &$\begin{array}{c|cc|cc}
    & 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}$

Is there a way to make each column have same width?

share|improve this question
    
Please read the documentation of array. –  Marco Daniel Mar 4 '12 at 20:34
    
Sorry, have not found anything which helps... –  SoftTimur Mar 4 '12 at 20:40
4  
This question needs considerable clarification. Ideally you should provide us with a minimum complete document that illustrates what you have so far, and, perhaps, a diagram of what you're trying to achieve. All I get from the code you supply is a jumble. –  Brent.Longborough Mar 4 '12 at 20:41
    
Documentation of array Table 1 -- column type p{width} –  Marco Daniel Mar 4 '12 at 20:47
2  
have you looked at the tabularx documentation? –  cmhughes Mar 4 '12 at 22:31

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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