Graphics, Figures & TablesSame size tables side by side

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
eamrob
Posts: 3
Joined: Fri Jan 13, 2017 5:50 pm

Same size tables side by side

Post by eamrob »

Hello all,

I'm new in this forum and i really need your help.

In fact, i need to put two tables side by side. I dit it but the problem is that the second column of the second table is not centered and i don't understand why.

Thanks for you're help.

here is the table code:

Code: Select all

\begin{table}[!ht]
    \caption{Exons et SCRs correspondants \citep{Perez2001}}
    \centering
    \label{exons_scr}
    \begin{minipage}{.3\linewidth}
      \centering
        \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}|c|c|}
            \hline
Exon & SCR \\ 
\hline 
1 & Peptide signal \\ 
\hline 
2 & SCR1 \\ 
\hline 
3 & SCR2A \\ 
\hline 
4 & SCR2B \\ 
\hline 
5 & SCR3 \\ 
\hline 
6 & SCR4 \\ 
\hline 
7 & SCR5 \\ 
\hline
8 & SCR6 \\ 
\hline
9 & SCR7 \\ 
\hline
11 & SCR8 \\ 
\hline
12 & SCR9 \\ 
\hline
        \end{tabular*}
    \end{minipage}%
    \hspace{1cm}
    \begin{minipage}{.3\linewidth}
      \centering
        \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}|c|c|}     
            \hline
Exon & SCR \\ 
\hline 
13 & SCR10 \\ 
\hline
14 & SCR11 \\ 
\hline
15 & SCR12 \\ 
\hline
16 & SCR13 \\ 
\hline
17 & SCR14 \\ 
\hline
18 & SCR15 \\ 
\hline
19 & SCR16 \\ 
\hline
20 & SCR17 \\ 
\hline
21 & SCR18 \\ 
\hline
22 & SCR19 \\ 
\hline
23 & SCR20 \\ 
\hline 
        \end{tabular*}
    \end{minipage} 
\end{table}
and here is the result:
Table.png
Table.png (45.81 KiB) Viewed 7869 times
Last edited by Stefan Kottwitz on Fri Jan 13, 2017 6:25 pm, edited 2 times 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

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Same size tables side by side

Post by Stefan Kottwitz »

Hi eamrob,

welcome to the forum!

That extra flexible space pushes the content to the right. I would use tabularx. That package provides stretchable columns too, up to a selectable table width.

\usepackage{tabularx}

The table could then be like

Code: Select all

\begin{tabularx}{\linewidth}{*2{|>{\centering\arraybackslash}X}|
...
\end{tabularx}
Stefan
LaTeX.org admin
eamrob
Posts: 3
Joined: Fri Jan 13, 2017 5:50 pm

Same size tables side by side

Post by eamrob »

Hi Stefan,

Thanks for your reply.

I used your solution, the problem now is that the tables are one under the other. (See picture).
I forget to say that it's the same table (same caption) that i divided in two sub tables for a best fit.

Here is the latex code :

\usepackage{tabularx}

Code: Select all

\begin{table}[!ht]
\centering
\caption{test}
\begin{tabularx}{.5\linewidth}{*2{|>{\centering\arraybackslash}X}|}
 \hline
Exon & SCR \\ 
\hline 
1 & Peptide signal \\ 
\hline 
2 & SCR1 \\ 
\hline 
3 & SCR2A \\ 
\hline 
4 & SCR2B \\ 
\hline 
5 & SCR3 \\ 
\hline 
6 & SCR4 \\ 
\hline 
7 & SCR5 \\ 
\hline
8 & SCR6 \\ 
\hline
9 & SCR7 \\ 
\hline
11 & SCR8 \\ 
\hline
12 & SCR9 \\ 
\hline
\end{tabularx}
\hspace{1cm}
\begin{tabularx}{.5\linewidth}{*2{|>{\centering\arraybackslash}X}|}
\hline
Exon & SCR \\ 
\hline 
13 & SCR10 \\ 
\hline
14 & SCR11 \\ 
\hline
15 & SCR12 \\ 
\hline
16 & SCR13 \\ 
\hline
17 & SCR14 \\ 
\hline
18 & SCR15 \\ 
\hline
19 & SCR16 \\ 
\hline
20 & SCR17 \\ 
\hline
21 & SCR18 \\ 
\hline
22 & SCR19 \\ 
\hline
23 & SCR20 \\ 
\hline 
\end{tabularx}
\end{table}
can you please review it for me ? I also tried

Code: Select all

\begin{tabularx}[t]
in the first tabular but i have this error: (log file)
! Missing number, treated as zero.
<to be read again>
}
l.227 \end{tabularx}

A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Illegal unit of measure (pt inserted).
<to be read again>
}
l.227 \end{tabularx}
http://www.filedropper.com/rapport_3 ==> Log file here

Imagehebergeur dimage
thanks !
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Same size tables side by side

Post by rais »

Hi eamrob,
eamrob wrote:the problem now is that the tables are one under the other. (See picture).

unlike your initial sub tables (using the minipages) with 0.3\linewidth each, you're now trying to put two sub tables with half the linewidth each, plus something in-between. Small wonder, really, that LaTeX breaks at this `something in-between'...
eamrob wrote:

Code: Select all

\end{tabularx}
\hspace{1cm}
\begin{tabularx}{.5\linewidth}{*2{|>{\centering\arraybackslash}X}|}
might even try to put more than the 1 cm from \hspace beween the sub tables: starting a new line between two somethings is like telling LaTeX you want a (horizontal) space between those two.
eamrob wrote: I also tried

Code: Select all

\begin{tabularx}[t]
in the first tabular but i have this error: (log file)
! Missing number, treated as zero.
you must not omit the width for the {tabularx} environment (nor the column specification). With {tabularx}, the optional position argument goes between the two mandatory arguments:

Code: Select all

\documentclass{article}
\usepackage{tabularx}
\usepackage{showframe}
\newlength\subtabdist
\setlength\subtabdist{1cm}
\begin{document}
\begin{table}[!ht]
\centering
\caption{test}
\begin{tabularx}{\dimexpr.5\linewidth-0.5\subtabdist}[t]{*2{|>{\centering\arraybackslash}X}|}
 \hline
Exon & SCR \\ 
\hline 
1 & Peptide signal \\ 
\hline 
2 & SCR1 \\ 
\hline 
3 & SCR2A \\ 
\hline 
\end{tabularx}%
\hfill%
\begin{tabularx}{\dimexpr.5\linewidth-0.5\subtabdist}[t]{*2{|>{\centering\arraybackslash}X}|}
\hline
Exon & SCR \\ 
\hline 
13 & SCR10 \\ 
\hline
14 & SCR11 \\ 
\hline
\end{tabularx}
\end{table}
\end{document}
KR
Rainer
eamrob
Posts: 3
Joined: Fri Jan 13, 2017 5:50 pm

Same size tables side by side

Post by eamrob »

Hi Rainer,

it works like a charm ! Thanks a lot !

Thank you Stefan for your help !

Have a nice week end guys !

KR
Eam!
Post Reply