LaTeX 中具有类似方程编号的内嵌图形

发布于 2024-08-24 03:45:25 字数 2109 浏览 8 评论 0原文

出于某种原因,我认为像方程环境一样拥有内联数字(即没有浮点数)会很好。它们必须被编号,因为我希望以后能够参考它们。我提出了两种尝试,但都有其缺点。我希望得到一些反馈,可以帮助我解决问题。

第一次尝试使用 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}

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

For some reason I thought it would be nice to have inline figures (i.e. no floats) just like the equation environment. They would have to be numbered, as I want to be able to refer to them later on. I've come up with two attempts, but both have their shortcomings. I'm hoping for some feedback that can sort me out.

The first attempt uses 3 minipages (see the code below). This looks nice as the figure number is aligned vertically with the middle of the figure. However, as the width of the figure approaches the width of the page, things start to break down. Also, it doesn't behave very nicely at pagebreaks.

The second attempt uses the equation environment with a different label. Apart from the fact that I don't know if this is a sensible thing to do, it produces extra whitespace in the beginning of the next paragraph. It also doesn't align the label vertically in the center, but puts it on the bottom.

Here's an example of both attempts:

\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}

Do you guys have any better ideas, or suggestions to fix the any of the shortcomings? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-08-31 03:45:25

如果您使用 "float" 包,您可以将 H 指定为放置位置,这使其准确地显示在“此处”。

If you use the "float" package, you can specify H as a placement, which makes it appear exactly "here".

帅气称霸 2024-08-31 03:45:25
\newbox\inlinefigbox
\newenvironment{inlinefig3} 
{
\refstepcounter{figure} % increase figure number 
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}
\newbox\inlinefigbox
\newenvironment{inlinefig3} 
{
\refstepcounter{figure} % increase figure number 
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文