在LaTeX中使用类似方程编号的内联数字

时间:2010-03-05 09:55:06

标签: latex figure

出于某种原因,我认为像方程式环境一样有内联数字(即没有浮点数)会很好。它们必须编号,因为我希望以后能够引用它们。我想出了两次尝试,但两者都有缺点。我希望得到一些可以解决我的反馈意见。

第一次尝试使用3个小型(请参阅下面的代码)。这看起来不错,因为图号与图的中间垂直对齐。但是,随着图形的宽度接近页面的宽度,事情开始崩溃。此外,它在分页符时表现不佳。

第二次尝试使用具有不同标签的等式环境。除了我不知道这是否是一件明智的事情之外,它还会在下一段的开头产生额外的空格。它也不会在中心垂直对齐标签,而是将其放在底部。

以下是两次尝试的示例:

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{lipsum}

%
% Attempt 1
%
% Uses 3 minipages.
% Breaks if figure is wide, and at the bottom of a page.
%

\usepackage{calc}
\newlength{\figlabelwidth} % width of label
\newlength{\imgwidth} % max. width of figure

\newenvironment{inlinefig1}
{
 \refstepcounter{figure} % increase figure number
 \begin{center} % don't know if this is necessary
 \setlength{\figlabelwidth}{\widthof{(Fig. \thefigure)}} 
 \setlength{\imgwidth}{\textwidth - \figlabelwidth - \figlabelwidth} 
 \setlength{\imgwidth}{0.9\imgwidth} % to be on the safe side
 \begin{minipage}{\figlabelwidth}\makebox[\figlabelwidth]{}\end{minipage} % ghost minipage for centering
 \hfill
 \begin{minipage}{\imgwidth}\begin{center} % minipage for figure
}
{
 \end{center}\end{minipage}
 \hfill
 \begin{minipage}{\figlabelwidth}(Fig. \thefigure)\end{minipage} % minipage for label
 \end{center}
}

%
% Attempt 2
%
% Uses an equation environment with relabeled labels.
% Label is not centered vertically, and produces extra whitespace in the paragraph after it.
%

\def\theoldequation{\theequation} % save the old equation format

\newenvironment{inlinefig2}
{
 \refstepcounter{figure} % increase figure number
 \def\theequation{Fig. \arabic{figure}} % switch to figure numbering
 \begin{equation}
}
{
 \end{equation}
 \def\theequation{\theoldequation} % reset to old equation label format
 \addtocounter{equation}{-1} % correct the equation numbering
}

\begin{document}
\noindent \lipsum[1]
\begin{inlinefig1}
 \begin{tikzpicture}
  \draw (0,0) grid +(12,2);
 \end{tikzpicture}
\end{inlinefig1}
\lipsum[2]
\begin{inlinefig2}
 \begin{tikzpicture}
  \draw (0,0) grid +(12,2);
 \end{tikzpicture}
\end{inlinefig2}
\lipsum[3]
\end{document}

你们有没有更好的想法或建议来解决任何缺点?谢谢!

2 个答案:

答案 0 :(得分:3)

如果您使用"float"套餐,则可以将H指定为展示位置,这样会使其显示为“此处”。

答案 1 :(得分:0)

\newbox\inlinefigbox
\newenvironment{inlinefig3} 
{
\refstepcounter{figure} % increase figure number 
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}