Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this document

Formatting Information — An introduction to typesetting with LATEX

### In this section…

Tabular typesetting is the most complex and time-consuming of all textual features to get right. This holds true whether you are typing in plaintext form, using a wordprocessor, using LATEX, using HTML or XML, using a DTP system, or some other text-handling package.

Fortunately, LATEX provides a table model with a mixture of defaults and configurability to let it produce very high quality tables with a minimum of effort.

### 4.2.1 Floats

Tables and Figures (and several other features of documents like sidebars) are what printers and publishers refer to as ‘floats’. This means they are not part of the normal stream of sentences of text, but separate freestanding entities, positioned in a part of the page to themselves (top, middle, bottom, left, right, or wherever the designer specifies). They always have a caption describing them and they are always numbered so they can be referred to from elsewhere in the text.

LATEX automatically floats Tables and Figures, depending on how much space is left on the page at the point that they are processed. If there is not enough room on the current page, the float is moved to the top of the next page. This can be changed by moving the Table or Figure to an earlier or later point in the text, or by adjusting some of the parameters which control automatic floating.

Authors sometimes want many floats occurring in rapid succession, which raises the problem of how they are going to fit on the page and still leave room for text. In this case, LATEX stacks them all up and prints them together if possible, or leaves them to the end of the chapter in protest.

The skill is to space them out within your text so that they intrude neither on the thread of your argument or discussion, nor on the visual balance of the typeset pages. But this is a skill few authors have, and it’s one point at which professional typographic advice or manual intervention may be needed.

There is a float package which lets you create new classes of floating object (sidebars, examples, exercises, etc), and it also implements a method of forcing a float not to float (that is, to appear where it occurs in the text, even if that breaks the page layout).

Please now read from § 4.2.1 up to here a second time.

### 4.2.2 Normal tables

To create a LATEX Table, use the table environment containing a \caption command followed by a \label command (the label can be used to refer to the table: see § 5.3.1).

\begin{table}
\caption{Project expenditure to year-end 2016}
\label{ye2016exp}
...
\end{table}


Numbering is automatic, but the \label command MUST follow the \caption command, not precede it. The numbering automatically includes the chapter number in document classes where this is appropriate (but this can of course be overridden). The \caption command has an optional argument to provide a short caption if the full caption would be too long for the List of Tables:

\caption[Something short]{Some very long caption
that will only look reasonable in the full figure.}


### 4.2.3 Simple tabular matter

Within a Table, there are four ways to enter the data:

By hand

you can enter the tabular matter (cell data) by typing it in, which is perhaps the most common method;

In a grid tool

many LATEX editors come with a pop-up grid tool like a miniature spreadsheet, which makes creating tabular matter easier, at the cost of some loss of control;

With a package

if the quantity of data is very large and is already in a spreadsheet, or if it is spreadsheet data which will change frequently before you are finished, you can use the datatool package to read the data from a spreadsheet CSV export file (see § 4.2.4)

As an image

it is also possible to include a ‘table’ which has actually been captured as an image from elsewhere, such as a screenshot from a spreadsheet (but then this isn’t really a table). We will see how to include images in § 4.3 on Figures, where they are more common.

In Table 4.3 we have a table which we’ll use as an example. It’s got a number, a caption, three columns with headings and some ruled lines, and a comment afterwards.

### Table 4.3: Project expenditure to year-end 2016

 Item € Amount Total 64,783 a) Salaries (2 part-time research assistants) 28,000 Conference fees and travel expenses 14,228 Computer equipment (5 workstations) 17,493 Software 3,562 b) Rent, light, heat, etc 1,500

The Institute also contributes to (a) and (b).

To typeset tabular matter, within a table environment or elsewhere, you use the tabular environment. This takes one compulsory argument which specifies how many columns and what type they are. You follow the \begin{tabular} command with a pair of curly braces giving the alignment of the columns.

Column alignment

You give one letter for each column using one of l, c, and r for a left-aligned, centered, or right-aligned column. The number of letters MUST be the same as the number of columns you are putting in the table.

\begin{tabular}{clr} ... \end{tabular}


In the example in Table 4.3, the tabular setting has three columns, the first one centered, the second left-aligned, and the third one right-aligned, so it is specified as {clr}.

Cell and row division

You can then type in each row, making sure each cell’s data in the row is separated with an & character, and each row ends with a double backslash (\\).

a)&Salaries (2 part-time research assistants)&28,000\\


You don’t need to add any extra spaces or do any manual formatting, although you can if you want: LATEX just uses the column specification to know how to format it.

If a cell has nothing to go in it, you just don’t type anything, but the ampersand must still be there:

&Total&64,783\\

Column headings

These are often set in bold type, as in the example (see ‘Cell formatting’ below).

&\textbf{Item}&\textbf{\EUR\ Amount}\\[6pt]\hline


In this case there is also some extra space (6pt, see ‘Row spacing’ below) and a horizontal line across the table (see ‘Tables rules’ below).

All the data for a row may be longer than a line in your editor, but it can take up as many lines as needed; the end of the row is signalled by the double backslash, so LATEX knows when it’s time for the next row.

Cell formatting

Font changes can be done within a cell (bold, italic, etc; we’ll come on to these later, see § 6.2.6) and these changes are limited to the cell in which they occur: they do not ‘bleed’ across cells (in the example, the column headings have each been made bold separately).

Row spacing

Additional vertical white-space below a row (but above a rule) can be specified by giving a dimension in [square brackets] immediately after the double backslash which ends the row (3pt in the case of the last row before the totals in the example). A negative value will decrease the spacing below that row.

If the line below a horizontal rule looks too close, it can be optically spaced by adding a strut at the start of the next line (that is, after the \hline). A ‘strut’ is hidden vertical rule a little bit higher than the row-height; hidden because its width is zero, making it invisible, as in the example code. The \rule command can be used for this, with a width of 0pt and height of 1.2em, just a fraction higher than the text, forcing the lines apart by 0.2em.

\begin{table}
\caption{Project expenditure to year-end 2016}
\label{ye2016exp}
\centering\smallskip
\begin{tabular}{clr}
&\textbf{Item}&\textbf{\EUR\ Amount}\\\hline\rule{0pt}{1.2em}
a)&Salaries (2 part-time research assistants)&28,000\\
&Conference fees and travel expenses&14,228\\
&Computer equipment (5 workstations)&17,493\\
&Software&3,562\\
b)&Rent, light, heat, etc.&1,500\\[3pt]\cline{2-3}
\rule{0pt}{1.2em}&Total&64,783\\
\end{tabular}
\par\medskip\footnotesize
The Institute also contributes to (a) and (b).
\end{table}

Table rules

A line across the whole table is done with the \hline command after the double-backslash which ends a row.

For a line which only covers some of the columns, use the \cline command (in the same place), with the column range to be ruled in curly braces. If only one column needs a rule, it must still be given as a range (eg in the example, {3-3}).

Vertical rules (between columns) can be specified in the column specifications with the vertical bar character (|) before, after, or between the l, c, r letters. This character creates rules which extend the whole height of a table: it is not necessary to repeat them every row.

I have indented the code example given just to make the elements of the table clearer to read: this is for editorial convenience, and has no effect on the formatted result (see Table 4.3). If you copy and paste this into your example document, you will need to add the marvosym package to your Preamble, which will let you use the official CEC-conformant Euro symbol command \EUR (€ as distinct from €).

### 4.2.4 More complex tabular formatting

TEX’s original tabular settings were designed for classical numerical grids, where each cell contains a single value. If you need a cell to contain multiline text, like a miniature paragraph, you can use the letter p followed by a width in curly braces instead of an l, c, or r. Thus p{3.5cm} would mean a column in which each cell would be 3.5cm wide, and could contain more than one line of text or values.

\begin{tabular}{cp{3.5cm}r}


These p column specifications are not multi-row (row-spanned) entries: they are single cells which contain multiple lines of typesetting: the distinction is extremely important. These paragraphic cells are typeset justified (two parallel margins) and the baseline of the top line of text is aligned with the baseline of neighbouring cells in the row.

The array package provides some important enhancements which overcome the limitations of the p cells:

Vertical alignment

In addition to the p, whose vertical alignment baseline is the the top line of text, the array package provides the m and b letters. These work the same way as p (followed by a width in curly braces), but their vertical alignment baseline is the middle or bottom of the cell respectively.

Prefixes and suffixes

With the array package, any column specification letter can be preceded by > and some LATEX commands in curly braces. These commands are applied to every cell in that column, so to make a p column typeset ragged-right you would say, for example, >{\raggedright}p{3.5cm} (or \raggedleft, or \centering).

Note that if you do this, the final column specification must include a prefix or suffix containing the \arraybackslash command, to revert the meaning of the double-backslash, which gets redefined by horizontal formatting commands like \raggedright.

There is a suffix format as well: you can follow a column letter with < and code in curly braces (often used to turn off math mode started in a prefix).

The colortbl package lets you colour rows, columns, and cells; and the dcolumn package provides decimal-aligned column specifications for scientific or financial tabulations. Multi-column (column-spanning) is built into LATEX tables with the \multicolumn command; but for multi-row (row-spanning) cells you need to add the multirow package. Multi-page and rotated (landscape format) tables can be done with the longtable, rotating, and landscape packages.

The LATEX table model is very different from the HTML auto-adjusting model used in web pages; it’s closer to the CALS model used in technical documentation. However, auto-adjusting column widths are possible with the tabularx and tabulary packages, offering different approaches to dynamic table formatting.

You do not need to format the tabular data in your editor: LATEX does this for you when it typesets the table, using the column specifications you provided. You can give the cell values all on one line, or split over many lines: it makes no difference so long as cells are separated with the & and rows are ended with the double-backslash.

As mentioned earlier, some editors have a grid-like array editor for entering tabular data. Takaaki Ota provides an excellent tables-mode for Emacs which uses a spreadsheet-like interface and can generate LATEX table source code (see Figure 4.1).

### Figure 4.1: Tables mode for Emacs

If your tabular data comes from outside LATEX, Nicola Talbot’s excellent datatool package allows the import of data from spreadsheet .csv files and other sources.

### 4.2.5 More on tabular spacing

Extra space, called a ‘shoulder’, is automatically added on both sides of all columns by default. The default value is 6pt, so you get that amount left and right of the tabulation; because it is added left and right of every cell, the space between columns is therefore 12pt by default. This can be adjusted by changing the value of the \tabcolsep dimension before you begin the tabular environment.

\setlength{\tabcolsep}{3pt}


The shoulder can be omitted in specific locations by adding the code @{} in the appropriate places. For example to omit it at the left-hand and right-hand sides of a tabular setting, put it at the start and end of the column specifications (putting it between two column specifications will remove all space between those columns).

\begin{tabular}{@{}clr@{}}



It is conventional to centre the tabular setting within a Table, using the center environment (note US spelling) or the \centering command (as in the example), but this is an æsthetic decision (or perhaps one mandated by your publisher: some journals insist instead that all tabular material is set flush to the left-hand margin).

If there is no data for a cell, just don’t type anything — but you still need the & separating it from the next column’s data. The astute reader will already have deduced that for a table of n columns, there must always be n-1 ampersands in each row. The exception to this is when the \multicolumn command is used to create cells which span multiple columns, when the ampersands of the spanned columns are omitted.

### 4.2.6 Tabular techniques for alignment

As mentioned earlier, it’s also perfectly possible to typeset tabular matter outside a formal Table, where you want to lay out an informal tabulation where a fully floating formal Table would be unnecessary (these are usually quite short: there are several of them in this document).

By default, LATEX typesets tabular environments inline to the surrounding text (that is, within the paragraph, as if the whole tabular environment was a character), so if you want your alignment displayed by itself, put it between paragraphs, inside a positioning environment like center, flushright, or flushleft, or leave a blank line or \par before and after so it gets typeset separately.

One side-effect of this is that small and intricately constructed micro-tabulations can be used to good effect when creating special effects like logos, as they they get treated like a character and can be typeset anywhere.

\begin{tabular}{
@{}
>{\raggedright}p{.5\textwidth}
@{}
>{\raggedleft\arraybackslash}p{.5\textwidth}
@{}}
left-hand material
&
right-hand material\\
\end{tabular}


Tabular setting can also be used wherever you need to align material side by side, such as in designing letterheads, where you may want your company logo and address on one side and some other information on the other side to line up with each other. One common way to implement ‘spring’ margins like this is to create two columns each half the width of the page, allowing for the extra space that would otherwise be added automatically between columns and at the edges:

As mentioned earlier, the @{} suppresses the inter-column gap (or the shoulder left or right) so that the total width available will be the full text width of the page.

### Exercise 13: Create a tabulation

Create one of the following in your document:

1. a formal Table with a caption showing the number of people in your class broken down by age and sex;

2. an informal tabulation showing the price for three products;

3. the logo (hint: § 4.6.2)