13

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}
robertspierre
  • 638
  • 3
  • 16
Jeff
  • 233
  • 1
    Welcome to TeX.SE! The short answer to both of your questions is: "Yes." Please consider adding a minimum working example (MWE) of the tabular environment you have in mind. Without such information, it's going to be very difficult to provide more specific suggestions. – Mico Feb 22 '12 at 03:16

3 Answers3

11

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.

David Carlisle
  • 757,742
Alan Munn
  • 218,180
5

This is a slightly different approach to what is posted in Make first row of table all bold.

tabular environments are easily formatted column-wise. Not so much row-wise. However, there are ways around it. Here's one attempt.

You can use an \if... conditional to distinguish between your header/non-header rows. \newif\headerrow defines the condition \ifheaderrow that can be either true (\headerrowtrue) or false (\headerrowfalse). Subsequently you can insert contents before each table entry depending on this condition with the aid of the array package:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\newif\ifheaderrow
\newcolumntype{C}{>{\ifheaderrow\bfseries\fi}c}
\begin{document}
\begin{table}
  \global\headerrowtrue
  \begin{tabular}{|>{\bfseries}c | *{3}{C|}}
    \hline
    & S1 & S2 & S3 \\ 
    \hline \global\headerrowfalse
     D1 & 4217 & 5821 & 1102 \\
    D2 & 3679 & 5089 & 991 \\
    D3 & 2589 & 3301 & 604 \\
    D4 & 1418 & 1722 & 294 \\
    \hline
  \end{tabular}
\end{table}

\end{document}​

The resetting of \headerrowfalse must be made \global, since the modification would otherwise only hold within the group, which is the cell within which it is called.

Moriambar
  • 11,466
Werner
  • 603,163
0

after eleven years ...

  • by use of the makecell package:
\documentclass{article}
\usepackage{booktabs,
            makecell} % for `thead` command
\renewcommand\theadfont{}
\begin{document}
\begin{tabular}{lrp{1in}}
\toprule
\thead{A} & \thead{B} & \thead{C}\\
\midrule
left & right & 1 inch\\
l & r & foo\\
\bottomrule
\end{tabular}
\end{document}

enter image description here

or

\documentclass{article}
\usepackage{booktabs,
            makecell}
\renewcommand\theadfont{\normalsize\bfseries}
\begin{document}
\begin{tabular}{lrp{1in}}
\toprule
\thead{A} & \thead{B} & \thead{C}\\
\midrule
left & right & 1 inch\\
l & r & foo\\
\bottomrule
\end{tabular}
\end{document}

enter image description here

  • today someone my consider novel table package tabularray:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document} \begin{tblr}{colspec = {l r Q[l, wd=1in]}, row{1} = {c, font=\bfseries} } \toprule A & B & C \ \midrule left & right & 1 inch \ l & r & foo \ \bottomrule \end{tblr} \end{document}

result is the same as in the second example.

Zarko
  • 296,517