如何对齐多个表的列

如何对齐多个表的列

给定表 1 和表 2,我想创建一个表 3,其列为

列 1 (表 3) = max{列 1 (表 1),列 1 (表 2)}

列 2 (表 3) = max{列 2 (表 1), 列 2 (表 2)}

列 3 (表 3) = max{列 3 (表 1), 列 3 (表 2)}

列 4 (表 3) = max{列 4 (表 1), 列 4 (表 2)}

因此,如果我将表 1 中的所有元素或表 2 中的所有元素复制到表 3,表 3 的布局不会改变。(下面的表 3 仅显示列宽。您可以忽略其中的元素。)

\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} 

在此处输入图片描述

答案1

最大的问题是必须输入两次文本,除非您想创建一堆 \savebox 和/或解析表格本身。

\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}

表格

相关内容