Motivation
LaTeX offers great functionality to create professionally looking tables. The default column types (left-aligned l
; center-aligned c
; and right-aligned r
) adjust to the text size, rather than wrapping text automatically. This works well as long as the content in each cell is short and of similar length. However, often you want to display several words or even complete sentences and the text might run into the margins. This is where the additional column-types p
, m
and b
come in. These help making tables more readable.
Here is a somewhat extreme example with a paragraph of dummy text (blindtext package) added to the first row of the second column.
\documentclass[11pt]{article} \usepackage{blindtext} \usepackage{showframe} \renewcommand*\ShowFrameColor{\color{red}} \renewcommand{\arraystretch}{1.2} \begin{document} \phantom{~} \begin{tabular}{ll} \hline First column&Second column\\ \hline abcd&\blindtext\\ efgh&ijkl\\ \hline \end{tabular} \end{document}
Fixed width column-types
There are three additional column types, which produce fixed width columns:
p{width} | Top-aligned cells width fixed width |
m{width} | Middle-aligned cells width fixed width |
b{width} | Bottom-aligned cells with fixed width |
The width can either be defined as an absolute number (e.g. 3cm / 2in) or as a fraction of the text width (e.g. 0.2\textwidth
). In the example below I use the latter. Typically, the fractions do not add up to 1, as you want to leave some white space between columns.
\documentclass[11pt]{article} \usepackage{blindtext} \usepackage{showframe} \renewcommand*\ShowFrameColor{\color{red}} \renewcommand{\arraystretch}{1.2} \begin{document} \phantom{~}\noindent \begin{tabular}{p{0.1\textwidth}p{0.8\textwidth}} \hline First column&Second column\\ \hline abcd&\blindtext\\ efgh&ijkl\\ \hline \end{tabular} \end{document}
You might wonder how to horizontally align the content in each column. After all, the three column types (p{}
, m{}
and b{}
) define the vertical alignment. But more often, we are concerned with the horizontal alignment. We can use pre- and post-column-type declaration. The horizontal alignment is set using a pre-column-type declaration.
>{decl.} | Can be used before an l , r , c , p , m or a b option. It insertsdecl. directly in front of the entry of the column. |
<{decl.} | Can be used after an l , r , c , p{..} , m{..} or a b{..} option. It inserts decl. right after the entry of the column. |
Horizontal alignment
Below is an example where the first column is left-aligned (default, so the declaration is optional) and the other three columns are center aligned.
\documentclass[11pt]{article} \usepackage{array} \usepackage{multirow} \renewcommand{\arraystretch}{1.2} \begin{document} \phantom{~}\noindent \begin{tabular}{p{0.2\textwidth}>{\centering}p{0.2\textwidth}>{\centering}p{0.2\textwidth}>{\centering\arraybackslash}p{0.2\textwidth}} \hline \multirow{2}{*}{Country}&\multicolumn{2}{c}{Population}&\multirow{2}{*}{Change (\%)}\\\cline{2-3} &2016&2017&\\ \hline China&1,403,500,365&1,409,517,397&+0.4\%\\ India&1,324,171,354&1,339,180,127&+1.1\%\\ United States&322,179,605&324,459,463&+0.7\%\\ Indonesia&261,115,456&263,991,379&+1.1\%\\ Brazil&207,652,865&209,288,278&+0.8\%\\ Pakistan&193,203,476&197,015,955&+2.0\%\\ Nigeria&185,989,640&190,886,311&+2.6\%\\ Bangladesh&162,951,560&164,669,751&+1.1\%\\ Russia&146,864,513&143,989,754&−2.0\%\\ Mexico&127,540,423&129,163,276&+1.3\%\\ \hline \end{tabular} \end{document}
There are three macros for left, center and right alignment, listed in the table below. To adjust the alignment, the array package needs to be loaded. This is because of a peculiarity of these alignment macros, where the function of \\
needs to be restored for use in the tabular environment. You do that by adding <strong>\arraybackslash</strong>
to the last column declaration after the last alignment modifier. If you forget to add \arraybackslash
, you will get an error (Extra alignment tab has been changed to \cr). See above for an example.
\raggedright | Left-align content (default) |
\centering | Center-align content |
\raggedleft | Right-align content |
The column definition at the beginning of a tabular tends to get rather lengthy. There are two ways to make them more readable: 1) Define a custom column-type; and 2) use the short form for repeating column-types.
Other array package macros
Define a new column-type
The array package further implements the macro \newcolumntype{name}{definition}
, which offers a simple way to define your own column types and use them throughout the document. For example, it allows to create a column-type which highlights a column. Note that this example requires additional packages to be loaded (xcolor and colortbl).
\documentclass[11pt]{article} \usepackage{array,xcolor,colortbl} \usepackage{multirow} \renewcommand{\arraystretch}{1.2} \newcolumntype{G}{>{\centering\columncolor{gray!20!white}}p{0.2\textwidth}} \newcolumntype{C}{>{\centering\arraybackslash}p{0.2\textwidth}} \begin{document} \begin{tabular}{p{0.2\textwidth}CGC} \hline \multirow{2}{*}{Country}&\multicolumn{2}{c}{Population}&\multirow{2}{*}{Change (\%)}\\\cline{2-3} &2016&2017&\\ \hline China&1,403,500,365&1,409,517,397&+0.4\%\\ India&1,324,171,354&1,339,180,127&+1.1\%\\ United States&322,179,605&324,459,463&+0.7\%\\ Indonesia&261,115,456&263,991,379&+1.1\%\\ Brazil&207,652,865&209,288,278&+0.8\%\\ Pakistan&193,203,476&197,015,955&+2.0\%\\ Nigeria&185,989,640&190,886,311&+2.6\%\\ Bangladesh&162,951,560&164,669,751&+1.1\%\\ Russia&146,864,513&143,989,754&--2.0\%\\ Mexico&127,540,423&129,163,276&+1.3\%\\ \hline \end{tabular} \end{document}
Short form for repeating column-types
When you create a table with a repeated column-type, you can use the short form: *{n}{type}
. This significantly shortens the column-type declaration, especial when using the array package column-types with pre- and/or post-column-type declarations.
\documentclass{article} \usepackage{array} % ... \begin{document} % ... \begin{tabular}{p{0.2\textwidth}*{3}{>{\centering\arraybackslash}p{0.2\textwidth}}} % Table content \end{tabluar} % ... \end{document}
Finally
You may also be interested in multi-row and multi-column cells in tabular/table, which was previously covered.
Anon
Very good. Exactly what I was looking for.