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'm trying to make a table however the contents of certain columns aren't centred correctly.

I'm using the following in my preamble...

\documentclass[11pt,a4paper,twoside, twocolumn]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[UKenglish]{babel}
\usepackage[UKenglish]{isodate}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage{bm}
\usepackage{url}
\usepackage{paralist}
\usepackage{lipsum}
\usepackage{siunitx}
\usepackage{lettrine}
\usepackage{graphicx}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{2.75cm}}

\usepackage{todonotes}

\usepackage{titling}
\setlength{\droptitle}{-2em} 

\title{}
\author{}
\date{}

\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}
\addbibresource{phage.bib}

\usepackage{cleveref}
\usepackage[font=footnotesize,labelfont={bf,sf},justification=centering]{caption}
\usepackage{fancyhdr} 
\usepackage{microtype}

And the code so far for my table is as follows:

\begin{table*}[t]
\centering
\begin{tabular}{ccc}
\toprule
Treatment & \multicolumn{2}{c}{No. of survivors/total no. of mice (\% survival)} \\
\midrule
& \multicolumn{2}{c}{Combined experiment} \\
& 48 hpi & 72 hpi \\
\midrule
PAO1 only & Data here & Data here\\
\bottomrule
\end{tabular}
\label{micesurvival}
\caption{Caption here}
\end{table*}

Which outputs like this:

table output

As you can see the 48hpi and 72hpi columns and the following data below aren't centred properly beneath the combined experiment multicolumn.

It's probably something obvious I've overlooked..

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Besides using the tabular* environment, you could also use the tabularx environment -- provided by the package with the same name. The X column type provided by the package is quite useful for your purpose, as the following modified form of your MWE shows:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered "X" column type
\begin{document}
\begin{table}
\centering
\begin{tabularx}{0.82\textwidth}{cCC} % 0.82 obtained empirically
\toprule
Treatment & \multicolumn{2}{c}{No. of survivors/total no. of mice (\% survival)} \\
\midrule
& \multicolumn{2}{c}{Combined experiment} \\
\cmidrule{2-3} % additional element
& 48 hpi & 72 hpi \\
\midrule
PAO1 only & Data here & Data here\\
\bottomrule
\end{tabularx}
\caption{Caption here}\label{micesurvival}
\end{table}
\end{document}

Note also that it's important to place the \label command after the \caption command. Incidentally, I've also changed the floating environment from table* to table; using a "starred" table or figure environment is only useful if your document has two (or more) columns per page and you need to have a float that spans all columns.

share|improve this answer
    
Brilliant, this worked perfectly thanks a lot. I changed it back to table* as I'm writing a two column document and made it a little wider for later entries in the table. Thanks again! –  Ben Mar 3 '13 at 22:34
    
@Ben - Glad this works for you. Since you are in fact creating a two-column document, I recommend that you use \textwidth as the width of the float. –  Mico Mar 3 '13 at 22:37
    
green tick stolen by my own package:-) –  David Carlisle Mar 3 '13 at 22:39
    
@DavidCarlisle - tabularx certainly is a great package! –  Mico Mar 3 '13 at 22:47
    
@DavidCarlisle sorry David! It really is a brilliant package though, I will be sure to use it more often. –  Ben Mar 3 '13 at 23:21
add comment

It's a feature of the underlying \halign primitive that if a spanning cell is wider than the cells it spans then all the extra width goes into the last spanned column.

One way to avoid that is to ensure that the natural width of the columns is very wide but shrinkable by adding shrinkable glue:

A second way is to force each of the columns to be half the width of the wide heading as shown in the second table.

enter image description here

\documentclass{article}
\usepackage{booktabs,array}
\begin{document}


\centering

\begin{tabular*}{\linewidth}{
@{\extracolsep{\linewidth minus \linewidth}}
ccc
@{}
}
\toprule
Treatment & \multicolumn{2}{@{}c@{}}{No. of survivors/total no. of mice (\% survival)} \\
\midrule
& \multicolumn{2}{@{}c@{}}{Combined experiment} \\
& 48 hpi & 72 hpi \\
\midrule
PAO1 only & Data here & Data here\\
\bottomrule
\end{tabular*}

\bigskip
\newlength\myl
\settowidth\myl{No. of survivors/total no. of mice (\% survival)}

\begin{tabular}{
c
>{\centering\arraybackslash}p{.5\myl}
>{\centering\arraybackslash}p{.5\myl}
}
\toprule
Treatment & \multicolumn{2}{c}{No. of survivors/total no. of mice (\% survival)} \\
\midrule
& \multicolumn{2}{c}{Combined experiment} \\
& 48 hpi & 72 hpi \\
\midrule
PAO1 only & Data here & Data here\\
\bottomrule
\end{tabular}




\end{document}

Please always post complete documents showing all packages used.

share|improve this answer
    
Thanks for the answer David. Apologies for the lack of full preamble, I've edited my original post to reflect that. –  Ben Mar 3 '13 at 22:10
    
Is there anyway to have the table not span the whole page, and for the columns to be centred as in the original table design? –  Ben Mar 3 '13 at 22:11
    
well to do the shrinkable glue trick (due to Donald Arseneau I believe) you need to give the table some fixed width, it doesn't have to be linewidth though. alternatives are to pad the panned columns with half the title width, I'll put a plan b in teh answer in a bot –  David Carlisle Mar 3 '13 at 22:19
    
@Ben alternative method preserving table width added –  David Carlisle Mar 3 '13 at 22:29
add comment

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.