0

Is it possible to detect if a minipage is present in an encapsulating minipage?

Let me expand on that:

I have a float environment which, simplified, can be defined by \begin{figure}[th]\begin{minipage}, \end{minipage}\end{figure}.

I have another float environment subfig which can be reduced to a subfigure environment, from the subcation package, which is simply a minipage.

I'd like to automatically introduce a \hfill before the \begin{subfigure} if another subfigure is already in there. However I have no idea how to test that... Does anyone have?

If that interest anyone, here is the actual code (with subcaption package, and where [arg] are optional, and {arg} mandatory):

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

Thanks to a friend, I have a solution: save the \linewidth length (plus a small quantity, to have strict inequalities) in the fig environment, and compare the current \linewidth length in the subfig environment with the saved one in order to add, or not, the \hfill.

The code become:

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

(also added a position option in fig environment)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.