Formatting captions and subcaptions in LaTeX

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 styleCodeExample
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 FormatResult
labelformat = emptyNo label is shown - i.e. no number or letter
labelformat = simpleShows number or letter in caption
labelformat = parensShows 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.

本文转自

http://www.peteryu.ca/tutorials/publishing/latex_captions

### 使用 `dd` 命令进行文件复制并带转换和格式化选项 `dd` 是一种强大的命令行工具,主要用于低级别的数据处理任务,例如文件复制、磁盘镜像创建以及数据转换等。通过其丰富的参数组合,可以实现复杂的文件操作需求。 #### 参数详解 以下是与文件复制及转换相关的常用参数: - **if=FILE**: 指定输入文件路径。如果未指定,则默认从标准输入读取。 - **of=FILE**: 指定输出文件路径。如果没有定义,默认会将结果发送到标准输出。 - **conv=CONVERSIONS**: 应用一组转换标志来修改数据流行为。常见的转换标志包括: - **ascii**: 转换 EBCDIC 到 ASCII 编码[^1]。 - **ebcdic**: 转换 ASCII 到 EBCDIC 编码。 - **ibm**: 转换 ASCII 到另一种 IBM 版本的 EBCDIC 编码。 - **lcase**: 将所有大写字母转为小写形式[^1]。 - **ucase**: 将所有小写字母转为大写形式[^1]。 - **swab**: 交换每一对字节位置。 - **noerror**: 遇到读取错误时忽略而不终止程序运行[^1]。 - **notrunc**: 不截断输出文件内容,仅追加新数据[^1]。 - **sync**: 如果输入块小于指定大小则填充零以补齐差异[^1]。 - **bs=BYTESIZE**: 同时设定了 ibs 和 obs 的数值,即统一规定了输入输出块大小,通常建议采用此方法简化配置流程[^1]。 #### 实际案例分析 假设存在这样一个实际应用场景:我们需要把一个名为 `source.txt` 的纯文本文档转化为全大写的版本,并将其保存至另一个叫做 `uppercase_output.txt` 的新文件当中去。那么我们可以这样编写对应的 shell script: ```bash dd if=source.txt of=uppercase_output.txt conv=ucase ``` 这条语句的作用就是打开原始文件 `source.txt` 并逐块读取其中的内容,与此同时执行大小写转换操作最后再把这些经过加工后的字符序列依次写回到目标文件 `uppercase_output.txt` 中完成整个工作流程[^1]。 另外还有一种稍微复杂一点的情况涉及到跨编码系统的互译过程比如说要把一份日志记录从原本使用的 Shift-JIS 字符集转变为 UTF-8 形式以便后续更广泛的支持兼容性考虑此时就可以借助如下所示的方式达成目的: ```bash iconv -f SHIFT_JIS -t UTF-8 input.log | dd of=output_utf8.log conv=sync,notrunc ``` 这里先调用了外部工具 iconv 来负责具体的编码映射事务接着管道传递给后面的 dd 步骤做最终落地存储安排同时加入了 sync 和 notrunc 两个额外属性确保即使原素材里含有不完整区块也能妥善处置不会丢失任何有效信息而且保留既有结构不变动只单纯增加新的条目而已。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值