Graphics, Figures & TablesCenter columns inside a tabular

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Center columns inside a tabular

Post by ptrcao »

I need the second two columns to be centered.

I also need someone to please help me clean up this table. It looks ugly.

Notice it is inside a multicolumn (2-column) environment, because the context is a scientific report.

It is complex because it involves the interaction of several packages like tabularx, multicolumn and multirow.

I tried to put it inside a table environment but this introduces float issues. I want to to be floated exactly at the location in which it takes place in the document. If need be, it should continue spill over into the next column or over the page.

Min. example:

Code: Select all

\documentclass[a4paper,12pt]{article}

\author{Peter Cao}
\date{}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption} %apparently needs to come first or options clash with other packages will occur

\usepackage[x11names,dvipsnames]{xcolor} %for use in color links



\usepackage[hyphens]{url}
\usepackage[colorlinks=true,linkcolor=Blue4,citecolor=blue]{hyperref}
\usepackage[hyphenbreaks]{breakurl}
\usepackage{a4wide}
\usepackage{graphicx}

\usepackage[version=3]{mhchem}

\usepackage[T1]{fontenc} %for > and < in text mode
\usepackage{tikz}
\mhchemoptions{arrows=pgf-filled}

\usepackage{booktabs} %for top, middle and bottomline

\usepackage{multirow} %multi column and row spanning

\usepackage{amsmath}


\usepackage{cite}

\usepackage{multirow}
\usepackage{tabularx}



\usepackage{siunitx}

\usepackage{fancyhdr}
%\usepackage{fancyheadings} seems to be obsoleted by fancyhdr

\usepackage{comment}
\usepackage{multicol}

\usepackage{lastpage}

\newcommand{\ignore}[1]{} %a null macro which gobbles up comments, and thus acts as a tool for in-line commenting.


\begin{document}

\begin{multicols}{2}

\subsection*{Results}

{\small
\begin{tabularx}{\columnwidth}{X | X | X}
\toprule
Characteristic & \multicolumn{2}{c}{Result}\\
 \cmidrule(l){2-3}
 & Seaweed isolate & Coral isolate \\
\cmidrule(r){1-1} \cmidrule(l){2-3}
Cell shape & Rod & Rod\\
Gram stain & - & -\\
Oxidase & + & -\\
Catalase & + & -\\
MSA & Growth & No growth\\
Anaerobic & Growth (weak) & No growth\\
Motility & & \\
Indole production & & \\
Hugh \& Leifsons & & \\
\bottomrule
\end{tabularx}
}

\end{multicols}

\end{document}
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Center columns inside a tabular

Post by Stefan Kottwitz »

Hi Peter,

you could define a centered X column like this:

Code: Select all

\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
Stefan
LaTeX.org admin
ptrcao
Posts: 88
Joined: Sun Dec 05, 2010 3:26 am

Center columns inside a tabular

Post by ptrcao »

Stefan_K wrote:Hi Peter,

you could define a centered X column like this:

Code: Select all

\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
Stefan
I'm not sure why but it didn't work - wasn't able to center-align the contents of the column? Did it work for you?

Could it be because my tabular is not inside a table environment? I tried to put it inside a table environment but that introduces float issues in a 2-column page layout.
OS info: Win XP | Debian 6.0 | Ubuntu 10.04
Latex editor of choice: TexWorks (for dual view feature)
Post Reply