3

Given Table 1 and Table 2, I want to create a Table 3 whose columns are

Column1(Table3) = max{Column1(Table1), Column1(Table2)}

Column2(Table3) = max{Column2(Table1), Column2(Table2)}

Column3(Table3) = max{Column3(Table1), Column3(Table2)}

Column4(Table3) = max{Column4(Table1), Column4(Table2)}

So, if I copy the elements all from table 1 or all from table 2 to table 3, the layout of table 3 will not change. (The Table 3 below just shows the column width. You can ignore the elements in it.)

\documentclass{article}  
\begin{document}  


\begin{table}
\centering
\begin{tabular}{|l|l|l|l|}
    \hline
    Column1 & Column2 & Column3 & Column4 \\ \hline
    The & quick brown & fox & jumps over the lazy dog \\ 
    The & quick brown & fox & jumps over the lazy dog \\
    The & quick brown & fox & jumps over the lazy dog\\
    \hline
\end{tabular}
\caption{First table}
\end{table}


\begin{table}
\centering
\begin{tabular}{|l|l|l|l|}
      \hline
    Column1 & Column2 & Column3 & Column4 \\ \hline
    The  quick & brown & fox  jumps & over the lazy dog \\
    The  quick & brown & fox  jumps & over the lazy dog \\
    The  quick & brown & fox  jumps & over the lazy dog\\
    \hline
\end{tabular}
\caption{Second Table}
\end{table}


\begin{table}
\centering
\begin{tabular}{|l|l|l|l|}
      \hline
    Column1 & Column2 & Column3 & Column4 \\ \hline
    The  quick & quick brown & fox  jumps & jumps over the lazy dog \\ 
    The  quick & quick brown & fox  jumps & jumps over the lazy dog \\
    The  quick & quick brown & fox  jumps & jumps over the lazy dog\\
    \hline
\end{tabular}
\caption{Desired Width}
\end{table}


\end{document} 

enter image description here

5
  • I think I don't understand one point of your request. First you start with looking at two tables, whose column sizes should somehow build up a third table automatically(?). But, now you want to apply the layout of this third table back to those it was derived from. This sounds like the begin of a endless loop to me. However, you also seem to work from two directions at the same time. Can you clarify this? If you don't want an automated solution you could just create three tables all with the same alignment pattern, using p{} columns.
    – Ruben
    Nov 2, 2013 at 12:26
  • @Ruben, sorry for the ambiguous description. I've updated the problem. Is it clear now?
    – Lijie Xu
    Nov 2, 2013 at 12:39
  • No need to apalogize. I just tried to understand better what you need and amplify your chances to get a good answer. It is much more clear to me now. Only the sentence in the brackets still seems problematic to me, as it states that you have some desired column width constellation in table 3, but it also has to depend on the two former tables. Both won't be possible.
    – Ruben
    Nov 2, 2013 at 13:28
  • To set the width of a column is easier than determining it. The former can be done using \makebox for one row of the table. The latter consists of taking the maximum of the \settowidth results for each entry in the column. Nov 5, 2013 at 4:40

1 Answer 1

0

The big problem is that the text has to be typed in twice, unless you want to create a bunch of \savebox's and/or parse the tabular itself.

\documentclass{article}

\newlength{\temp}

\newcommand{\maxwidth}[2]% #1 = length name, #2 = text
{\settowidth{\temp}{#2}%
\ifdim #1<\temp \setlength{#1}{\temp}%
\fi}

\newlength{\columnA}
\newlength{\columnB}

\begin{document}

\maxwidth{\columnA}{Column 1}
\maxwidth{\columnA}{The quick}
\maxwidth{\columnA}{The}

\maxwidth{\columnB}{Column 2}
\maxwidth{\columnB}{brown fox}
\maxwidth{\columnB}{quick brown fox}

\begin{tabular}{|l|l|}
\hline
\makebox[\columnA]{Column 1}&\makebox[\columnB]{Column 2}\\
\hline
The quick&brown fox\\
\hline
\end{tabular}

\vspace*{0.1in}
\begin{tabular}{|l|l|}
\hline
\makebox[\columnA]{Column 1}&\makebox[\columnB]{Column 2}\\
\hline
The&quick brown fox\\
\hline
\end{tabular}

\end{document}

tabulars

You must log in to answer this question.

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