LaTex 允许图形和表格浮动以增强排版效果。不过,偶尔也会希望一幅图形不要浮动,不过处理起来还是有点麻烦的,即使使用了 [h] 选项,浮动体还是会乱跑,但如果去掉 \begin{figure} \end{figure} 来一个裸的 \includegraphics 又不能结合 \caption 命令加标题。而 figure 和 table 之所以能够使用 \caption 命令是因为定义了 \@captype 命令,也就是说,如果我们自己定义一下的话也是可以起到同样的效果的。要想做到这一点,首先在导言区中加入如下的命令:
\makeatletter
\newcommand{\figcaption}{\def\@captype{figure}\caption}
\newcommand{\tabcaption}{\def\@captype{table}\caption}
\makeatother
其中加入 \makeatletter,\makeatother 的原因是命令中有@符号。
这样在正文中无论是否在图形环境中,都可用 \figcaption 来得到图形标题。 同样地,无论是否在表格环境中,都可用 \tabcaption 来得到表格标题。 下面的命令
This is the text before the figure.
\\[\intextsep]
\begin{minipage}{\textwidth}
\begin{minipage}{\textwidth}
\centering
\includegraphics[width=2in]{graphic.eps}%
\figcaption{This is a non-floating figure}
\label{fig:non:float}
\end{minipage}
\\[\intextsep]
This is the text after the figure.
可得到一幅不浮动的图形。对于不浮动的图形,需要注意下面几点:
- 需要使用小页环境(minipage)来防止在图形中出现分页的情况。
- 命令 \\[\intextsep] 开始一新行并在图形的前后加上任意大小垂直的空白。\intextsep 被用来使不浮动的图形具有与浮动图形相同的上下间距。
- 一般地,浮动图形是按照它们在 LaTex 源文件中的顺序一一被放置的。而不浮动的图形是被立即放置到页面上,所以可能会出现图形顺序错误的情况,图形出现的顺序被打乱。要避免这种顺序错乱,可在不浮动的图形前用 \clearpage 或 \FloatBarrier 命令清除未处理的浮动图形。
- \figcaption 和 \tabcaption 在生成边注图形以及与图形并列的表格时会很有用。
Mark