LaTeX 排版教程系列图片横幅 最基础
在 LaTeX \LaTeX L A T E X 里添加图片使用 graphicx 宏包,在导言区导入
然后,在需要插入图片的地方,使用如下命令
1
\includegraphics [选项] { 图形文件名}
其中,图形文件名支持包含路径,常用的选项包括:
width, height → \to → 指定图形的宽度和高度 scale → \to → 缩放因子,如 scale = 0.6 angle → \to → 制定旋转角度,逆时针,以度为单位 1
2
3
4
5
6
7
8
9
10
11
12
13
14
% 插入 MatNoble logo
\includegraphics [width = .3\textwidth] { images/logo.pdf}
% 居中 logo
{ \centering \includegraphics [width = .3\textwidth] { images/logo.pdf}
% 下方必须有一行空格才能居中
}
% 居中 logo 并逆时针旋转 37 度
{ \centering \includegraphics [width = .3\textwidth,angle=37] { images/logo.pdf}
% 下方必须有一行空格才能居中
}
graphicx 宏包支持多种常见的图片格式,但推荐使用 .eps 和 .pdf 格式,以得到更清晰的显示效果。
自动搜寻路径
以上 图形文件名 处,加上了 .tex 文件目录下的用来存储图片文件夹的目录 images,为了更方便使用,我们可以不加目录,甚至是图片后缀名。只需在导言区加入以下代码
1
2
3
4
5
6
% 如果图片没有指定后缀, 依次按下列顺序搜索
\DeclareGraphicsExtensions { .eps,.pdf,.jpg,.png}
% 设置图表搜索路径, 可以给图表文件夹取如下名字
\graphicspath {{ figures/}{ figure/}{ pictures/}
{ picture/}{ pic/}{ pics/}{ image/}{ images/}}
之后,就可以省去目录和后缀名
1
\includegraphics [width = .3\textwidth] { logo}
浮动图环境
如果想为图片自动添加编号,则需要使用..浮动图环境.. 。浮动 的意义在于:自动调整图表位置, 避免出现大片的空白
1
2
3
\begin { figure} [位置]
··· ···
\end { figure}
位置 选项的取值: h → \to → here, t → \to → top, b → \to → bottom, p → \to → page
推荐顺序:h → \to → t → \to → b → \to → p
一般情况,还要求图片居中,则可以在环境中首先加上 \centering
\begin{figure}[htbp]
\centering
··· ···
\end{figure}接着填入开始说的 \includegraphics[选项]{图形文件名} 命令。添加图片名称使用 \caption 命令。最后,使用 \label 标签及 \ref 命令实现..交叉引用..
1
2
3
4
5
6
7
8
\begin { figure} [htbp]
\centering
\includegraphics [width = .3\textwidth] { logo}
\caption { \em logo}
\label { fig:logo}
\end { figure}
figure \ref { fig:logo} is my logo
子图
在文章中通常还需要添加..子图..,此时,可以使用 subfigure 环境来实现,首先在导言区加载 \usepackage{subcaption}
子图并排
两张图片并排是最常用的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\begin { figure} [htbp]
\begin { subfigure}{ .5\textwidth }
\centering
% 子图一
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图一}
\label { fig:sub-first2}
\end { subfigure}
\begin { subfigure}{ .5\textwidth }
\centering
% 子图二
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图二}
\label { fig:sub-second2}
\end { subfigure}
\caption { 并列子图}
\label { fig:fig2}
\end { figure}
% 交叉引用
图(\ref { fig:fig2} )包括子图(\ref { fig:sub-first2} )和子图(\ref { fig:sub-second2} )
多行多列子图
实现多行子图也不难,需要注意在换行处添加一行空格
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
\begin { figure} [htbp]
\begin { subfigure}{ .3\textwidth }
\centering
% 子图一
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图一}
\label { fig:sub-first}
\end { subfigure}
\begin { subfigure}{ .3\textwidth }
\centering
% 子图二
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图二}
\label { fig:sub-second}
\end { subfigure}
\begin { subfigure}{ .3\textwidth }
\centering
% 子图三
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图三}
\label { fig:sub-third}
\end { subfigure}
% 下方需空一行
% 上方需空一行
\begin { subfigure}{ .3\textwidth }
\centering
% 子图四
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图四}
\label { fig:sub-fourth}
\end { subfigure}
\begin { subfigure}{ .3\textwidth }
\centering
% 子图五
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图五}
\label { fig:sub-fifth}
\end { subfigure}
\begin { subfigure}{ .3\textwidth }
\centering
% 子图六
\includegraphics [width=.8\linewidth] { logo.png}
\caption { 子图六}
\label { fig:sub-sixth}
\end { subfigure}
\caption { 多行多列子图}
\label { fig:fig}
\end { figure}
% 交叉引用
图(\ref { fig:fig} )包括子图(\ref { fig:sub-first} )、图(\ref { fig:sub-second} )、图 (\ref { fig:sub-third} )、图(\ref { fig:sub-fourth} )、图 (\ref { fig:sub-fifth} )和图(\ref { fig:sub-sixth} )
常见问题 (FAQ)
Q: 为什么我的图片到处乱跑,不待在插入的位置?
A : 这是 LaTeX 的“浮动”特性。如果你非要图片固定在当前位置,可以使用 float 宏包,并将位置参数设为 [H] (大写)。但通常建议让 LaTeX 自动决定最佳位置,或者使用 [htbp] 给它更多选择空间。
Q: 如何让图片跨栏显示(双栏排版中)?
A : 使用 \begin{figure*} ... \end{figure*} 环境(带星号)。注意:跨栏图片通常只能出现在页面顶部或底部,无法使用 [h] 参数。
Q: 图片标题 (Caption) 怎么改字体或字号?
A : 推荐使用 caption 宏包进行全局设置。例如:\usepackage[font=small,labelfont=bf]{caption} 可以将标题字体设为小号,且标签(如 “Figure 1”)加粗。