6
Float Environments

6.1 The figure Environment

Figures are really problematical to present in a document as they never split between pages. This leads to bad page breaks which in turn leave blank space at the bottom of pages. For fine-tuning that document, the typesetter 1 1 That is us. has to adjust the page breaks manually. Fortunately for us, LaTeX provides floating figures which automatically move to suitable locations. So the positioning of figures is the duty of LaTeX.

6.1.1 Creating floating figures

Floating figures are created by putting commands in a figure environment. The contents of the figure environment always remains in one chunk, floating to produce good page breaks. The following commands put the graphic from figure.eps inside a floating figure:

latex
\begin{figure} \centering \includegraphics{figure.eps} \caption{This is an inserted EPS graphic} \label{fig1} \end{figure}

Let’s look at what is going on here:

6.1.2 Figure Placement

The figure environment has an optional argument which allows users to specify possible figure locations. The optional argument can contain any combination of the letters: h, t, b, p.

h

Place the figure in the text where the figure command is located. This option cannot be executed if there is not enough room remaining on the page.

t

Place the figure at the top of the page.

b

Place the figure at the bottom of a page.

p

Place the figure on a page containing only floats.

If no optional arguments are given, the placement options default to [tbp].

When we input a float, LaTeX will read that float and hold it until it can be placed at a better location. Unprocessed floats are those which are read by LaTeX but have not yet been placed on the page. Though the float-placing is done by LaTeX, sometimes the user has to invoke commands to process unprocessed floats.

Following commands will do that job:

\clearpage

This command places unprocessed floats and starts a new page.

\FloatBarrier

This command causes all unprocessed floats to be processed. This is provided by the placeins package. It does not start a new page, unlike \clearpage.

Since it is often desirable to keep floats in the section in which they were issued, the section option

latex
\usepackage[section]{placeins}

redefines the \section command, inserting a \FloatBarrier command before each section.

This option is very strict and does not allow a float from the previous section to appear at the bottom of the page, since that is after the start of a new section.

The below option

latex
\usepackage[below]{placeins}

is a less-restrictive version of the section option. It allows floats to be placed after the beginning of a new section, provided that some of the previous section appears on the page.

\afterpage/\clearpage

The afterpage package provides the \afterpage command which executes a command at the next naturally-ocurring page break.

Therefore, using \afterpage{\clearpage} causes all unprocessed floats to be cleared at the next page break.

\afterpage{\clearpage} is especially useful when producing small floatpage figures.

6.1.3 Customising the float placement

The following style parameters are used by LATEX to prevent awkward-looking pages which contain too many floats or badly-placed floats.

\topnumber

The maximum number of floats allowed at the top of a text page (the default is 2).

\bottomnumber

The maximum number of floats allowed at the bottom of a text page (the default is 1).

\totalnumber

The maximum number of floats allowed on any one text page (the default is 3).

These counters prevent LaTeX from placing too many floats on a text page. These counters do NOT affect float pages. Specifying a ! in the float placement options causes LaTeX to ignore these parameters. The values of these counters are set with the \setcounter command. For example,

latex
\setcounter{totalnumber}{2}

prevents more than two floats from being placed on any text page.

Figure Fractions

The commands given below control what fraction of a page can be covered by floats

In this context, “fraction” refers to the height of the floats divided by \textheight.

The first three (3) commands pertain only to text pages, while the last command pertains only to float pages. Specifying a ! in the float placement options causes LaTeX to ignore the first three parameters, but \floatpagefraction is always used. The value of these fractions are set by \renewcommand. For example,

latex
\renewcommand{\textfraction}{0.3}

Looking at this in more detail:

\textfraction

The minimum fraction of a text page which must be occupied by text. The default is 0.2, which prevents floats from covering more than 80% of a text page.

\topfraction

The maximum fraction of a text page which can be occupied by floats at the top of the page. The default is 0.7, which prevents any float whose height is greater than 70% of \textheight from being placed at the top of a page.

\bottomfraction

The maximum fraction of a text page which can be occupied by floats at the bottom of the page. The default is 0.3, which prevents any float whose height is greater than 40% of \textheight from being placed at the bottom of a text page.

\floatpagefraction

The minimum fraction of a float page that must be occupied by floats. Thus the fraction of blank space on a float page cannot be more than 1-\floatpagefraction. The default is 0.5.

6.1.4 Using Graphics in LaTeX

Now we got the formalities out of the way let’s look at how graphics can be handled in LaTeX documents. While LaTeX can import virtually any graphics format, Encapsulated PostScript (EPS) is the easiest graphics format to import into LaTeX. The “eps” files are inserted into the file using command \includegraphics[]{file.eps}.

The includegraphics command

The syntax of it is as follows:

latex
\includegraphics[options]{filename}

The following options are available in \includegraphics command:

width

The width of the graphics,

height

The height of the graphics,

totalheight

The totalheight of the graphics,

scale

Scale factor for the graphic (i.e., specifying scale=2 makes the graphic twice as large as its natural size.)

angle

Specifies the angle of rotation, in degrees, with a counter-clockwise (i.e., anti-clockwise) rotation being positive.

Graphics Search Path

By default, LaTeX looks for graphics files in any directory on the TEX search path. In addi- tion to these directories, LaTeX also looks in any directories specified in the \graphicspath command. For example,

latex
graphicspath{{dir1/}{dir2/}}

tells LaTeX to look for graphics files also in dir1/ and dir2/.

Graphics Extensions

The \DeclareGraphicsExtensions command tells LaTeX which extensions to try if a file with no extension is specified in the \includegraphics command. For convenience, a default set of extensions is pre-defined depending on which graphics driver is selected.

latex
\DeclareGraphicsExtensions{.eps,.ps,.eps.gz,.ps.gz,.eps.Z}

With the above graphics extensions specified, \includegraphicsfile first looks for file.eps, then file.ps, then file.eps.gz, etc. until a file is found. This allows the graphics to be specified with

latex
\includegraphics{file}

instead of

latex
\includegraphics{file.eps}

6.2 The Table Environment

The environments tabular and tabular* are the basic tools with which tables can be constructed. The syntax for these environments is [26]:

latex
\begin{tabular}[pos]{cols} rows \end{tabular} \begin{tabular*}{width}[pos]{cols} rows \end{tabular*}

Both the above environments actually create a minipage. The meaning of the above arguments is as follows:

pos

Vertical positioning arguments (see also the explanation of this argument for parboxes). It can take on the values:

t

The top line of the table is aligned with the baseline of the current external line of text.

b

The bottom line of the table is aligned with the external baseline.

With no positioning argument given, the table is centered on the external baseline.

width

This argument applies only to the tabular* environment and determines its overall width. In this case, the cols argument must contain the @ -expression @{\extracolsep{\fill}} somewhere after the first entry. For the other two (2) environments, the total width is fixed by the textual content.

cols

The column formatting argument. There must be an entry for every column, as well as possible extra entries for the left and right borders of the table or for the inter-column spacings. The possible column formatting symbols are:

Without going into more unnecessary information let’s have a look at an example:




Sample Tabular



col head col head col head



Left centered right
aligned items aligned
items items items
Left items centered right aligned



typed as following:

latex
\begin{center} \begin{tabular}{|l|c|r|} \hline \multicolumn{3}{|c|}{Sample Tabular} \\ \hline col head & col head & col head \\ \hline Left & centered & right \\\cline{1-2} aligned & items & aligned \\\cline{2-3} items & items & items \\\cline{1-2} Left items & centered & right aligned \\ \hline \end{tabular} \end{center}

6.2.1 tabularray Package

For a long time, the tabular environment was used to build tables. However, writing tables with tabular can be troublesome for beginners and really complex tables can be near impossible to write as we have seen from the previous example. Also, tables built with the tabular environment have some typographical issues and, when color is used, can be misread by PDF ‘ readers. Therefore, more and more LaTeX users are calling to use the tblr environment from the tabularray package instead.

The tblr environment

The tblr environment, like HTML and CSS, separates the content and the style of table cells. To do so, it uses keyval arguments to define options for columns, rows or cells. A keyval argument is an argument that takes the form of:

latex
option1=value, option2=value

If a value contains commas, it must be surrounded by brackets in this way:

latex
mylist = {element1, element2, and element 3}, option2=value

The tblr environment takes a single mandatory argument, a keyval argument containing a list of options for styling the table. Inside the environnment, each column is separated by &. Finally, each row is separated by the command \\. A really basic table would be this:

A1 B1 C1
A2 B2 C2
A3 B3 C3

which is typed as follows:

latex
% \usepackage{tabularray} \begin{tblr}{} A1 & B1 & C1 \\ A2 & B2 & C2 \\ A3 & B3 & C3 \end{tblr}

In the mandatory argument, you can define options for columns, rows and cells using those keys:

Key Descriptions
cells Options for all cells
cell{<Row- Number>}{<Column- Number>} Options for specific cells
columns Options for all columns
column{<Column- Number>} Option for specific columns
rows Options for all rows
row{<Row- Number>} Options for specific rows

The values <Row-Number> and <Column-Number> can be a single number, 2 2 1, for the first column or row. a range (1-3) or a special value (like odd or even).

The characters X, Y and Z are converted to the indexes of the last three child, respectively.

The following example use the key bg to set the color of the background of each cell and fg to set the color of the text:

latex
% \usepackage{xcolor} % \usepackage{tabularray} \begin{tblr}{ row{odd} = {bg = red}, cell{2}{2-3} = {bg = green}, column{1} = {bg = blue, fg = white} } A1 & B1 & C1 \\ A2 & B2 & C2 \\ A3 & B3 & C3 \end{tblr}

This would result in the following colorful table:

A1 B1 C1
A2 B2 C2
A3 B3 C3
Some Basic Options

We already saw two options which can be used to style table cells. However, there are much more. Here are some of them:

Key Value Description
bg Background color Colour name
fg Color of the text Color name
halign Horizontal alignment of the text l (left), c (center), r (right)
valign Vertical alignment of the text t (top), m (middle), b (bottom), h (head), f (foot)

As a trick, you can omit the name of the keys bg, halign and valign and simply enter their value, separated by a comma:

latex
% \usepackage{color} % \usepackage{tabularray} \begin{tblr}{ row{odd} = {red}, cell{2}{2-3} = {green}, column{1} = {blue, fg = white}, cell{3}{1} = {r}, cell{2}{3} = {r}, cell{2}{2} = {c}, cell{3}{3} = {c} } A very very long text & A very very long text & A very very long text \\ Left & Center & Right \\ Right & Left & Center \end{tblr}

which is typed as following:

A very very long text A very very long text A very very long text
Left Center Right
Right Left Center
Adding Borders

To add borders to the table, we use other “superkeys” like cell or row. However, instead, we use hlines to set all horizontal borders, hline to set specific horizontal borders, vlines to set all vertical borders and vline to set specific vertical borders.

With those superkeys, we can set several options: the thickness of the border, the color of the border and its style ( solid, dashed or dotted ). If the brackets are empty, a normal black border will be used. See this basic example:

A1 B1 C1
A2 B2 C2
A3 B3 C3

typed as:

latex
% \usepackage{tabularray} \begin{tblr}{ vlines = {}, hlines = {} } A1 & B1 & C1 \\ A2 & B2 & C2 \\ A3 & B3 & C3 \end{tblr}

or this more complex example:

_ _ _
A1 B1 C1
A2 B2 C2
A3 B3 C3

implemented using:

latex
% \usepackage{color} % \usepackage{tabularray} \begin{tblr}{ vline{1,4} = {3pt}, hline{1,4} = {}, hline{2} = {blue,dotted} } A1 & B1 & C1 \\ A2 & B2 & C2 \\ A3 & B3 & C3 \end{tblr}
Merging Cells

It is way easier to merge columns or rows with tblr than tabular. To do so, you only have to use the r key to set the number of rows or c to set the number of cells.

you can only use these keys with the cell superkey and they must be surrounded by distinct brackets.

Also, unlike with tabular, you don’t omit ampersands. See the following example:

A1
A2 B2 C2 D2
B3 C3
B4

Typed as following:

latex
% \usepackage{tabularray} \begin{tblr}{ vlines = {}, hlines = {}, cell{1}{1} = {c = 4}{halign = c}, cell{2}{1} = {r = 3}{valign = m}, cell{3}{3} = {c = 2, r = 2}{c,m} } A1 & & & \\ A2 & B2 & C2 & D2 \\ & B3 & C3 & \\ & B4 & & \end{tblr}