3

I have the following simple tabular environment:

\begin{center}
\small
\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{$cm(ag)$}  & \multicolumn{2}{|c|}{Number of agents in}\\ \cline{2-3}
 & $\mathcal{B}_i(\alpha)$ & $\mathcal{B}'_i(\alpha)$ \\ \hline
HA & 10& 0 \\ \hline
AD & 5 & 2 \\ \hline
H  & 0 & 3 \\ \hline
\end{tabular}
\end{center}

And this is the result:

enter image description here

Why the two columns in the figure are not equally wide (one would expect it naturally to happen)?

  • Replace Number in the top header row with # and you'll see a big difference. – Mico Jan 7 '15 at 12:14
  • Because you have \multicolumn in the first row, which make table wider than necessary concerning the second row. So the second column obey "natural width" of content, third is accommodated to width dictated by \multicolumn. – Zarko Jan 7 '15 at 12:17
  • @Mico You are right in this case. But this problem keeps happening and I am after a general solution. Thanks. – Ali Jan 7 '15 at 12:17
  • 1
    Please always post complete documents not just fragments – David Carlisle Jan 7 '15 at 12:46
5

The third column is particularly wide as it has been stretched by the multicolumn entry. It is best to ensure that the natural width of a spanning cell is less than the width of the columns it is spanning.

Note that even without the spanning head the third column would be wider than second column as the heading is wider because of the '

In multicolumn, only use | on the right edge of a cell, except for the first column, otherwise you get duplicated double thick lines as seen in your image.

I left $cm$ as I do not know what it means but it looks a bit odd: if cm is to be read as a single word rather than c times m then it ought to be \mathit{cm}.

Here I used dcolumn to align the digits of the numbers and force the natural widths of the columns to be greater than the heading.

I also added some extra padding to keep the horizontal rules off the text.

enter image description here

\documentclass{article}
\usepackage{dcolumn,multirow}
\begin{document}

\begin{center}
\small
\setlength\extrarowheight{2pt}
\begin{tabular}{|c|D{.}{}{5.5}|D{.}{}{5.5}|}
\hline
\multirow{2}{*}{$cm(ag)$}  & \multicolumn{2}{c|}{Number of agents in}\\ \cline{2-3}
 & \multicolumn{1}{c|}{$\mathcal{B}_i(\alpha)$} & 
\multicolumn{1}{c|}{$\mathcal{B}'_i(\alpha)$} \\ \hline
HA & 10& 0 \\ \hline
AD & 5 & 2 \\ \hline
H  & 0 & 3 \\ \hline
\end{tabular}
\end{center}
\end{document}
  • In my quick test, the ' does not increase the width of the heading at all. – Paul Gessler Jan 7 '15 at 13:20
  • @PaulGessler yes I forgot about the subscript when making that comment but decided to let it stand as the real point is not this specific example but that in general with c columns latex is not trying to make the columns the same width at all. – David Carlisle Jan 7 '15 at 13:39
3

The issue you've noticed arises because the natural widths of columns 2 and 3 are quite small and because the width of the combined header, "Number of agents in", is more than twice the width of each individual column.

One way to address this imbalance is to force the widths of the next pair of header cells, $\mathcal{B}_i(\alpha)$^ and $\mathcal{B}'_i(\alpha)$, to exceed the natural widths of the columns; moreover, it's (probably?!) a good idea to make the equally wide. To generate the first table shown below, I set up a little utility macro called \mybox that typesets its argument centered in a "box" that's 1.25cm wide. (You're obviously free to choose a different width, but I wouldn't go below 1.0cm.)

A separate issue with your table, as currently defined, is that it's not all that readable: the multitude of lines mainly provides clutter, not clarity. The result of (i) getting rid of all vertical lines and two of the horizontal lines and (ii) using the rule-drawing macros of the booktabs package instead of the basic LaTeX \hline and \cline macros is shown in the second table below. I hope you'll agree with me that it's at least as readable as the first table is. :-)

enter image description here

\documentclass{article}
\usepackage{multirow,booktabs}
\newcommand{\mybox}[1]{\parbox{1.25cm}{\centering #1}}
\begin{document}
\begin{table}
\centering
\small
\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{$cm(ag)$}  & \multicolumn{2}{c|}{Number of agents in}\\ 
\cline{2-3}
 & \mybox{$\mathcal{B}_i(\alpha)$} 
 & \mybox{$\mathcal{B}'_i(\alpha)$} \\ \hline
HA & 10& 0 \\ \hline
AD & 5 & 2 \\ \hline
H  & 0 & 3 \\ \hline
\end{tabular}

\bigskip\bigskip
\begin{tabular}{lcc}
\toprule
$cm(ag)$  & \multicolumn{2}{c}{Number of agents in}\\
\cmidrule(l){2-3}
 & \mybox{$\mathcal{B}_i(\alpha)$}
 & \mybox{$\mathcal{B}'_i(\alpha)$} \\ \midrule
HA & 10& 0 \\ 
AD & 5 & 2 \\ 
H  & 0 & 3 \\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}

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.