Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

LaTeX Figures and Tables

LaTeX Figures and Tables

Figures and tables are the skeleton of a paper. This part covers four things: the include command and floats, basic tables, the academic-standard booktabs table, and merged cells.

LaTeX's management philosophy for figures and tables is consistent with numbering: you only place content and give a caption; position, numbering, and references are all automatic.


Figures: The graphicx Package

Including a figure needs only two commands: load graphicx in the preamble, and use \includegraphics in the body to place the image file.

Common options Meaning Example
width Width, relative values allowed width=0.8\textwidth (80% of the text width)
height Height height=5cm
scale Scale factor scale=0.5
angle Rotation angle angle=90

It is recommended not to include the extension in the file name: write sin instead of sin.png; LaTeX automatically searches by the formats supported by the current compiler (XeLaTeX supports PDF, PNG, JPG, etc.), so changing format requires no source change.

When controlling image size, prefer the proportional value width=\textwidth over scale - the former guarantees the image never exceeds the text width, while the latter may overflow unexpectedly under different font-size settings.


Floats: The figure Environment

Images should not lie directly in the body flow; the standard practice is to put them in a figure float environment.

Example: Standard Figure

\

documentclass

{

ctexart}
\usepackage{graphicx

}

             

% required package for figures

\begin

{

document}
The graph of the sine function $y = \sin x$ is shown in Figure~\ref{fig:sin}.

  \begin{figure

}[

htbp

]

             

% float environment; position parameters see table below

 

\

centering

                     

% image centered within the float

 

\

includegraphics

[

width=0.6\textwidth

]{

sin}
  \caption{Sine Function Curve

}

           

% figure caption; numbering auto-generated

 

\

label

{

fig:sin

}

                 

% label, for \ref reference

\end

{

figure}
\end{document

}

Compilation result of floating figure

The square brackets after the environment name are "position suggestions", telling LaTeX where you want it to appear.

Parameters Meaning Priority Notes
h here, at the source code position Only a suggestion; when the layout cannot fit it, LaTeX adjusts on its own
t top, at the top of the current page The most common choice for academic papers
b bottom, at the bottom of the current page Often paired with t
p float page, an independent float page When there are many figures/tables, automatically collected into an appendix page
! (e.g., [!h]) Relax typesetting constraints Use in combination when strongly required

"Why did my image go somewhere else?" - this is not a bug; it is the float mechanism at work: LaTeX chooses a position for each image that does not create large gaps. When you really need to pin the position, use the [H] parameter of the float package (capital H means here and do not move).


Basic Tables: The tabular Environment

The syntax skeleton of a table is like a matrix: & separates columns, \\ changes lines, and column formats are declared in the environment parameters.

Example: A Table with Full Borders

\

documentclass

{

ctexart}
\begin{document}
\begin{tabular}{|l|c|r|

}

         

% column format: left-aligned, centered, right-aligned, separated by vertical lines

\

hline

                           

% top horizontal line

Name

&

Age

&

Score

\

             

% & separates columns, \\ changes lines

\

hline

Zhang San

&

20

&

92

\

Li Si

&

21

&

88

\

\

hline

                           

% bottom horizontal line

\end

{

tabular}
\end{document

}

Compilation result of basic table

Column Format Symbols Meaning
l / c / r That column is left-aligned / centered / right-aligned
| Draw a vertical line at this position
p{2cm} Fixed column width, automatic line wrapping inside the cell

Academic Standard: The booktabs Three-Line Table

Open any formal publication and its tables are "three horizontal lines, zero vertical lines" - this is the three-line table, provided by the booktabs package.

Example: Three-Line Table

\

documentclass

{

ctexart}
\usepackage{booktabs

}

             

% package dedicated to three-line tables

\begin

{

document}
\begin{tabular}{lccc

}

             

% the three-line table uses no vertical lines

\toprule

                         

% top rule, thickest

Algorithm

&

Accuracy

&

Recall

&

F1

\

\midrule

                         

% middle rule, thinner

Model A

&

0.91

&

0.88

&

0.89

\

Model B

&

0.93

&

0.90

&

0.91

\

\bottomrule

                       

% bottom rule, thickest

\end

{

tabular}
\end{document

}

Compilation result of booktabs three-line table

Almost all journal and thesis formatting rules require three-line tables. Using | and \hline to build full-border tables is fine for daily homework, but before submission please switch to booktabs - vertical lines are a signal of "amateur" in professional typesetting.


Merging Cells: multirow and multicolumn

Complex headers need merging: horizontal merging uses \multicolumn (built into LaTeX), vertical merging uses \multirow (requires loading the multirow package).

Example: Two-Level Header

\

documentclass

{

ctexart}
\usepackage{booktabs}
\usepackage{multirow

}

             

% merge cells vertically

\begin

{

document}
\begin{tabular}{lcc}
\toprule
\multirow{2}{*}{Dataset} & \multicolumn{2}{c}{Accuracy} \
\cmidrule(lr){2-3

}

               

% draw partial horizontal line only on columns 2-3

 

&

Training Set

&

Test Set

\

\midrule

MNIST

&

0.99

&

0.98

\

CIFAR-10

&

0.95

&

0.91

\

\bottomrule

\end

{

tabular}
\end{document

}

Compilation result of merged-cell table

Command Syntax Description
\multirow \multirow{rows}{*}{content} Span rows vertically; leave the corresponding position in spanned rows empty
\multicolumn \multicolumn{columns}{column format}{content} Span columns horizontally; the column format of that cell must be rewritten
\cmidrule \cmidrule(lr){2-3} Partial horizontal line; (lr) leaves a little gap at both ends for a finer look

Table Captions and Overall Floating

Tables also live in floats; the environment is table, and the rest of the approach is exactly the same as figure.

Example: A Numbered Table

\begin

{

table

}[

htbp

]

 

\

centering

                       

% table centered overall

 

\

caption

{

Experimental Results Comparison

}

           

% table caption (conventionally above the table)

 

\

label

{

tab:result}
  \begin{tabular}{lcc}
    \toprule
    Dataset & Training Set & Test Set \
    \midrule
    MNIST & 0.99 & 0.98 \
    \bottomrule
  \end{tabular}
\end{table

}

Two unwritten conventions: the table caption goes above the table, the figure caption below the image; chart labels use the prefixes tab: and fig: respectively, so you can tell the type at a glance when referencing.

When a table is wider than the text width, there are two quick fixes: wrap the entire tabular in \resizebox{\textwidth}{!}{...} for proportional scaling, or use the tabularx package to auto-distribute column widths. The former is fast, the latter more standard.


Summary

Requirements Command / Environment
Figure \includegraphics[width=0.8\textwidth]{filename}
Figure/Table Float figure / table + [htbp] + \centering + \caption + \label
Table Skeleton tabular, & separates columns, \\ changes lines
Three-Line Table booktabs:\toprule \midrule \bottomrule
Merging Cells \multirow, \multicolumn
Partial Horizontal Line \cmidrule(lr){2-3}