THE captions for figures, tables, subfigures and subtables in LaTeX can be customized in various ways using the caption and subcaption packages. You can change the fonts, numbering style, alignment and format of the captions and the caption labels.
The letters and numbers (“a”, “b” and “1”) that enumerate the captions and subcaptions are caption labels. In the above, the subfigure caption label is enclosed by parentheses and the figure caption label is separated from the caption text by a colon. This page will go over a few examples of how to change these and other aspects of the captions, including the numbering or lettering style (arabic, alphabetical or Roman numerals), the caption position and how to get captions with no label (i.e. no number and no letter).
The examples on this page use the \caption and \subcaption packages. I also have an older page which has examples using the deprecated subfig package. It is recommended to use the examples on this page; the older page is kept for reference and previous reader comments.
Getting started
To use the examples in this page, you will need the caption package and also the subcaption package (if you are working with subfigures or subtables):
\usepackage{caption}
\usepackage{subcaption}
For reference, the code that produces the figure shown in the introduction is:
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Caption 1}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Caption 2}\label{fig:1b}
\end{subfigure}
\caption{Main figure caption}\label{fig:1}
\end{figure}
The label commands allow you to generate a cross-reference in the text to the figure. The label commands assigns a name that you can reference later, which will automatically be filled with the figure or subfigure number or letter. See the section on cross references.
To create a table with two subtables, the code is very similar, except you can replace the figure environment with table and subfigure with subtable:
\begin{table}
\centering
\begin{subtable}[t]{2in}
\centering
\begin{tabular}{|l|l|l|}
\hline
100 & 200 & 300\\
\hline
400 & 500 & 600\\
\hline
\end{tabular}
\caption{Caption 1}\label{table:1a}
\end{subtable}
\quad
\begin{subtable}[t]{2in}
\centering
\begin{tabular}{|l|l|l|}
\hline
100 & 200 & 300\\
\hline
400 & 500 & 600\\
\hline
\end{tabular}
\caption{Caption 2}\label{table:1b}
\end{subtable}
\caption{Main table caption}\label{table:1}
\end{table}
Both subfigure and subtable environments allow you to specify their width. This controls how much space is “reserved” for their contents. You can replace the width with \columnwidth to make it the column width of your document. Other values are possible.
Set caption label numbering style
You can change the numbering or lettering style of the caption label by using variants of the following commands in your document:
% change the style of the caption numbering.
\renewcommand{\thetable}{\alph{table}}
\renewcommand{\thefigure}{\Alph{table}}
\renewcommand{\thesubtable}{\Roman{subtable}}
\renewcommand{\thesubfigure}{\arabic{subfigure}}
Each command specifies the label you want to modify (e.g. \thetable
) and what you want to appear as the label (e.g. \alph{table}
, which means to show the table counter as a lower case letter like a, b, c, etc.). Each type of float has its own label (\thetable
) and counter variable (table). Anything that appears after you issue these commands will have the new label numbering / lettering style.
There are five ways you can show the counters (replace counter with the actual counter you want to show, such as table):
Counter style | Code | Example |
---|---|---|
Arabic numerals | \arabic{counter} | 1, 2 |
Lower case letters | \alph{counter} | a, b |
Upper case letters | \Alph{counter} | A, B |
Lower case Roman numerals | \roman{counter} | i, ii |
Upper case Roman numerals | \Roman{counter} | I, II |
Here is an example of changing the figure and subfigure caption label numbering / lettering:
\renewcommand{\thefigure}{\Roman{figure}}
\renewcommand{\thesubfigure}{\arabic{subfigure}}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Arabic numerals}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Arabic numerals}\label{fig:1b}
\end{subfigure}
\caption{Capital Roman numerals}\label{fig:1}
\end{figure}
If your document has chapters, then the caption labels would be something like 1.1, 1.2, 2.1, etc. You can customize the numbering or lettering style in these cases. For example:
% This applies if you have chapters
\renewcommand{\thefigure}{\thechapter.\Alph{figure}} % set caption label style to 1.A
\renewcommand{\thesubfigure}{\arabic{subfigure}}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Arabic numerals}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Arabic numerals}\label{fig:1b}
\end{subfigure}
\caption{Chapter number dot figure letter}\label{fig:1}
\end{figure}
Setting caption numbering and lettering style with chaptered documents
The output differs from a standard caption in that the figure label is now a capital letter rather than a number. The subcaptions are also using arabic numerals. You can also change the period to another character if you want.
The \thechapter
command produces the chapter label, just like \thefigure
produces the figure label. By including \thechapter
in the \thefigure
command, you can reference the chapter in the figure label.
If you want to change the style of the chapter labels, you can in fact override the \thechapter
command just as you override the \thefigure
command. Alternatively, if you want to change the style of the chapter labels only for figure labels, you can do this:
\renewcommand{\thefigure}{\Alph{chapter}.\Alph{figure}}
The above examples were all using figures or subfigures, but the same ideas apply for tables and subtables. Just use \thetable
and \thesubtable
instead of \thefigure
and \thesubfigure
and use the table and subtable counters instead of figure and subfigure.
Caption package options and captionsetup
The \caption
package allows many other aspects of the caption to be modified, via either the \captionsetup
command or in the package options. These include the type of label separator (e.g. the colon in “Figure 1: Caption”), the label format (whether the number or letter is shown and whether it is shown in parentheses), the label and caption text font and style, the justification of the captions and many others.
To use these options, you can either set them when you first include the package:
% options apply to all captions
\usepackage[OPTIONS]{caption}
% applies to all subfigure and subtable captions
\usepackage[OPTIONS]{subcaption}
When you specify the options in the \usepackage
command, they apply to all the captions or subcaptions in the document.
Alternatively, you can use the \captionsetup
command within the document so that all subsequent captions have the desired properties:
\captionsetup[FLOAT_TYPE]{OPTIONS}
FLOAT_TYPE can be table, figure, subtable and subfigure and specifies what type of caption that particular \captionsetup
command applies to, so you can set different options for each of the figure, table, subfigure and subtable floats individually.
When you use the \captionsetup
command in your document, all subsequent captions will use the options you specify. Alternatively, you can put the \captionsetup
within a figure, table, subfigure or subtable environment and it will apply only within that environment.
There are several examples that follow which show the types of things that can be configured with the package options or the \captionsetup
command. The examples here are not an exhaustive listing; there are many other things you can do, so it would be worthwhile to read the caption package documentation.
Caption justification and font
The following example demonstrates \captionsetup
commands that set the caption label font and the caption text font for the figures and subfigures. It also shows how to change the alignment of the subcaptions under the subfigure.
% for figures: caption label is italic, the caption text is bold / italic
\captionsetup[figure]{labelfont=it,textfont={bf,it}}
% for subfigures: caption label is bold, the caption text normal.
% justification is raggedright (i.e. left aligned)
% singlelinecheck=off means that the justification setting is used even when the caption is only a single line long.
% if singlelinecheck=on, then caption is always centered when the caption is only one line.
\captionsetup[subfigure]{labelfont=bf,textfont=normalfont,singlelinecheck=off,justification=raggedright}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Caption}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Caption}\label{fig:1b}
\end{subfigure}
\caption{Main figure caption.}\label{fig:1}
\end{figure}
Caption font and justification can all be changed with the captionsetup commands
Note that the subcaptions are left aligned under the subfigure, and the font styles have all changed:
The figure caption labels is italic, the caption text is bold and italic. The subfigure label is bold, the text is normal and it is aligned left. As explained in the code block, singlelinecheck is turned off so that even short, one line captions use the justification setting. Otherwise, it will always be centered.
You can also set up the above options for all captions and subcaptions by using the appropriate options when you include the caption and subcaption packages:
% will apply to all captions
\usepackage[labelfont=it,textfont={bf,it}]{caption}
% will apply to all subcaptions
\usepackage[labelfont=bf,textfont=normalfont,singlelinecheck=off,justification=raggedright]{subcaption}
Label format and label separator
If you look at the captions, you can see that the subfigure labels are surrounded by parentheses and a colon separates “Figure 1” from the rest of the caption.
Both of these aspects (the label format and the label separator) can be customized by options in the caption package. The label format controls how the label shows up: whether it is visible at all, appears plainly or enclosed by parentheses. The label separator is simply the character that appears after the label.
This is the format of the \captionsetup
command to set the labelformat and labelsep options:
\captionsetup[FLOAT_TYPE]{labelformat=simple, labelsep=colon}
FLOAT_TYPE
can be table, figure, subtable and subfigure.
The labelformat option can be set to:
Label Format | Result |
---|---|
labelformat = empty | No label is shown - i.e. no number or letter |
labelformat = simple | Shows number or letter in caption |
labelformat = parens | Shows number or letter in caption in parentheses - i.e. (1), (A) |
The labelsep
option can be set to:
Label Separator |
---|
labelsep = none |
labelsep = colon |
labelsep = period |
labelsep = space |
labelsep = quad |
labelsep = newline |
Here is an example where the labelformat and labelsep for the figure caption and subfigure caption are changed individually:
% set up labelformat and labelsep for figure
\captionsetup[figure]{labelformat=parens, labelsep=newline}
% set up labelformat and labelsep for subfigure
\captionsetup[subfigure]{labelformat=simple, labelsep=colon}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Simple}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{Simple}\label{fig:1b}
\end{subfigure}
\caption{Parentheses, newline separator.}\label{fig:1}
\end{figure}
The labelformat of the subfigure captions is set to simple, which produces only the caption letter without parentheses. The labelsep is colon for subfigure captions and a newline for the figure.
Subfigure and subtable captions with no number or letter
By setting the labelformat option to “empty” in your \captionsetup
commands or in the \subcaption
package options, you can disable the display of the number or letter label in subfloats. Here is an example that produces subfigures with no caption numbering or lettering:
% no subfigure caption label.
\captionsetup[subfigure]{labelformat=empty}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{No caption label}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\includegraphics[width=1in]{placeholder}
\caption{No caption label}\label{fig:1b}
\end{subfigure}
\caption{Main figure caption.}\label{fig:1}
\end{figure}
As you can see, there is no bloody A, B, C or D!
The same command applies for subtables, tables and figures (just make the appropriate substitution in the captionsetup
command).
For figures and tables, which appear in the List of Figures or List of Tables, setting the label format to empty will simply prevent the display of the number or letter in the figure or table caption itself. The letter or number will still appear in the List of Figures or List of Tables.
No caption number for figures and tables
You can disable the caption number for figures and tables by using the \caption*
command:
\begin{figure}[tbp]
\centering
\includegraphics[width=1in]{placeholder}
\caption*{Unnumbered figure caption.}
\end{figure}
The \caption*
command will produce a caption that has no label or number at all and will not appear in the List of Figures.
Set caption position for subfigures and subtables
For regular floats such as tables and figures, the caption position can be set to above or below the float by simply issuing the caption command above or below the float contents. The same applies for subfigures and subtables:
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\centering
\caption{Caption 1}\label{fig:2a}
\includegraphics[width=1in]{placeholder}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\centering
\caption{Caption 2}\label{fig:2b}
\includegraphics[width=1in]{placeholder}
\end{subfigure}
\caption{Main figure caption.}\label{fig:2}
\end{figure}
Subfigure caption is placed on top of subfloat using a captionsetup command.
A table and subtable example
This example shows the above techniques with a table that has two subtables:
% set numbering style
\renewcommand{\thetable}{\Roman{table}}
\renewcommand{\thesubtable}{\arabic{subtable}}
% set up labelformat and labelsep for table
\captionsetup[table]{labelformat=simple, labelsep=period}
% set up labelformat and labelsep for subtable
\captionsetup[subtable]{labelformat=simple, labelsep=colon}
\begin{table}
\centering
\begin{subtable}[t]{1in}
\centering
\begin{tabular}{|l|l|}
\hline
100 & 200\\
\hline
\end{tabular}
\caption{Caption 1}\label{table:1a}
\end{subtable}
\quad
\begin{subtable}[t]{1in}
\centering
\begin{tabular}{|l|l|}
\hline
100 & 200\\
\hline
\end{tabular}
\caption{Caption 2}\label{table:1b}
\end{subtable}
\caption{Table caption text}\label{table:1}
\end{table}
Cross references
You can reference the caption labels in the text by using \ref{LABEL},
where LABEL
is the label you assigned using a \label command. This will generate the number or letter corresponding to the label. Subcaptions can be referenced with \subref{LABEL}
. To generate “Figure 1(a)”:
Figure \ref{fig:1}(\subref{fig:1a})
Note that the name assigned using the \label command is arbitrary so you do not have to keep track of whether a figure is Figure 1 or not. I just called them fig:1 and fig:1a to be more clear here.
You can also reference a subfigure directly without splitting it up into ref and subref:
\ref{fig:1a})
This, however, will by default generate 1a. If you want to use 1(a), you will need to use:
% put these at the beginning of your document.
\captionsetup[subfigure]{labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}
\begin{figure}
\centering
\begin{subfigure}[t]{1in}
\includegraphics[width=1in]{placeholder}
\caption{Caption 1}\label{fig:1a}
\end{subfigure}
\quad
\begin{subfigure}[t]{1in}
\includegraphics[width=1in]{placeholder}
\caption{Caption 2}\label{fig:1b}
\end{subfigure}
\caption{Main figure caption}\label{fig:1}
\end{figure}
Figure \ref{fig:1a}
The first \captionsetup[subfigure]{labelformat=simple}
will prevent double parentheses in the figures themselves.
As before, you can replace figure with table and subfigure with subtable to work with tables and subtables.
Reset the caption counter
If you need to reset the caption counter for figures (so subsequent figures start at 1 again), you can use the following:
\setcounter{figure}{0} % reset figure counter to 0.
You may use table or the name of any other counter in lieu of figure to reset the corresponding counter.
Change the caption name
If you want to change the name of the figure or table caption from “Fig.” to “Figure” or “Table” to “Tab.” or anything else you want, you can use \renewcommand
with \figurename or \tablename:
\renewcommand{\tablename}{Tbl}
\renewcommand{\figurename}{Image}
The above will change all table captions to be called “Tbl [Table Number]” or “Image [Figure Number]”.
One potential issue you may run into is that the various document classes use a command other than \figurename
or \tablename
in the table captions. For example, the thesis document class will use \figureshortname
or \tableshortname
in the captions instead. In these cases, renew the corresponding command. To find out what the command is, you can look into the .cls file of your document class. You can look for the specific string (e.g. “Fig.”) and find out what command was defined for it. Then you can override it. You could also just create a new document class based on the original .cls file.
本文转自