0

Suppose I have several tables that are in this format (I will put the example data at the end)

table 1

enter image description here

table 2

enter image description here

As you can see the column variables is the same in both tables.

I am working in overleaf, so I have the tables (1 and 2) as tex files in a folder.

How can I merge this two tables, so I can get this:

Desired output: enter image description here

Is there any function to do this? Because I have a lot of tables, and I find it clumsy to do it "by hand"

Thanks in advance!!

Example data: table 1

\begin{table}[]
\centering
\begin{tabular}{@{}llll@{}}
\toprule
variables & group a & group b & diff \\ \midrule
x         & a11     & b12     & d13  \\
y         & a21     & b22     & d23  \\
z         & a31     & b32     & d33  \\ \bottomrule
\end{tabular}
\end{table}

table 2

\begin{table}[]
\centering
\begin{tabular}{@{}llll@{}}
\toprule
variables & group a & group b & diff \\ \midrule
x         & c11     & f12     & g13  \\
y         & c21     & f22     & g23  \\
z         & c31     & f32     & g33  \\ \bottomrule
\end{tabular}
\end{table}

Desired Output:

\begin{table}[]
\centering
\begin{tabular}{@{}lllllll@{}}
\toprule
variables & group a t2 & group b t2 & diff t2 & group a t1 & group b t1 & diff t1 \\ \midrule
x         & c11        & f12        & g13     & a11        & b12        & c13     \\
y         & c21        & f22        & g23     & a21        & b22        & c23     \\
z         & c31        & f32        & g33     & a31        & b32        & c33     \\ \bottomrule
\end{tabular}
\end{table}
2
  • 1
    I'm afraid that there is no other way than manually edit those table , especially merged tables has different column headers as have individual table. However, you can "merge" tables (again manually), as subtables in one table.
    – Zarko
    Oct 18, 2022 at 21:20
  • @Zarko ): that's sad Oct 18, 2022 at 22:25

1 Answer 1

2

If you're on a unix based system (e.g., Linux; maybe MacOS: I haven't tested it there), you can use the paste command to accomplish this.

First save the "guts" of the tables as files:

t1.txt

variables & group a & group b & diff \\ \midrule
x         & a11     & b12     & d13  \\
y         & a21     & b22     & d23  \\
z         & a31     & b32     & d33  \\ \bottomrule

t2.txt

variables & group a & group b & diff \\ \midrule
x         & c11     & f12     & g13  \\
y         & c21     & f22     & g23  \\
z         & c31     & f32     & g33  \\ \bottomrule

Now run this command in bash or zsh in the terminal:

paste -d '&' <(sed -e 's/\\.*//' -e '1s/&/t2 \&/g' \
    -e '1s/variables t2/variables/' -e '1s/$/t2 /' t2.txt) \     
    <(sed -e 's/^[^&]*&//' -e '1s/&/t1 \&/g' -e 's/\\/t1 \\/' \
    t1.txt) | column -s '&' -o '&' -t

The output is this:

variables & group a t2 & group b t2 & diff t2 & group a t1 & group b t1 & diff t1 \\ \midrule
x         & c11        & f12        & g13     & a11        & b12        & d13  t1 \\
y         & c21        & f22        & g23     & a21        & b22        & d23  t1 \\
z         & c31        & f32        & g33     & a31        & b32        & d33  t1 \\ \bottomrule

Let me break that down.

The paste command merges files that are arranged in the same order, line by line. The -d '&' tells it to use & as the delimiter rather than the default tab.

We want to modify the files before merging, however, so we use the <(...) construction in place of file names, and these will point to the output of the commands inside the parentheses.

Now consider the first one:

sed -e 's/\\.*//' -e '1s/&/t2 \&/g' -e '1s/variables t2/variables/' -e '1s/$/t2 /' t2.txt

The sed commands performs four substutitions on the file t2.txt. The first one 's/\\.*//' chops off the part from \\ onward in each line, because we don't want to duplicate that. The next one '1s/&/t2 \&/g' adds "t2" before each & but only on the first line. Since we don't want "t2" after "variables", the third one 1s/variables t2/variables/ removes that first t2. Finally '1s/$/t2 /' adds a "t2 " at the end of the first line.

The output of this will be merged, line by line with the output of the other one.

sed -e 's/^[^&]*&//' -e '1s/&/t1 \&/g' -e '1s/\\/t1 \\/' t1.txt

Here there are three substitutions made to t1.txt. The first one 's/^[^&]*&//' removes everything up to and including the first & on each line, since we don't want to duplicate the first column which is common to both tables. The second one '1s/&/t1 \&/g' again adds t1 before each & on the first line. And '1s/\\/t1 \\/' puts "t1" in the last column before the \\.

The output of that is already more or less what you want, but I also pipe it into:

column -s '&' -o '&' -t

This will add extra spaces to align all the &s in the output, since the addition of the t1s and t2s in the header line would otherwise make them no longer match up with the rows below.

Depending on exactly what all your tables look like, it might be easier to manually make the additions to the header rows rather than figuring out the sed stuff, and manually remove the duplicated columns, and just run

paste -d '&' t2.txt t1.txt

Which will just merge the contents as is line by line.

Sorry can't help you if you're using Windows (though you could probably do it with the Windows Subsystem for Linux, WSL).

2
  • Thanks!! but i use windows, I gotta do it manually ): Oct 19, 2022 at 2:35
  • I'm sure there are comparable tools on Windows: just don't ask me what they are! If you know even the basics of any programming language, you could write little program to do this fairly easily too.
    – frabjous
    Oct 19, 2022 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .