Skip to content

LaTeX 图片 ​

graphicx 负责插入、缩放、裁剪和旋转图片,figure 环境负责标题、编号和浮动位置。只需要显示图片时可以直接使用 \includegraphics。通常把需要标题或交叉引用的图片放进 figure。也可用 caption 的 \captionof 创建非浮动标题,具体方式应服从文档模板。

1 插入图片 ​

1.1 加载宏包 ​

latex
\usepackage{graphicx}

最简单的写法如下:

latex
\includegraphics{figures/topology.pdf}

图片路径区分大小写。扩展名可以省略,便于在不修改正文的情况下替换图片格式。

1.2 设置图片宽度 ​

latex
\includegraphics[width=0.8\linewidth]{figures/topology}

论文中优先使用 \linewidth 控制宽度。它会随当前环境变化,在单栏、双栏和子图中都比固定厘米值更容易复用。

同时限制宽度和高度时,应保留纵横比:

latex
\includegraphics[
  width=\linewidth,
  height=0.35\textheight,
  keepaspectratio
]{figures/topology}

1.3 统一图片路径 ​

图片较多时,可以在导言区设置搜索目录:

latex
\graphicspath{{figures/}{images/}}

每个目录都需要单独放在一组花括号中。设置后可以简写为:

latex
\includegraphics[width=0.8\linewidth]{topology}

2 浮动图片 ​

2.1 基本结构 ​

latex
\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.8\linewidth]{figures/topology}
  \caption{Testbed topology}
  \label{fig:topology}
\end{figure}

常用位置参数如下:

参数含义
h当前文字附近
t页面顶部
b页面底部
p单独的浮动体页面
!放宽部分限制

[htbp] 表示放置偏好,实际位置由 LaTeX 根据页面剩余空间、浮动体数量和排版参数决定。

2.2 交叉引用 ​

\label 应放在 \caption 后面,确保记录的是当前图片编号:

latex
As shown in Figure~\ref{fig:topology}, the switches form a tree.

~ 是不可断行空格,可以避免 “Figure” 和编号分到两行。

2.3 限制浮动范围 ​

确实不希望图片越过某个位置时,可以使用 placeins:

latex
\usepackage{placeins}

% 正文和图片
\FloatBarrier

\FloatBarrier 会先排出此前尚未放置的浮动体,可用于控制章节边界处的浮动位置。

需要强制放在当前位置时,可以使用 float 提供的 [H]:

latex
\usepackage{float}

\begin{figure}[H]
  % ...
\end{figure}

[H] 固定图片位置,会限制 LaTeX 调整页面的空间。图片放不进当前页时,可能留下较大的空白。

3 标题设置 ​

3.1 标题样式 ​

caption 宏包可以统一设置图片标题:

latex
\usepackage{caption}

\captionsetup[figure]{
  font=small,
  labelfont=bf,
  labelsep=period
}

局部设置只影响当前图片:

latex
\begin{figure}[htbp]
  \captionsetup{justification=centering}
  % ...
\end{figure}

投稿时,标题格式按模板提供的设置调整。

3.2 目录中的短标题 ​

标题较长时,可以为图片目录提供短标题:

latex
\caption[Testbed topology]{
  Testbed topology and the connections between all programmable switches.
}

方括号中的内容写入图片目录,花括号中的内容显示在图片下方。

4 子图 ​

4.1 基本写法 ​

使用 subcaption 提供的 subfigure 环境:

latex
\usepackage{subcaption}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.47\linewidth}
    \centering
    \includegraphics[width=\linewidth]{figures/baseline}
    \caption{Baseline}
    \label{fig:baseline}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.47\linewidth}
    \centering
    \includegraphics[width=\linewidth]{figures/proposed}
    \caption{Proposed method}
    \label{fig:proposed}
  \end{subfigure}
  \caption{Latency comparison}
  \label{fig:latency-comparison}
\end{figure}

两个子图宽度与中间间距之和不能超过当前行宽。\hfill 会把剩余空间放在两个子图之间。

4.2 引用子图 ​

latex
Figure~\ref{fig:latency-comparison} compares both methods.
The result in Figure~\ref{fig:proposed} has lower latency.

引用整个组合图时使用外层标签,引用单个子图时使用对应 subfigure 中的标签。

subcaption、subfig 和已经过时的 subfigure 提供相近功能,同时加载容易造成命令或标题格式冲突。选择模板支持的一种子图方案即可。

5 裁剪与旋转 ​

5.1 裁剪空白 ​

latex
\includegraphics[
  width=0.8\linewidth,
  trim={20 10 20 10},
  clip
]{figures/result.pdf}

trim 的顺序为左、下、右、上。省略单位时按 bp(1/72 英寸)解释,也可以明确写成 20bp 10bp 20bp 10bp。只有同时加入 clip,超出裁剪区域的内容才会被隐藏。

5.2 旋转图片 ​

latex
\includegraphics[
  origin=c,
  angle=90,
  width=0.6\linewidth
]{figures/result.pdf}

origin=c 表示围绕图片中心旋转。选项从左到右处理,示例先旋转再限制最终宽度。若把 width 放在 angle 前面,限制的就是旋转前的宽度。整页横向图片较多时,使用专门的横向页面环境通常比逐张旋转更容易维护。

6 双栏与环绕图片 ​

6.1 跨双栏图片 ​

双栏文档中,普通 figure 只占一栏。跨越两栏使用 figure*:

latex
\begin{figure*}[t]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/architecture}
  \caption{System architecture}
  \label{fig:architecture}
\end{figure*}

标准双栏模式下,figure* 通常放在页顶或浮动体专页。页底双栏浮动需要文档类或额外宏包支持,再通过 [b] 请求页底放置。使用前核对投稿模板的要求。

6.2 文字环绕 ​

使用 wrapfig 可以让正文环绕较小的图片:

latex
\usepackage{wrapfig}

\begin{wrapfigure}{r}{0.4\textwidth}
  \centering
  \includegraphics[width=0.38\textwidth]{figures/device}
  \caption{Test device}
  \label{fig:device}
\end{wrapfigure}

环绕图片应放在相关段落之前,并避开章节标题、列表和其他浮动体。正文太短时,图片周围容易出现不自然的空白。

7 图片格式与清晰度 ​

  • 曲线图、拓扑图和架构图优先使用 PDF 等矢量格式,缩放后边缘仍然清晰。
  • 截图和带透明区域的图像适合 PNG。
  • 照片适合 JPEG,使用高质量原图导出。
  • 位图按最终排版尺寸导出,并提供足够的像素分辨率。
  • 图中的字体、线宽和颜色在导出阶段统一。

SVG 图片可预先转换为 PDF,或者在编译环境允许调用外部程序时使用 svg 宏包。

8 完整示例 ​

下面的 example-image-a 和 example-image-b 来自 mwe 宏包,是排版占位图。实际使用时替换为自己的图片路径。

latex
\documentclass[UTF8]{ctexart}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{mwe}

\captionsetup[figure]{
  font=small,
  labelfont=bf,
  labelsep=period
}

\begin{document}

图~\ref{fig:comparison} 对比了两组实验结果。

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.47\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{基线方法}
    \label{fig:example-baseline}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.47\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{改进方法}
    \label{fig:example-proposed}
  \end{subfigure}
  \caption{两种方法的实验结果对比}
  \label{fig:comparison}
\end{figure}

\end{document}

9 常见问题 ​

9.1 找不到图片 ​

检查相对路径、文件名大小写和扩展名。项目从主文件所在目录编译时,图片路径通常相对于主文件所在目录,或通过 \graphicspath 配置的目录查找。

9.2 图片超出版心 ​

把宽度限制为当前环境的行宽:

latex
\includegraphics[width=\linewidth]{figures/result}

子图内部也应使用 \linewidth,此时它表示子图自身的宽度。

9.3 图片没有出现在源码位置 ​

LaTeX 会根据页面空间安排浮动体。先检查图片是否过大,再尝试 [!htbp] 或在章节边界使用 \FloatBarrier。

9.4 引用编号错误 ​

确保 \label 位于 \caption 后,并至少编译两次,让 LaTeX 写入并读取交叉引用信息。

9.5 图片模糊 ​

清晰度取决于原始文件的分辨率和导出质量。检查原图,必要时按最终排版尺寸重新导出。

9.6 图片周围有大块空白 ​

空白可能来自图片文件本身,也可能由浮动位置造成。前者可在导出时去除画布边缘,或使用 trim 和 clip。后者应调整图片尺寸和浮动参数。

Released under the MIT License.