Graphics, Figures & TablesTabularx and multicolumn problem

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
timtico
Posts: 6
Joined: Wed Sep 01, 2010 6:59 pm

Tabularx and multicolumn problem

Post by timtico »

Hello people,

I've been spending hours to get this table working, but without luck. Perhaps I don't fully understand the concept of \multicolumn.

I would like to have a 5 column table. The first column contains long text and therefore is set to variable width. The other columns have very short text (one character) and should all have the same width.

I want three multicolumns defined, the first one spans the first column with variable width, the second spans column 2-3 and the last spans column 4-5. The labels of the last two multicolumn should be equally sized (as they span 4 equally sized columns)

Anyway, here is my minimal working example...

MWE:

Code: Select all

\documentclass[11pt]{article}
\title{Table testing}
\author{Tim}
\date{oktober 2010}
\usepackage{setspace} %setting interline spacing
\onehalfspacing
\usepackage{geometry}
\geometry{a4paper}
\usepackage{booktabs}
\usepackage{ctable} %provides good options for footnotes 
\usepackage{paralist}
\usepackage{floatrow}
\floatsetup[table]{style=plaintop, captionskip = 4pt,footnoterule =none, footskip=-10pt}
\usepackage{tabularx}

\begin{document}
\begin{table}[htbp]
\begin{tabularx}{\textwidth}{ lXXXX }
\multicolumn{1}{c}{labelcolumn1}\multicolumn{2}{c}{label column 2-3}\multicolumn{2}{c}{label column 3-4}\\
\hline
  longtextlongtextlongtextlxt & item 1 & item 2 & item 3 & item 4 \\
  strain 2  & item 1  & item 2  & item 3 & item 4 \\
  strain 2  & item 1  & item 2  & item 3 & item 4 \\
  strain 2  & item 1  & item 2  & item 3 & item 4 \\
  strain 2  & item 1  & item 2  & item 3 & item 4 \\
  strain 2  & item 1  & item 2  & item 3 & item 4 \\
\hline
\end{tabularx}
\end{table}
\end{document}
As you can see when running the code the multicolumns are not placed at the right position, instead they span the first few columns... What am I doing wrong? I've found no good examples of using multicolumns in the way i want (also checked the tabularx manual) Any help is very much appreciated.

Recommended reading 2024:

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

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

gnramos
Posts: 3
Joined: Tue Oct 26, 2010 7:43 pm

Re: Tabularx and multicolumn problem

Post by gnramos »

try adding column separators between your spanning columns.
timtico
Posts: 6
Joined: Wed Sep 01, 2010 6:59 pm

Tabularx and multicolumn problem

Post by timtico »

Thanks alot! That was quite silly of me to forget those.

I've been spending some more time working on getting the table perfect, and I'm almost there... I say almost since I think that the spanning column text is not fully centered. Aswell... when i decrease the size for my table to for example

Code: Select all

0.5\textwidth 
using the tabularx preamble, then the booktabs rulers are not fully made while the rest of the text is typeset perfectly.. any suggestions anyone?

Code: Select all

\documentclass[11pt]{article}

\title{Table testing}
\author{Tim Homan}
\date{oktober 2010}

\usepackage{setspace} %setting interline spacing
\onehalfspacing

\usepackage{geometry}
\geometry{a4paper}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{ctable} %provides good options for footnotes on tables\captionsetup[table]{singlelinecheck = false,margin=0pt, skip=10pt, position = below,format = default, }\usepackage{booktabs}
\usepackage{paralist}
\usepackage{floatrow}
\floatsetup[table]{style=plaintop, captionskip = 4pt,footnoterule =none, footskip=-10pt}
\usepackage{graphicx}
\usepackage[nearskip = 0pt, farskip = 6pt]{subfig}
\usepackage{tabularx}
\captionsetup{labelfont={bf,sf}, labelsep=period, format = plain, singlelinecheck = false, textfont={rm}}
\captionsetup[table]{format = default, singlelinecheck = false, figurewithin=section, justification =raggedright, indention=2em, skip = 4pt}
%\captionsetup[table]{singlelinecheck = false,margin=0pt, skip=10pt, position = below,format = default}

\newcommand{\celsius}{$^\circ$C~} %degrees Celsius
\newcommand{\microliter}{~$\mu$l~} %microliter
\begin{document}
\begin{table}[htbp]
\begin{floatrow}
\ttabbox
{\caption{Genetic screening selection conditions}\label{table:2}}
{\begin{tabularx}{1\textwidth}{@{\extracolsep{\fill}}lCCCC}
\toprule
\multicolumn{1}{}&\multicolumn{2}{C}{MM-Acetamide}&\multicolumn{2}{C}{MM-Fluoroacetamide}\\
\cmidrule(r){2-3}\cmidrule(lr){4-5}
\multicolumn{1}{C}{strain}&GA&Glucose&GA&Glucose\\
\midrule 
x&-&-&+&+\\
xxxxxxxxxxxxx&+&-&-&+\\
xxxxxx&-&-&+&+\\
\bottomrule
\end{tabularx}}
\end{floatrow}
\end{table}
\end{document}
gravityflyer
Posts: 1
Joined: Fri Nov 12, 2010 7:59 am

Tabularx and multicolumn problem

Post by gravityflyer »

Hi all,

I tried to input the above min working example but to no avail. Regardless, I sense I am encountering a similar problem. You'll note that the last columns (specifically #'s 4 and 8) are stretched due to the multicolumn text. I tried switching to both tabular* and tabularx but couldn't resolve this issue in a clean manner. Any suggestions would be greatly appreciated!

Code: Select all

\documentclass{article}
\usepackage[margin=1in]{geometry} %Allows for 1in margins.
\usepackage{multirow} %Allows for multiple rows in tables.
\usepackage{booktabs} %Allows for different line widths in tabular
\begin{document}
\begin{center}
\begin{tabular}{c c c c c c c c}
\toprule
 & \multicolumn{3}{c}{\textbf{GDP (millions of US dollars)}} & & \multicolumn{3}{c}{\textbf{Per Capita GDP (millions of US dollars)}}\\
\cmidrule{2-4} \cmidrule{6-8}
\textbf{Colony} & \textbf{1962} & \textbf{1965} & \textbf{1970} & & \textbf{1962} & \textbf{1965} & \textbf{1970}\\ 
One & 803.7 & 1,099.0 & 1,888.5 & & 161 & 210 & 333\\
\bottomrule
\end{tabular}
\end{center}
\end{document}
jaykemper
Posts: 22
Joined: Wed Apr 15, 2009 12:11 am

Re: Tabularx and multicolumn problem

Post by jaykemper »

My question is along this line, but more general, I think.

I just want to have a cell span across a tabularx. In the documentation for tabularx, it says not to use multicolumn across an X column (section 4.3, page 3), but in the very first example (section 2, page 1), it clearly shows a multicolumn cell spanning across an X column. They never explain how it was accomplished! Does anyone know the secret, or know where the latex code for the tabularx documentation is located?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Tabularx and multicolumn problem

Post by Stefan Kottwitz »

Hi,
jaykemper wrote:Does anyone know the secret, or know where the latex code for the tabularx documentation is located?
the source code of tabularx.pdf is contained in tabularx.dtx.

Stefan
LaTeX.org admin
Post Reply