# Can I detect whether my macro is being used inside a subscript or superscript?

I defined this macro:

\newcommand{\foldeq}[1]{\sim_{#1}}


Later on, I noticed that when it's used inside a subscript, it looks a little bit hard to tell whether it's $\sim f$ or $\sim_f$. ($\sim f$ is not meaningful, but that's beside the point.) So I added an additional macro, for use in subscripts (and in principle superscripts, though that also would not be meaningful):

\newcommand{\scriptfoldeq}[1]{\foldeq{\raisebox{-0.25mm}{$\scriptscriptstyle #1$}}}


But then it occurred to me that it would be quite convenient if there is a way to detect whether a macro is being called inside a subscript or superscript, and react accordingly. Is there?

# Update:

Using David's answer, here is what my original macro looks like now:

\newcommand{\foldeq}[1]{
\sim_{\mathchoice{}{}{#1}{\raisebox{-0.25mm}{$\scriptscriptstyle #1$}}}
% {\sim_f} looks similar to {\sim f} in subscript/superscript.
}

-

You can use \mathchoice or its wrapper \mathpalette . These don't really detect where the macro is being used, they simply set the content four times in four different styles and TeX uses the appropriate box when laying out the mathematics.
\def\test{{\mathchoice{D}{T}{1}{2}}}
$$\test {\test \over \test} \test^{\test^\test}$$
\centerline{$\test {\test \over \test} \test^{\test^\test}$}