The array package
Extends the column argument of tabular and array: m{...} and b{...} columns, >{...} and <{...} for code around every cell, and \newcolumntype.
Loading the package
\usepackage{array}Without the package, a column is l, c, r or p{width}, and formatting has to be repeated in every cell. With array, >{\bfseries}l sets a whole column in bold, >{$}c<{$} puts every cell into math mode, and m{3cm} is a wrapped column whose neighbours are centred vertically.
\newcolumntype gives such a combination a letter of its own, with arguments if needed. Many table packages are built on array and load it, among them tabularx, colortbl and dcolumn.
Main commands
m{3cm} | Wrapped column of 3 cm, vertically centred; p aligns at the top line, b at the bottom line. |
>{\bfseries}l | Inserts code before every cell of the column. |
>{$}c<{$} | Code before and after every cell; here a column in math mode. |
>{\centering\arraybackslash}p{2cm} | Wrapped column with centred text. \arraybackslash restores \\ as the end of the row. |
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} | Defines the column type C{2cm}. |
w{c}{1.5cm} | Column of a fixed width without wrapping, with centred content. |
!{\vrule width 1pt} | Own material between two columns in place of |, here a thicker line. |
\setlength{\extrarowheight}{2pt} | Adds height to every row, so text does not touch an \hline above it. |
Complete example
\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\setlength{\extrarowheight}{2pt}
\begin{tabular}{>{\bfseries}l m{4.5cm} C{1.5cm} >{$}c<{$}}
\hline
Name & Description & Count & \multicolumn{1}{c}{Formula} \\ \hline
Alpha & A longer text that is broken into several lines
inside the cell. & 12 & x^2 + 1 \\
Beta & A short text. & 7 & \sqrt{y} \\ \hline
\end{tabular}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
\centering,\raggedrightand\raggedleftchange the meaning of\\. In the last column of a table, the row end then gives Misplaced \noalign at the next\hline. Add\arraybackslashafter the alignment command.m{...},>{...}and the other additions used without\usepackage{array}give Illegal character in array arg.- The code in
>{...}also applies to the header row. Write a header cell as\multicolumn{1}{c}{Header}to leave it out.