# Align words in cells of a table

Consider a table like this:

| text1      | text2        |
|------------|--------------|
| a b c      | AAA BBB CCC  |
|------------|--------------|
|            |   aa bb cc   |
|------------|--------------|
| AA BB CC   |              |


Now I want that a is aligned with AA, b with BB, c with CC and AAA with aa and so on.

Is it possible to do this in LaTeX (if possible with a simple and elegant syntax)?

I should add, that for my purpose the three tokens are generated by a command \foo for example instead of writing a b c directly into the table there is something like \foo{a'}{b'}{c'}.

-

You can use the tabular environment along with multcol package. I defined the \foo macro as requested that takes three parameters.

\documentclass{article}
\usepackage{multicol}
\usepackage{array}
\newcolumntype{L}{l@{\hspace{1mm}}}% Adjust horizontal spacing between columns here
\newcolumntype{C}[1]{m{#1}}% Centered columns of fixed width
\newcommand{\foo}[3]{#1&#2&#3}
\begin{document}
\begin{tabular}{|LLL|LLL|}
\multicolumn{3}{c}{text1} &\multicolumn{3}{c}{text2} \\ \hline
a &b&c      & AAA & BBB & CCC   \\
\hline
& &       &   aa & bb & cc   \\
\hline
AA &BB &CC & & & \\
\hline
\end{tabular}

Using the foo macro:
\begin{tabular}{|LLL|LLL|}
\multicolumn{3}{c}{text1} &\multicolumn{3}{c}{text2} \\ \hline
\foo{a}{b}{c}      & \foo{AAA}{BBB}{CCC}   \\
\hline
\foo{}{}{}&    \foo{aa}{bb}{cc}\\
\hline
\foo{AA}{BB}{CC}&  \foo{}{}{} \\
\hline
\end{tabular}

With fixed width columns:
\newcommand{\Size}{1.0cm}%
\begin{tabular}{|C{\Size}C{\Size}C{\Size}|C{\Size}C{\Size}C{\Size}|}
\multicolumn{3}{c}{text1} &\multicolumn{3}{c}{text2} \\ \hline
\foo{a}{b}{c}      & \foo{AAA}{BBB}{CCC}   \\
\hline
\foo{}{}{}&    \foo{aa}{bb}{cc}\\
\hline
\foo{AA}{BB}{CC}&  \foo{}{}{} \\
\hline
\end{tabular}
\end{document}


If you want more spacing between columns you can adjust the newcolumntype, or just use l instead of L for the columns specifications.

You can adjust the width of the columns by changing the parameter passed in to the C column type.

-
Thanks. Is it possible to adjust this such that each \foo{x}{y}{z} is centered? –  student Sep 12 '11 at 19:45
Sure, replace the l (left alignment) with c (center alignment) in the column specification. –  Peter Grill Sep 12 '11 at 21:44
Is it possible to have all "big" columns with the same fixed width? –  student Sep 13 '11 at 18:20
Updated to include fixed width columns. –  Peter Grill Sep 13 '11 at 23:25

You can use \rlap (and \llap and \clap) to overwrite certain components in text. Additionally, \phantom can be use to typeset something without actually doing it. The example below can be modified to have a modify the overlap. Regardless, this does not require any additional packages to be included.

\documentclass{article}
\begin{document}
\begin{tabular}{l|l}\hline
text1 & text2 \\ \hline
\rlap{a}\phantom{AA}\ \rlap{b}\phantom{BB}\ \rlap{c}\phantom{CC} & AAA\ BBB\  CCC \\ \hline
& \rlap{aa}\phantom{AAA}\ \rlap{bb}\phantom{BBB}\ \rlap{cc}\phantom{CCC} \\ \hline
AA\ BB\ CC \\ \hline
\end{tabular}
\end{document}


-