Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 558
Andreas Grech Avatar asked Jan 25 '10 14:01

Andreas Grech


People also ask

How do you align text in a table in LaTeX?

You can also use r to align the text to the right and l for left alignment. This will insert a horizontal line on top of the table and at the bottom too. There is no restriction on the number of times you can use \hline . Each & is a cell separator and the double-backslash \\ sets the end of this row.

How do I align text to the top of a table?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.

How do you align text justify in LaTeX?

Full Justify Text By default, LaTeX fully justifies text in LaTeX documents. However, you can explicitly specify this if you are using a different alignment method. To do this, use the \justify command.


2 Answers

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.

like image 71
Seth Johnson Avatar answered Sep 20 '22 10:09

Seth Johnson


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}
like image 36
Martin B Avatar answered Sep 20 '22 10:09

Martin B