# align columns in multiple tables

I am trying to align the columns of multiple tables. I know I can use p column format to fix column widths but I would prefer that TeX automatically figure out the widths for me. I was hoping to:

1. output one table with all rows of each subsequent table so that the column widths of the combined table would be automatically good for all the subsequent tables
2. then output each subsequent table using the same settings and
3. figure out some way in which the first combined table could be generated but not visible.

Here is my attempt. The first combined table uses \savetabu to save the settings and then each subsequent table uses \usetabu to retrieve them. Unfortunately this does not work as I had hoped. The columns do not line up as the output below (after the code) shows. Does anyone have a method of accomplishing this?

\documentclass{article}

\usepackage{tabu}
\begin{document}

Output both tables together as one table and then output each
individual table.  We had hoped that this would cause the subsequent
tables to be the same width as the first since the first contains all
columns of the subsequent tables but it seems that that does not work.

\begin{center}
\begin{tabu}{ll}\savetabu{mytabu2}
1 & 2 \\
3 & 4 \\
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
1 & 2 \\
3 & 4 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\end{document}


Here is the output:

-
Please provide more information about the data types that are likely to be encountered in the tables. E.g., will the left-most column always contain text and the remaining columns always contain numeric data? If so, you could indeed use a p{<len>} specifier for the first column; for the numeric columns you might use the S column type of the siunitx package, possibly with a prespecified table-format proviso. Without more specific information about the broad structure of the table(s), it's going to be quite difficult to provide specific advice. –  Mico Jul 16 '12 at 18:24
@Mico, This is not just for a specific instance but I am looking at this as a general technique that I may use in multiple places. If it worked for left justified text fields that would be a good start though. –  user1189687 Jul 16 '12 at 18:39
Thanks for providing this clarification. I guess the point I was trying make (rather unsuccessfully, I'm afraid) is that without some more-detailed information about what the various tables may have in common it's going to be well-nigh impossible to give specific recommendations. –  Mico Jul 16 '12 at 18:47

What you describe is basically what longtable does internally so that the various parts of the table line up, so it is fairly easy to use longtable but redefine things so the two tables share longtable width data:

\documentclass{article}

\usepackage{longtable}
\makeatletter
\def\dontchopLT{\let\LT@entry@chop\relax}
\def\resumeLT{%
\let\zzz\stepcounter
\def\yyy{LT@tables}
\def\stepcounter##1{%
\def\xxx{##1}%
\ifx\xxx\yyy\else\zzz{##1}\fi}}
\makeatother

\begin{document}

Output both tables together as one table and then output each
individual table.  We had hoped that this would cause the subsequent
tables to be the same width as the first since the first contains all
columns of the subsequent tables but it seems that that does not work.

\noindent X\dotfill X

{\dontchopLT
\begin{longtable}{ll}
1 & 2 \\
3 & 4 \\
\end{longtable}}

\noindent X\dotfill X

{\resumeLT
\begin{longtable}{ll}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{longtable}}

\end{document}

-
Excellent. I just tried it and the same approach does seem to work with longtabu and X columns too and I also tried it with multiple sets of tables and it also continues to work in that scenario too. Furthermore, it has the advantage of eliminating the necessity of a dummy table. –  user1189687 Jul 16 '12 at 21:02
yes if you want more than two tables use dontchopLT before all but the last and resumeLT before all but the first, I believe tabu uses longtable internally so it is picking up the same code patch. –  David Carlisle Jul 16 '12 at 21:07
Nice One! I suspect there is a risk with this solution that one of the tables may end up split across pages. –  ArTourter Jul 16 '12 at 21:55
@ArTourter If that were a concern you could end the rows with \\* instead of \ or put the tables in a minipage (in the latter case you'd have to disable longtable complaining about that) –  David Carlisle Jul 16 '12 at 22:26

The tabu environment will need to use the X columns in order to be able to scale them and remember the calculations with savetabu.

since you do not want the table to occupy the full line width, you can use the spread definition with a value a 0pt and have your X columns with a negative coefficient:

\begin{tabu} spread 0pt  {|X[-1]|X[-1]|}\savetabu{mytabu2}
1 & 2 \\
3 & 4 \\
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
1 & 2 \\
3 & 4 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}


-
Great! That is certainly what I am looking for. Is there some way to address my third point, i.e. generate but do not display the first table so that only the subsequent tables are actually visible? –  user1189687 Jul 16 '12 at 18:37
That is a very good question! I have tried the \phantom{} thing on the whole table but that doesn't work, I have also tried putting the last table into an \edef but that didn't work either. I'll keep looking –  ArTourter Jul 16 '12 at 20:00
I posted the hiding question as a separate question and did get this answer: tex.stackexchange.com/questions/63565/… which together with your response answers the question. The strengths of the approach is your post are (1) that the tables would not have to be consecutive. That is one could interleave tables saving one set of tabu settings in mytabuA and another in mytabuB, say. Also it has a certain simplicity because it does not rely on any user macros. –  user1189687 Jul 16 '12 at 23:59