[Tex/LaTex] Justify Table by Row

horizontal alignmenttables

For example, is it possible to justify the first row of a table differently from the rest? Maybe there is a way to combine two tables to look like one? MWE:

\documentclass{article}
\begin{document}

\begin{table}
  \begin{tabular}{| c | c | c | c |}
    \hline
    & \textbf{S1} & \textbf{S2} & \textbf{S3} \\
    \hline
    \textbf{D1} & 4217 & 5821 & 1102 \\
    \textbf{D2} & 3679 & 5089 & 991 \\
    \textbf{D3} & 2589 & 3301 & 604 \\
    \textbf{D4} & 1418 & 1722 & 294 \\
    \hline
  \end{tabular}
\end{table}

\end{document}

Best Answer

The standard way to justify the heading rows of a small table is to use the \multicolumn command, which allows you to override the column specification for each span of rows. In this example, I've used the booktabs package, which is highly recommended for any professional looking table.

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lrp{1in}}
\toprule
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B} & \multicolumn{1}{c}{C}\\
\midrule
left & right & 1 inch\\
l & r & foo\\
\bottomrule
\end{tabular}
\end{document}

output of code

For some fancier ideas for heading rows, see Make first row of table all bold.