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.
\mathchoice{displaystyle}{textstyle}{scripststyle}{scriptscriptstyle}
.^
and_
. Can you add some motivation for your problem?\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.\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.