Graphics, Figures & TablesSplit Columns in a Table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sw3quant
Posts: 76
Joined: Tue Nov 02, 2010 11:40 pm

Split Columns in a Table

Post by sw3quant »

I have a table which I construct as following (This is MWE):

Code: Select all

\documentclass[final,authoryear,3p,times]{elsarticle}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{prettyref}
\usepackage{url}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{nicefrac}
\usepackage{capt-of}
\usepackage{float}
\usepackage{xspace}
\usepackage{verbatim}
\usepackage{mathdots}
\usepackage{bm}
\usepackage{amsmath,amssymb}
\usepackage{array}
\newcommand{\head}[1]{\textbf{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{rotating} %table in landscape
\usepackage{color}
\usepackage{array}
\usepackage{longtable}
\usepackage{calc}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{ifthen}
\usepackage{lscape}


\begin{document}



\begin{sidewaystable}
\begin{center}
\captionof{table}{\bf my Table}
\footnotesize
\begin{tabular}{|C{0.6cm}|C{2.0cm}|C{2.0cm}|C{2.0cm}|C{2.0cm}|C{2.0cm}|C{2.0cm}|C{2.0cm}|}
  \hline
\textbf{Code}   & \head{Number of ohh (\# [\%])} &  \head{AHHH (P [\%])} &  \head{Mean WAAA (lots)} & \head{Number of PLOP (\# [\%])} &  \head{OHH Volume (G [\%])} & \head{Mean WAA (Y)} & \head{Fleeting Z (\%)} \\
  \hline \hline
  OR & X & X & X & X&  X & X  & X \\
  \hline
  SP & X & X & X & X&  X & X & X  \\
  \hline
  BF & X & X & X & X&  X & X & X  \\
  \hline
  PK & X & X & X & X&  X & X & X  \\
  \hline
  FB & X & X & X & X&  X & X & X  \\
  \hline
  CF & X & X & X & X&  X & X & X  \\
  \hline
  BS & X & X & X & X&  X & X & X  \\
  \hline
  DF & X & X & X & X&  X & X & X \\
  \hline
  MP & X & X & X & X&  X & X & X  \\
  \hline
  PB & X & X & X & X&  X & X & X \\
  \hline
  PS & X & X & X & X&  X & X & X  \\
  \hline
  Total & X [100.00] & X [100.00] & X & X [100.00] &  X & X & X \\
  \hline
\end{tabular}
\label{hi}
\end{center}
\end{sidewaystable}




\end{document} 
and it gives me a nice looking table, as per the attached pic. great.

However I now wish to split each col into two. I will use the first col as an example.

I wish the top row "Number of Ohh ()" to span two cols. Then a new row underneath that, which is 2 cells wide, with the names col A and colB.

Hence each existing col header cell is merged (in EXCEL language) and becomes the merged header for 2 new cols : col A and col B.

I have posted an attempt to show this.! :oops:

Having looked around on the web, I havent a clue how to do this. Can anyone help?

Thank you!
Attachments
tab2-800.jpg
tab2-800.jpg (38.33 KiB) Viewed 18249 times
tab-800.jpg
tab-800.jpg (33.45 KiB) Viewed 18249 times
Last edited by cgnieder on Tue Nov 27, 2012 9:11 pm, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Split Columns in a Table

Post by cgnieder »

I guess you're looking for \multicolumn{<num of cols>}{<col specs>}{<cell contents>} that can be used to merge <num of cols> cells into one cell. Here is an example:

Code: Select all

\documentclass{article}
\usepackage{array}

\begin{document}

\begin{tabular}{|c|c|c|}
 \hline
  \multicolumn{2}{|c|}{Head 1} & Head 2 \\
 \hline
  a & b & c \\
 \hline
  a & b & c \\
 \hline
\end{tabular}

\end{document}
Regards
site moderator & package author
Post Reply