Search code examples
alignmentlatex

Custom aligning text in a Table in LaTeX


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


Solution

  • 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.