# Figures and tables

Tables and figures are examples of entities that 'float'. They generally form too large an entity to be conveniently placed just anywhere on a page. Instead LaTeX waits so that it can put them in a convenient place: the top of a page, the bottom of a page, or on a page by itself.

Let LaTeX float tables and figures, do not try to insist that you know better.

From research by Colin Wheildon: "Seventy-seven percent said articles in which body type jumped over an illustration ..., contrary to the natural flow of reading, annoyed them. The natural expectation was that once a barrier such as an illustration ... was reached, the article would be continued at the head of the next leg of type."

See video discussion 41

## Tables

To request LaTeX to include a table use the table environment:

\begin{table}
\caption{...}
\label{...}
...
instructions for typesetting the table
(usually tabular within a center environment,
or array within a displaymath environment)
...
\end{table}

See the table of fractal dimensions at the end of Src/fractals31.tex. In the first run through, LaTeX cannot find room on page 5 for the table, and so places it on page 6 by itself. In the second run, the Table of Contents has pushed more material into the document, and now the table is placed at the top of the page.

• If your data is mostly text with some mathematics, then typeset in the tabular environment within a center environment. Typeset mathematical entries within $...$, or better is $$...$$.
• If your data is mostly mathematics, even if simply numbers, then typeset in the array environment within a displaymath environment (the arguments of the array environment are the same as those of the tabular environment). Typeset the textual entries within \text{...}, or if relatively long then \parbox{width}{\raggedright ...}.

My table has few drawn lines: this is best practice. Let the grid structure of a table work for you without the distraction of zillions of horizontal and vertical lines. Use lines sparingly.

For tables of numbers, only report as few as digits as possible to convey the message.

For example, many authors report "errors" to four significant digits, and almost always there is no justifiable reason. As copyeditor I truncate to two digits, but really one significant digit is almost always sufficient for errors. Do not report more.

From James Hartley, Academic writing and publishing: "round off the numbers so that readers can make meaningful comparisons more easily"

One may include a List of Tables in the document with the command \listoftables.

## Figures

See video discussion 42

Just as for tables, somewhere near where you need the figure, usually at the start of the first paragraph that discusses the figure, include the figure environment

\begin{figure}
\centering
... graphic LaTeX code ...
\caption{...}
\label{...}
\end{figure}
The example Src/fractals34.tex shows how to \usepackage{graphicx} in order to include a figure of the lego fractal jpeg picture Share/sflower.jpg.

In twocolumn documents a figure is usually placed within one column, but the figure* environment instead spreads a figure over both columns.

Of course, mostly scientific or engineering graphics are not pictures, but plots. There are several good possibilities for the LaTeX code that includes a plotted graph, and analogous possibilities with other computing languages:

• draw graph in Matlab, Octave, R, etc, export to postscript, convert to pdf, include in the LaTeX;
• draw graph in Matlab or Octave, export to LaTeX code, input the code into your source;
• or code 'pgfplots' commands yourself directly into your LaTeX source.
Let's look at these alternatives in reverse order---after four other comments.

One may include a List of Figures in the document with the command \listoffigures.

One could alternatively put the \caption{} and \label{} above the graphic, as then hyperlinks to the figure will move to a better position. We are already used to titles being above graphics, and I know of no research finding that captions above graphics hinder comprehension by a reader.

#### Small graphics

Place small graphics (or indeed anything) into the margin with the command \marginpar{...}, and you may need to change its width via say

\setlength{\marginparwidth}{6em}
For example, download my thumbnail picture and include it in the margin with
\marginpar{\includegraphics{tonyroberts}}
For best results with marginpars include the preamble \usepackage{marginfix}.

Alternatively, the wrapfig package empowers us to wrap text around a graph, or indeed around any rectangular area of typeset material, but use only with care, and also with the needspace package.

#### Avoid animations, movies and VR-3D

It is possible to include animations, movies, and interactive 'virtual reality' 3D graphics within pdf documents via LaTeX (see examples). However, many pdf readers do not support them, and almost no journal supports their inclusion. So avoid :(

### Stunning graphs with pgfplots

See video discussion 43

Obtain stunning quality graphics within LaTeX using the pgfplots package by invoking in the preamble

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
To get started, see this introduction, then see a host of examples at PGFPlots.net

The cost of these beautiful pgfplots is a major slow down of LaTeX as it draws the graphs (although one can 'externalize' and recover the speed).

Alternatively, draw the graph in Matlab or Octave, then export to pgfplots-LaTeX code with matlab2tikz() function. I recommend invoking with something like

cleanfigure;
matlab2tikz('filename.tex','showInfo',false ...
,'noSize',true,'parseStrings',false,'showWarnings',false ...
,'extraCode','\tikzsetnextfilename{filename}' ...
,'extraAxisOptions',string ...
)


Currently (2020) there is a bug in matlab2tikz(). If you get unwanted legends appearing in your plots, then edit line 1064 of matlab2tikz.m by replacing "legendhandle = legend(axisHandle);" with "legendhandle = axisHandle.Legend;" This replacement should omit the unwanted legends for you.

### Graphs via postscript and pdf

See video discussion 44

The usual way to include a figure in LaTeX is, as follows, to draw the graph in Matlab, Octave, R, etc, export to postscript, convert to pdf, include in the LaTeX.

Do not export to bit-image formats such as .png, .gif, .tiff, or .jpg

• Create a postscript file of the drawing from whatever application is being used to generate the figure---most publishers still require postscript figures so do not bother trying to avoid this step. For example, Src/cantor.m and Src/koch.m are Matlab programs that create postscript graphs in files Src/cantor.eps and Src/koch.eps (prefer the command print -depsc2 filename ).
• However, pdfLaTeX does not support postscript graphics! In order to seamlessly be able to use pdfLaTeX or ordinary LaTeX (especially as collaborators sometimes use the other one), and produce a quality graphic, convert to pdf using the public domain ps2pdf program (so you have two copies of the graphics file, an .eps file and a .pdf file). For example, the two files Src/cantor.pdf and Src/koch.pdf
• If the graphic is photograph-like, either because it is a photograph or because it is a surface plot with smooth graduations, then convert to jpeg rather than pdf.
• Then place in the preamble the commands
\usepackage{graphicx}
• Somewhere near where you want the figure, include the figure environment
\begin{figure}
\centering
\includegraphics{...}
\caption{...}
\label{...}
\end{figure}
where the argument of the \includegraphics command is the filename (without the extension).

• Or use this to scale the picture up/down, but only a little,
\begin{figure}
\centering
\includegraphics[scale=0.9]
{...}
\caption{...}
\label{...}
\end{figure}

See the two figures in Src/fractals32.tex

The scale=0.9 scales the figure to 90% of the size of the drawn graphic: scaling between 0.9--1.1 is fine; scaling between 0.8--1.2 is probably OK; but avoid any more drastic scalings.

I strongly urge you to generate the graphics at about the same size as they are to appear.

This sizing is so that the title, label and legend information is readable and the line thicknesses are credible (avoid grotesque scaling such as this), or this even worse example.

• Most journals, and good dissertation styles, typeset to a text-width of about 14cm, so:
• a single graph must be no more than 14cm wide;
• two side-by-side graphs no more than 7cm wide each;
• and so on.
• For two column journals or proceedings, a single graph, to fit into a single column, must be no more than about 9cm wide.

For two examples:

• Src/cantor.m and Src/koch.m show how to use Matlab's clf(), subplot('position',[.2 .2 .55 .55]) commands to generate graphics that are close to the text width, and subplot(3,3,1) for close to half the text width, respectively;
• for Maple, an internet search suggests that before you issue a plot command, invoke
plotsetup(postscript,
plotoutput=image.ps,plotoptions=
color,portrait,height=300,width=300);


Always code into a script the commands that generate a graphic.

• Even though in exploring how to generate a graphic you may use a graphical user interface, ultimately reproducible research requires that you must codify the process, including codifying the print' command that actually outputs the tex/eps/pdf/jpeg file.
• Ensure your graphics are traceable back to the source file that generated them. I recommend prefixing the name of the script file to the name of the graphic file. That is, if a script named abc.* generates a graph on topic xyz, then name the graphic file abcxyz.* For example, if using Matlab/Octave, then invoke the function mfilename as in
print('-deps',[mfilename 'xyz'])`