# Table specific questions

I use report class and I have just started working with tables. I have created one:

\begin{table} [ht]
\centering
\caption{Ut convallis pharetra augue at aliquet}
\label{table1_disc_log}
\small
\begin{tabular}{ |p{4cm} || p{5cm} | p{5cm} | }
\hline\hline
\textbf{Ut convallis} & \multicolumn{2}{|c|}{\textbf{Lorem ipsum dolor sit amet}}\\
\cline{2-3}
\ & Ut convallis pharetra & Consectetur adipisci veliturea \\
\hline
Lorem ipsum dolor sit amet & \centering $O(n)$ & Lorem ipsum dolor \\
\hline
Veliturea adipiscing, consectetur adipisci & \centering $O(\sqrt{n})$ & Lorem ipsum \\
\hline
$\rho$--Pollard method & \centering $O(\sqrt{n})$ &  Lorem ipsum dolor \\
\hline
Pharetra augue at aliquet, consectetur adipisci &
\centering $O\left(\left(\sum_{i=1}^rk_i\left(\sqrt{p_i} + \ln p \right) \right)\ln^2p \right)$ &
Ut convallis pharetra augue at aliquet, consectetur \\
\hline
Adipiscing elit, ut convallis & \centering $L_p\left[\frac{1}{3},const\right]$ &
Lorem ipsum dolor sit amet, consectetur adipisci (ut convallis pharetra augue at aliquet, consectetur) \\
\hline\hline
\end{tabular}
\end{table}


Unfortunately, I haven't found a clear explanation how to adjust some of its elements. So there are my questions:

1. How could I horizontally center the text in some cells? I have tried \centering in cells, but it doesn't work with all table cells. For example, I need to center the headers of two columns: 'Ut convallis pharetra' and 'Consectetur adipisci veliturea', how can I manage that?

2. Why didn't the text in the first cell automatically become vertically centered? Additionally, how to make it horizontally centered at the same time?

3. How could I set text margins for cells (in general, custom for some columns and custom for some cells)? How to set custom height for some rows? (higher than the text inside of it.)

4. How could I set default text alignment for the whole table ('on top of the cell', or 'on center of the cell'.) How could I change this behaviour for some particular columns or cells?

5. How to make an empty cell? I had to use \ (space), otherwise there was an error.

-

1. paragraph-style columns can be centered, since they have a known width. However, since changes in horizontal justification also changes the way they \\ macro works, you need to correct for this. The array package provides \arraybackslash to redefine the way \\ works. So, to centre the two sub-column headings, you can use

\usepackage{array}% http://ctan.org/pkg/array
%...
\textbf{Ut convallis} & \multicolumn{2}{c|}{\textbf{Lorem ipsum dolor sit amet}}\\
\cline{2-3}
\ & \centering Ut convallis pharetra &
%...

2. The easiest here is to use multirow to push that element halfway down. Or you could just use \raisebox as well. Why didn't it do this automatically? Because you this requirement could be very user-specific, so specifying it manually is the only way.

\usepackage{multirow}% http://ctan.org/pkg/multirow
%...
\hline\hline
\centering\arraybackslash\multirow{2}*{\textbf{Ut convallis}} &
\multicolumn{2}{c|}{\textbf{Lorem ipsum dolor sit amet}} \\
\cline{2-3}
%...


An alternative would have been to provide the column heading/sub-heading in columns 2-3 as a separate tabular, making it a single cell (rather than a 4-cell tabular as it is now). Then other alignment options become available, but then vertical rule re-definition and some spacing compensation might crop up.

3. I would using whitespace in the \hspace*{<len>} form for horizontal padding, and possibly zero-width rules: \rule{0pt}{<len>} for vertical padding. This would help with a cell-by-cell horizontal adjustment.

For column-specific padding, you can use the column specification @{<stuff>} to insert <stuff> between two columns. For example, r@{\quad}l would insert \quad between a right- and left-aligned column.

4. You can use the array package to insert an element at the start of each cell to centre it (say), while still specifying a paragraph-style column. This is typically done using (say) >{\centering\arraybackslash}p{5cm} to provide a 5cm wide, centre-aligned column. Additionally, array provides a vertically-aligned cell specification using the m{<len>} column type. See the array documentation for more on these specification.

5. There is no need to use \ (control space) for an empty cell.

Here is your table, wrapped in a minimal working document:

\documentclass{report}
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{multirow}% http://ctan.org/pkg/multirow
\begin{document}

\begin{table} [ht]
\centering
\caption{Ut convallis pharetra augue at aliquet}
\label{table1_disc_log}
\small
\begin{tabular}{ |m{4cm} || >{\centering\arraybackslash}m{5cm} | m{5cm} | }
\hline\hline
\centering\arraybackslash\multirow{2}*{\textbf{Ut convallis}} & \multicolumn{2}{c|}{\textbf{Lorem ipsum dolor sit amet}}\\
\cline{2-3}
& Ut convallis pharetra & \centering\arraybackslash Consectetur adipisci veliturea \\
\hline
Lorem ipsum dolor sit amet & $O(n)$ & Lorem ipsum dolor \\
\hline
Veliturea adipiscing, consectetur adipisci & $O(\sqrt{n})$ & Lorem ipsum \\
\hline
$\rho$--Pollard method & $O(\sqrt{n})$ &  Lorem ipsum dolor \\
\hline
Pharetra augue at aliquet, consectetur adipisci &
$O\left(\left(\sum_{i=1}^rk_i\left(\sqrt{p_i} + \ln p \right) \right)\ln^2p \right)$ &
Ut convallis pharetra augue at aliquet, consectetur \\
\hline
Adipiscing elit, ut convallis & $L_p\left[\frac{1}{3},\mathrm{const}\right]$ &
Lorem ipsum dolor sit amet, consectetur adipisci (ut convallis pharetra augue at aliquet, consectetur) \\
\hline\hline
\end{tabular}
\end{table}

\end{document}​

-
Thank you for your prompt answer, but how to vertically center content in some cell (or in particular column)? I mean for example I wouldn't like to have those two big formulae aligned on top, but on center. –  Denis Apr 17 '12 at 21:03
@Denis: I've updated the answer to allow for a vertically centered view using m{..} instead of p{..} columns. I've also used \mathrm{..} for text in math mode (although you could consider using amsmath's \text{..} macro. –  Werner Apr 17 '12 at 21:13
@Werner: I'd say this is a case of meta.tex.stackexchange.com/q/2414/4012; while this question isn't bad per se, it really doesn't fit our format in that it contains a bunch of distinguishable questions that would better not be asked in a single question. –  doncherry Apr 17 '12 at 22:13