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 am asking for advice on achieving two separate effects in the table environment.

(1) I have two tables with a different number of columns. I need to merge them into a single table.

I have this

\begin{table}[ht]
\small
\centering

\begin{tabular}{|l|c|l|c|}
\hline
    parameter & value & parameter & value \\
\hline
    blah blah & a & blah blah & 2 \\
    blah blah & 4 & blah blah   & 17 \\             
    blah blah & 4 & blah blah & 4 \\
    blah blah & 4 & blah blah & 128  \\
    blah blah & 1 & blah blah &  6 \\
    blah blah & 8 & blah blah & 48 \\
    blah blah & 48 & blah blah & 32 \\ 
    blah blah & 10 & blah blah & 16 \\
\hline
\end{tabular}

\caption{My table 1}
\end{table}

\begin{table}[ht]
\small
\centering
\begin{tabular}{|l|l|l|l|l|l|}
\hline
        blah & bloo & bleeblee & bloo bloo & meeh & hoohaa  \\ \hline
        xyz & 3ubfjdf & 14 & 64 & 4 & 444 \\ 
        abc & fddf4 & 44 & 64 & 8 & 555 \\ 
        mno & dsf4tv & 100 & 64 & 8 & 777 \\
\hline
\end{tabular}
\caption{My even better table 2}
\label{tab:cacheparams}
\end{table}

two tables

I want to join the two tables together. I've tried this

\begin{table}[ht]
\small
\centering

\begin{tabular}{|l|c|l|c|}
\hline
    parameter & value & parameter & value \\
\hline
    blah blah & a & blah blah & 2 \\
    blah blah & 4 & blah blah   & 17 \\             
    blah blah & 4 & blah blah & 4 \\
    blah blah & 4 & blah blah & 128  \\
    blah blah & 1 & blah blah &  6 \\
    blah blah & 8 & blah blah & 48 \\
    blah blah & 48 & blah blah & 32 \\ 
    blah blah & 10 & blah blah & 16 \\
\hline
\end{tabular}
\begin{tabular}{|l|l|l|l|l|l|}
\hline
        blah & bloo & bleeblee & bloo bloo & meeh & hoohaa  \\ \hline
        xyz & 3ubfjdf & 14 & 64 & 4 & 444 \\ 
        abc & fddf4 & 44 & 64 & 8 & 555 \\ 
        mno & dsf4tv & 100 & 64 & 8 & 777 \\
\hline
\end{tabular}
\caption{My merged table}
\end{table}

But it produces this

Table that I get

However I want this (notice the lack of white space between the two tabulars).

Table that I would like

(2) Once I have these two tabulars correctly positioned, I would like to make them both equal widths. Preferably the width of the column. I've tried using tabularx and using |X|c|l|c| which is so-so, but it puts too much whitespace in the first column. I would like the whitespace distributed over all of the columns.

Any way to achieve these things?

share|improve this question
    
What about the two captions? –  Bernard Apr 15 at 14:45

4 Answers 4

up vote 5 down vote accepted

Here's one option using tabularx:

\documentclass{book}
\usepackage{tabularx}

\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}
\small
\centering
\begin{tabularx}{.7\linewidth}{|X|C|X|C|}
\hline
    parameter & value & parameter & value \\
\hline
    blah blah & a & blah blah & 2 \\
    blah blah & 4 & blah blah   & 17 \\             
    blah blah & 4 & blah blah & 4 \\
    blah blah & 4 & blah blah & 128  \\
    blah blah & 1 & blah blah &  6 \\
    blah blah & 8 & blah blah & 48 \\
    blah blah & 48 & blah blah & 32 \\ 
    blah blah & 10 & blah blah & 16 \\
\hline
\end{tabularx}\par\vskip-1.4pt
\begin{tabularx}{.7\linewidth}{*{6}{|X}|}
\hline
        blah & bloo & bleeblee & bloo bloo & meeh & hoohaa  \\ \hline
        xyz & 3ubfjdf & 14 & 64 & 4 & 444 \\ 
        abc & fddf4 & 44 & 64 & 8 & 555 \\ 
        mno & dsf4tv & 100 & 64 & 8 & 777 \\
\hline
\end{tabularx}
\caption{My merged table}
\end{table}

\end{document}

enter image description here

share|improve this answer
    
Perfect, exactly what I wanted. –  hayesti Apr 15 at 14:57

You can simply use the standard command multicolumn to achieve this. Then you choose which of the columns of the top tables should span more than one column of the lower table. Then do this in each row of the top table.

\begin{tabular}{|l|l|l|l|l|l|}
\hline
 \multicolumn{2}{|l|}{  parameter } & value & \multicolumn{2}{l|}{  parameter } & value \\
\hline
 \multicolumn{2}{|l|}{ blah blah }&  a & \multicolumn{2}{l|}{ blah blah } &  2 \\
 \multicolumn{2}{|l|}{ blah blah }&  4 & \multicolumn{2}{l|}{ blah blah } &  17 \\             
 \multicolumn{2}{|l|}{ blah blah }&  4 & \multicolumn{2}{l|}{ blah blah } &  4 \\
 \multicolumn{2}{|l|}{ blah blah }&  4 & \multicolumn{2}{l|}{ blah blah } &  128  \\
 \multicolumn{2}{|l|}{ blah blah }&  1 & \multicolumn{2}{l|}{ blah blah } &   6 \\
 \multicolumn{2}{|l|}{ blah blah }&  8 & \multicolumn{2}{l|}{ blah blah } &  48 \\
 \multicolumn{2}{|l|}{ blah blah }& 48 & \multicolumn{2}{l|}{ blah blah } &  32 \\ 
 \multicolumn{2}{|l|}{ blah blah }& 10 & \multicolumn{2}{l|}{ blah blah } &  16 \\
\hline
    blah & bloo & bleeblee & bloo bloo & meeh & hoohaa  \\ \hline
    xyz & 3ubfjdf & 14 & 64 & 4 & 444 \\ 
    abc & fddf4 & 44 & 64 & 8 & 555 \\ 
    mno & dsf4tv & 100 & 64 & 8 & 777 \\
\hline
\end{tabular}
\caption{My merged table}
\end{table}

It looks like: enter image description here

The advantages of this approach is that

  • it uses standard commands
  • it's versatile, you can easily use other columns and combine more than two tabulars.
  • it doesn't rely on spacings and other values that could change

The disadvantage is of course it must be done manually. Which can be bothersome. But this is unfortunately quite often the case for tables in LaTeX. (But when it is complicated to produce them it will also be complicated to read them. This can be a hint, that you should think of another way to show your data.)

I hope this answers your question.

share|improve this answer
    
Thanks. It's a clever approach. I chose the answer of gonzalo-medina as it doesn't require me to force columns from the second table to conform with the widths of those from the first table. I appreciate the reply though. –  hayesti Apr 15 at 15:11
    
I may add, that if you do not have columns that conform within one table, you should really consider using two tables. But I know sometimes, there are reasons around we don't know. –  Philipp Apr 15 at 15:18
    
I absolutely would love to use two tables, unfortunately space restrictions for an article submission is forcing me to be ruthless with every bit of whitespace. –  hayesti Apr 15 at 15:21

A first trial without equal widths, but I am not sure whether I understand the request correctly... ;-)

\documentclass{scrbook}

\begin{document}

\begin{table}[ht]
\small
\centering

\begin{tabular}{l|l|c|l|c|l}
\cline{2-5} 
&    parameter & value & parameter & value \\
\cline{2-5} 
 &   blah blah & a & blah blah & 2 \\
 &   blah blah & 4 & blah blah   & 17 \\             
 &   blah blah & 4 & blah blah & 4 \\
 &   blah blah & 4 & blah blah & 128  \\
 &   blah blah & 1 & blah blah &  6 \\
 &   blah blah & 8 & blah blah & 48 \\
 &   blah blah & 48 & blah blah & 32 \\ 
 &   blah blah & 10 & blah blah & 16 \\
\hline
\multicolumn{1}{|l|}{        blah} & bloo & bleeblee & bloo bloo & meeh & \multicolumn{1}{|l|}{hoohaa}  \\ \hline
\multicolumn{1}{|l|}{        xyz} & 3ubfjdf & 14 & 64 & 4 & \multicolumn{1}{|l|}{444} \\ 
\multicolumn{1}{|l|}{        abc} & fddf4 & 44 & 64 & 8 & \multicolumn{1}{|l|}{555} \\ 
\multicolumn{1}{|l|}{        mno} & dsf4tv & 100 & 64 & 8 & \multicolumn{1}{|l|}{777} \\
\hline
\end{tabular}
\caption{My merged table}
\end{table}

\end{document}

enter image description here

share|improve this answer
    
Thank you, it is clever approach to solve the first request. –  hayesti Apr 15 at 15:27

Another layout with different types of "X" columns, and a better vertical spacing with the help of the cellspace package:

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{graphicx}%[draft]
\usepackage{array, booktabs, multirow}
\usepackage{tabularx}
\newcolumntype{Y}{ >{\centering\arraybackslash\hsize =0.7165\hsize}X}
\newcolumntype{Z}{ >{\centering\arraybackslash\hsize =0.2835\hsize}X}
\renewcommand{\tabularxcolumn}[1]{ >{\small\centering\arraybackslash}m{#1}}

\usepackage{cellspace} 
\renewcommand{\cellspacetoplimit}{3pt}
\renewcommand{\cellspacebottomlimit}{3pt}
\addparagraphcolumntypes{X, Y, Z}

\begin{document}

\begin{minipage}{0.6\linewidth}
\small
\centering
\begin{tabularx}{\linewidth}{|*{2}{S{Y}|S{Z}|}}
\hline
    parameter & value & parameter & value \\
\hline
    blah blah & a & blah blah & 2 \\
    blah blah & 4 & blah blah   & 17 \\
    blah blah & 4 & blah blah & 4 \\
    blah blah & 4 & blah blah & 128  \\
    blah blah & 1 & blah blah &  6 \\
    blah blah & 8 & blah blah & 48 \\
    blah blah & 48 & blah blah & 32 \\
    blah blah & 10 & blah blah & 16 \\
\hline
\end{tabularx}\\[-0.25ex]
%%
\begin{tabularx}{\linewidth}{|*{6}{S{X} |}}
         blah  &  bloo &  blee  blee  & bloo bloo & meeh & hoo haa   \\
        \hline
        xyz & 3ubf jdf & 14 & 64 & 4 & 444 \\
        abc & fdd f4 & 44 & 64 & 8 & 555 \\
        mno & dsf 4tv & 100 & 64 & 8 & 777 \\
\hline
\end{tabularx}
\end{minipage}

\end{document} 

enter image description here

share|improve this answer

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.