[Tex/LaTex] merge two tables into one

tables

i have two sperate tables with the same number of rows and labels
as follows:

one:

a  12
b  32 
c  18

two:

a  45 
b  98
c  300

how can i combine those two tables into one?
as follows:

a 12 45
b 32 98
c 18 300

thank
Elisheva

Best Answer

The \pgfplotstablecreatecol command from the pgfplotstable package can be used here to add additional columns to a table when it is created:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
%\pgfplotsset{compat=1.10}

%the following section is just for the example; you can have your actual tables
% in separate external files
\usepackage{filecontents}
\begin{filecontents*}{table1.dat}
name data1
a  12 
b  32 
c  18 
\end{filecontents*}
\begin{filecontents*}{table2.dat}
name data2
a  45 
b  98
c  300
\end{filecontents*}
%%%% end of section %%%%%%%%%%%%%%%

\pgfplotstableread{table1.dat}{\loadedtablei}
\pgfplotstableread{table2.dat}{\loadedtableii}

\pgfplotstablecreatecol[
  copy column from table={\loadedtableii}{[index] 1},
  ]{data2}{\loadedtablei}

\begin{document}

\pgfplotstabletypeset[
  string type,
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=\bottomrule}
]{\loadedtablei}

\end{document}

enter image description here

As I mentioned in the code, the filecontents* section is just for the example; you can have your tables in external separate files, for example, or you can have them in your actual .tex file.

Here's a more real example, explaining step by step the procedure.

  1. Create a file table1.dat like this one:

    Category Valuea
    {Share Jewish}  0.87
    {Share Muslim}  0.05
    {Share other religion}  0.08
    {Mean age}  33.28 
    {Share born in Israel}  0.69
    {Share work}  0.23
    {Share male}  0.51 
    {Share dis\_21}  0.01 
    {Share dis\_18}  0.00 
    {Share dis\_13}  0.00
    
  2. Create a file table2.dat like this one:

    Category Valueb
    {Share Jewish}  0.13
    {Share Muslim}  0.51
    {Share other religion}  0.18
    {Mean age}  23.16 
    {Share born in Israel}  0.29
    {Share work}  0.15
    {Share male}  0.33 
    {Share dis\_21}  0.02 
    {Share dis\_18}  0.01 
    {Share dis\_13}  0.01
    

    Notice that entries with more than one word are grouped using braces. Also I provided some headings for the first row of the merged table.

  3. Save these files in your current working directory (the same cotaining your .tex file).

  4. Your .tex file should have the following aspect:

    \documentclass{article}
    \usepackage{pgfplotstable}
    \usepackage{booktabs}
    %\pgfplotsset{compat=1.10}
    
    % Read table1
    \pgfplotstableread{table1.dat}{\loadedtablei}
    % Read table2
    \pgfplotstableread{table2.dat}{\loadedtableii}
    
    % Create additional column for table1 containing
    % second column from table2
    \pgfplotstablecreatecol[
      copy column from table={\loadedtableii}{[index] 1},
      ]{Valueb}{\loadedtablei}
    
    \begin{document}
    
    % Print the merged table
    \pgfplotstabletypeset[
      string type,
      every head row/.style={before row=\toprule,after row=\midrule},
      every last row/.style={after row=\bottomrule}
    ]{\loadedtablei}
    
    \end{document}
    

Processing the above document yields:

enter image description here

Related Question