2

I would like to provide different formatting for an xparse command, depending on whether the command is expanded in superscript, in subscript, or neither.

That is, I would like to know inside the command

\NewDocumentCommand{\command}{}{%
    % here
}

whether it is being invoked like

{}^{\command}

{}_{\command}

or

\command

A question like this has already been asked: Can I detect whether my macro is being used inside a subscript or superscript?. I would like to find a direct solution I could later expand upon, provided I'm working with latex3 and xetex, if either is of relevance here.

If there is no direct means provided by the kernel, I believe a comparison with the size of the box (?) in which \command is expanded may be a good proxy.


Thank to the comments some misconceptions dissipated and I've come up with an approach to "dimension measurement" referred to elsewhere.

In the example provided in the comments, I now have replaced the fixed-height \big| strut (vphantom) with a strut constructed as follows.

\savebox{\testbox}{\ensuremath{#1}}%
% \settodepth{\dimen0}{\testbox}%
\settoheight{\dimen1}{\testbox}%
% \addtolength{\dimen1}{\dimen0}% Not universally appropriate.
\addtolength{\dimen1}{0.375 \dimen1}%
\rule{0pt}{\dimen1}%

where \newsavebox{\testbox} is invoked outside of the macro.

This, however, does not properly set the height of superscript or subscript arguments, e.g.,

$\settoheight{\dimen0}{k}$ \the\dimen0  == ${}^{\settoheight{\dimen0}{k}}$  \the\dimen0

are identical.

It is furthermore still lacking the self-referential property desired.

21
  • 4
    You can't know that by default, but you can now what's the style with \mathchoice{displaystyle}{textstyle}{scripststyle}{scriptscriptstyle}.
    – Manuel
    Mar 2, 2015 at 12:38
  • 1
    Welcome to TeX.SX! You can't really do what you want, because TeX acts in exactly the same way when it's building the math list for a superscript or a subscript; the eventual attribution to the corresponding field is done much later than the level of macro expansion. It's possible that LuaTeX makes this information available, but with pure TeX one can achieve this only by changing the meaning of ^ and _. Can you add some motivation for your problem?
    – egreg
    Mar 2, 2015 at 12:42
  • The key to remember with \mathchoice is that, while only one style is typeset in the end, all four styles are constructed in boxes, before the right one is selected. This means that parameters set in a \mathchoice are lost upon exiting \mathchoice, because they are in essence set 4 times, and the \scriptscriptstyle version will be the last one remembered, even if that isn't the one that gets typeset. Mar 2, 2015 at 12:46
  • @Manuel: Thanks, this goes in the line of the answer by David Carlisle to the referred question. I perceive it as an indirect work-around. But I, certainly, will have to consider it.
    – user73358
    Mar 2, 2015 at 12:48
  • @egreg: Thanks for your in-depth explanation, this clarifies some misconceptions. As my motivation, consider a macro expanding to \left| \vphantom{\big|} #1 \right| which, admittedly, is a quick fix, without properly extending the height in terms of dimensions (if possible). When this command is in a regular-style expression, the result is as desired. In sub- and superscript \big| is too high. It was meant to not augment the height if #1 is higher than \big|. Maybe proper dimension measurement would dismiss the issue altogether -- I'd then like to ask for a reference.
    – user73358
    Mar 2, 2015 at 12:55

0

Your Answer

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

Browse other questions tagged or ask your own question.