Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.
}
share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

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.

enter image description here

\def\test{{\mathchoice{D}{T}{1}{2}}}

$$\test {\test \over \test} \test^{\test^\test} $$

\centerline{$\test {\test \over \test} \test^{\test^\test} $}

\bye
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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