I am trying to detect whether I am inside or outside a float. I know the way how caption
works. It uses the internal \@captype
. But in the example below you see that this test failed by using captionsetup{type=figure}
. Of course this feature is really fussy.
I create a minimal example which changed the internal \@endfloatbox
and \@floatboxreset
to add a bool expression.
You will see that it works in the minimal example. Now my question: Do you know any reasons why I shouldn't do it. (It's for a package on CTAN)
\documentclass{article}
\parindent0pt
\usepackage{caption}
\usepackage{etoolbox}
\makeatletter
\newbool{detect@float}
\appto\@endfloatbox{\boolfalse{detect@float}}
\preto\@floatboxreset{\booltrue{detect@float}}
\newrobustcmd*\testiffloat[1]{%
in \texttt{#1}\ \ifbool{detect@float}{Inside float}{Outside float}%
}
\newrobustcmd*\testiffloatI[1]{%
in \texttt{#1}\
\ifx\@captype\@undefined
Outside float%
\else
Inside float%
\fi
}
\makeatother
\usepackage{lipsum}
\begin{document}
\testiffloat{text}\qquad\testiffloatI{text}
\begin{figure}[!ht]
\testiffloat{figure}\qquad\testiffloatI{figure}
\end{figure}
\testiffloat{text}\qquad\testiffloatI{text}
\begin{figure*}[!ht]
\testiffloat{figure*}\qquad\testiffloatI{figure*}
\end{figure*}
\testiffloat{text}\qquad\testiffloatI{text}
\begin{flushleft}
\testiffloat{flushleft}\qquad\testiffloatI{flushleft}
\captionsetup{type=figure}
\end{flushleft}
\begin{flushleft}
\captionsetup{type=figure}
\testiffloat{flushleft}\qquad\testiffloatI{flushleft}
\end{flushleft}
\testiffloat{text}\qquad\testiffloatI{text}
\end{document}