检测封装小页面内的(另一个)子小页面?

检测封装小页面内的(另一个)子小页面?

是否可以检测封装小页面中是否存在小页面?

让我进一步阐述一下:

\begin{figure}[th]\begin{minipage}我有一个浮点环境,简化地说,可以用,来定义\end{minipage}\end{figure}

我有另一个浮动环境,它可以从子包中subfig缩减为一个环境,它只是一个小页面。subfigure

如果另一个子图已经存在,我想自动\hfill在之前引入一个。但是我不知道如何测试它...有人做过吗?\begin{subfigure}

如果有人感兴趣的话,这里是实际的代码(带有 subcaption 包,其中 [arg] 是可选的,而 {arg} 是必需的):

\makeatletter
%% figure environment
%% use example %%
%   \begin{fig}[fig:label]{figure caption}
%       figure...
%   \end{fig}
%% end example %%
\newsavebox\ContentFig
\newenvironment{fig}[2][@]
{%
    \if#1@
        \newcommand{\ContentCaptionLbl}{}%
    \else%
        \newcommand{\ContentCaptionLbl}{\label{#1}}%
    \fi%
    \noindent\ignorespaces%
    \begin{figure}[th]%
        \newcommand{\ContentCaption}{\unskip{#2}}%
        \begin{lrbox}{\ContentFig}%
            \begin{minipage}{\linewidth}%
            \centering%
}{%
            \end{minipage}%
        \end{lrbox}%
        \parbox{\linewidth}{\centering\usebox\ContentFig}%
        \caption{\ContentCaption}\ContentCaptionLbl%
    \end{figure}%
    \global\let\ContentCaptionLbl\undefined%
    \global\let\ContentCaption\undefined%
}

%% sub-figure environment
%% use example %%
%   \begin{fig}[fig:figurelabel]{figure caption}
%       \begin{subfig}[fig:subfig1label]{sub-figure caption}[width]
%           figure1...
%       \end{subfig}
%       \begin{subfig}[fig:subfig2label]{sub-figure caption}[width]
%           figure2...
%       \end{subfig}
%   \end{fig}
%% end example %%
\newsavebox\ContentSubFig
\newenvironment{subfig}[1][@]{%                 % takes opt. arg for label
    \if#1@%
        \newcommand{\ContentSubCaptionLbl}{}%
    \else%
        \newcommand{\ContentSubCaptionLbl}{\label{#1}}%
    \fi%
    \begin{@subfig}
}{
    \end{@subfig}
    \global\let\ContentSubCaptionLbl\undefined%
}
\newenvironment{@subfig}[1]{%                   % takes mand. arg for caption
    \newcommand{\ContentSubCaption}{#1}%
    \begin{@@subfig}
}{
    \end{@@subfig}
    \global\let\ContentSubCaption\undefined%
}
\newenvironment{@@subfig}[1][@]{%               % takes opt. arg for width
    \newlength{\subfigwidth}%
    \if#1@%
        \setlength{\subfigwidth}{0.48\linewidth}%
    \else%
        \setlength{\subfigwidth}{#1}%
    \fi%
    \begin{subfigure}[b]{\subfigwidth}%
        \begin{lrbox}{\ContentSubFig}%
            \centering%
}{%
        \end{lrbox}%
        \parbox{\linewidth}{\centering\usebox\ContentSubFig}%
        \caption{\ContentSubCaption}\ContentSubCaptionLbl%
    \end{subfigure}%
    \global\let\subfigwidth\undefined%
}
\makeatother

答案1

感谢一位朋友,我有一个解决方案:\linewidth在 fig 环境中保存长度(加上少量,以具有严格的不等式),然后将\linewidthsubfig 环境中的当前长度与保存的长度进行比较,以便添加或不添加\hfill

代码变成:

\newenvironment{fig}[2][@]
{%
    \if#1@
        \newcommand{\ContentCaptionLbl}{}%
    \else%
        \newcommand{\ContentCaptionLbl}{\label{#1}}%
    \fi%
    \newcommand{\ContentCaption}{\unskip{#2}}%
    \newlength{\parentFigWidth}%                    % to automagically add \hfill in subfigs
    \setlength{\parentFigWidth}{\linewidth}%
    \addtolength{\parentFigWidth}{2pt}%
    \begin{@fig}%
}
{
    \end{@fig}%
    \global\let\parentFigWidth\undefined%
    \global\let\ContentCaption\undefined%
    \global\let\ContentCaptionLbl\undefined%
}
\newenvironment{@fig}[1][th]
{%
    \noindent\ignorespaces%
    \begin{figure}[#1]%
        \begin{lrbox}{\ContentFig}%
            \begin{minipage}{\linewidth}%
            \centering%
}{%
            \end{minipage}%
        \end{lrbox}%
        \parbox{\linewidth}{\centering\usebox\ContentFig}%
        \caption{\ContentCaption}\ContentCaptionLbl%
    \end{figure}%
}

%% sub-figure environment
%% use example %%
%   \begin{fig}[fig:figurelabel]{figure caption}
%       \begin{subfig}[fig:subfig1label]{sub-figure caption}[width]
%           figure 1
%       \end{subfig}
%       \begin{subfig}[fig:subfig2label]{sub-figure caption}
%           figure 2
%       \end{subfig}
%   \end{fig}
%% end example %%
\newsavebox\ContentSubFig
\newenvironment{subfig}[1][@]{%             % takes opt. arg for label
    \if#1@%
        \newcommand{\ContentSubCaptionLbl}{}%
    \else%
        \newcommand{\ContentSubCaptionLbl}{\label{#1}}%
    \fi%
    \begin{@subfig}
}{
    \end{@subfig}
    \global\let\ContentSubCaptionLbl\undefined%
}
\newenvironment{@subfig}[1]{%               % takes mand. arg for caption
    \newcommand{\ContentSubCaption}{#1}%
    \begin{@@subfig}
}{
    \end{@@subfig}
    \global\let\ContentSubCaption\undefined%
}
\newenvironment{@@subfig}[1][@]{%           % takes opt. arg for width
    \newlength{\subfigwidth}%
    \if#1@%
        \setlength{\subfigwidth}{0.48\linewidth}%
    \else%
        \setlength{\subfigwidth}{#1}%
    \fi%
    \if\linewidth<\parentFigWidth%
        \hfill%
    \fi%
    \begin{subfigure}[b]{\subfigwidth}%
        \begin{lrbox}{\ContentSubFig}%
            \centering%
}{%
        \end{lrbox}%
        \parbox{\linewidth}{\centering\usebox\ContentSubFig}%
        \caption{\ContentSubCaption}\ContentSubCaptionLbl%
    \end{subfigure}%
    \global\let\subfigwidth\undefined%
}

(还在图环境中添加了位置选项)

相关内容