4

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

Torbjørn T.
  • 206,688

1 Answers1

5

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

Gonzalo Medina
  • 505,128
  • @elishevaschwarz You're welcome! Don't forget that you can accept the answer (if you consider it solved your problem) by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. – Gonzalo Medina Mar 20 '14 at 23:53
  • when i do what you have told me, i got the massage: "latex error: can be used only in preamble" do you know what the meaning is, and what i have to change? – elisheva schwarz Mar 20 '14 at 23:57
  • @elishevaschwarz Do you get that error with the same exact code I posted or with some of your own code? – Gonzalo Medina Mar 20 '14 at 23:58
  • i think i did the same, instead of the examle you gave me i worte "input{table1.tex}" and in the second "input{table2.rex}" after that i put it in my "big" article, again with "input{eli.tex}" – elisheva schwarz Mar 21 '14 at 00:24
  • @elishevaschwarz It all depends on what exactly is on table1.tex and table2.tex (raw data as in the example in your question or already formatted tabulars?). Also notice that you don't use \input to include (and merge) your tables. You use the commands I described in my answer. – Gonzalo Medina Mar 21 '14 at 00:33
  • sorry for all of this...i got the massage "package pgfplots error: input 'table.dat' has an unbalanced number of cokumns in row '1' (expected '2' cols; got '1')" – elisheva schwarz Mar 21 '14 at 00:34
  • @elishevaschwarz No problem. I will need to see (if possible) the actual files you are using. Can you upload elsewhere (pastebin, for example) the files with the information for the tables and provide a link in your next comment? – Gonzalo Medina Mar 21 '14 at 00:37
  • so how can i include my tables without the command "input"? my table1.tex look like this: \hspace{0.5cm}Share Jewish& 0.87\ \hspace{0.5cm}Share Muslim& 0.05\ \hspace{0.5cm}Share other religion& 0.08\ \hspace{0.5cm}Mean age& 33.28\ \hspace{0.5cm}Share born in Israel& 0.69\ \hspace{0.5cm}Share work& 0.23\ \hspace{0.5cm}Share male& 0.51\ \hspace{0.5cm}Share dis_21& 0.01\ \hspace{0.5cm}Share dis_18& 0.00\ \hspace{0.5cm}Share dis_13& 0.00\ and the same for table2.tex – elisheva schwarz Mar 21 '14 at 00:39
  • @elishevaschwarz As you can see from my example, pgfplots takes charge of including your tables and merging them. Let me produce some code for you showing you how to proceed. Unfortunately, it's dinner time here, so I will only be able to give you the code later (one hour or so). I'll write another message when the code is ready. – Gonzalo Medina Mar 21 '14 at 00:43
  • @elishevaschwarz just to be sure, you want to merge the second column of table2 into table1 right? – Gonzalo Medina Mar 21 '14 at 00:44
  • yes, and i really dont know ihow to thank you..tell me if there any way to do that:) here (israel) is midnight so you can take your time, i will see it tomorrow morning...appetite and thank again! – elisheva schwarz Mar 21 '14 at 00:49
  • @elishevaschwarz You're very kind! A "thank you" and an accepted answer (but only if my answer solves your problem) are more than enough :) I updated my answer with the code required to produce the merged table and step by step instructions on how to proceed. Please let me know if things now work as expected. – Gonzalo Medina Mar 21 '14 at 01:41
  • good morning! do you mean that i should chane my files by myself? i ask because i get them as they look like automatically from stata..if this is the only way it is good enough, i will try it and let you know..thank again! – elisheva schwarz Mar 21 '14 at 07:30
  • @elishevaschwarz Good morning to you too! I think that you can also work with the original formatting for the tables (with the ampersands and the line changing commands (I am not totally sure about the \hspaces)) making some modifications to my code. I would need to do some tests. I'll let you know the results. – Gonzalo Medina Mar 21 '14 at 12:30