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

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – jubobs Mar 20 '14 at 11:11
  • Your question is somewhat ambiguous. Do you mean to say that the "one:" and "two:" are in external files as text, and you would like to create a LaTeX table that looks like the third example? If that were so, then something like this: tex.stackexchange.com/questions/117747/… might be useful. – Steven B. Segletes Mar 20 '14 at 11:22
  • i have to seperate latex's file table, i am not sure that the link you gave help me, or i am worng..? – elisheva schwarz Mar 20 '14 at 11:40
  • 4
    @elishevaschwarz your question is still far from clear. What form are your two tables? As you show with space separated values? Or are they marked up as latex tabular already? – David Carlisle Mar 20 '14 at 12:01
  • i have to confess that i am just a bigginer.. i add Screenshot of the two files – elisheva schwarz Mar 20 '14 at 12:11
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

  • @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 0: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 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.