views:

413

answers:

2

Basically, I want to produce the following table in LaTeX (Notice the "comma alignment" of the second cell) :

----------------------------------------
| Header1 | Header2                    |
----------------------------------------
|    1    | "value 1"      , "value 2" |
|    2    | "one"          , "two"     |
|    3    | "abcdefheasal" , "ok"      |
----------------------------------------

The way I would produce the table above in LaTeX is as follows:

\begin{tabular}{|c|l|}
  \hline
  Header1 & Header2             \\
  \hline
  1 & ``value 1'' , ``value 2'' \\
  2 & ``one'' , ``two''         \\
  3 & ``abcdefheasal'' , ``ok'' \\
  \hline
\end{tabular} 

But obviously, that code produces the following (obviously without the "comma alignment") :

-----------------------------------
| Header1 | Header2               |
-----------------------------------
|    1    | "value 1" , "value 2" |
|    2    | "one" , "two"         |
|    3    | "abcdefheasal" , "ok" |
-----------------------------------

What is the best way to turn the latter table into the one I want? i.e. the former

+2  A: 

I would suggest using a three-column table, then merging the header of the second and third column, as follows:

\begin{tabular}{|c|ll|}
  \hline
  Header1 & \multicolumn{2}{l}{Header2} \\  
  \hline  
  1 & ``value 1''      & , ``value 2''  \\  
  2 & ``one''          & , ``two''      \\  
  3 & ``abcdefheasal'' & , ``ok''       \\  
  \hline
\end{tabular}
Martin B
There's only one single problem with your example: The end of the Headers row is not being closed. As Seth said, you need the extra `|` in the multicolumn: `\multicolumn{2}{l|}`
Andreas Grech
Good point -- thanks!
Martin B
+4  A: 

As a modification to Martin B's answer, consider using the @ sign to make a column separator:

\begin{tabular}{|c|l@{ , }l|}
  \hline
  Header1 & \multicolumn{2}{l|}{Header2}  \\
  \hline
  1 & ``value 1'' & ``value 2'' \\
  2 & ``one'' & ``two''         \\
  3 & ``abcdefheasal'' & ``ok'' \\
  \hline
\end{tabular}

Also note that an extra pipe is needed in the multicolumn to draw the vertical line on the first row.

Seth Johnson
+1 I chose your answer because I prefer using that `@` syntax for column separating. Thanks for the mention.
Andreas Grech