Underscript, subscript and superscript add the same time possibly via \mathoperator* without trailing space

I have a symbol, which I want to give a underscipt, subscript and superscript.

I tried:

\DeclareMathOperator*{\wmat}{w}
\wmat_2_i^j


But now I get a space between my symbol and the sub and superscript. Can this space be removed?

Or if someone knows a different method without any use of \DeclareMathOperator: you are more than welcome.

N.B. I tried many other things, with worse result.

Here's my proposal:

\documentclass{article}
\usepackage{amsmath}

\makeatletter % access "private" commands
\newcommand{\wmat}[1][]{%
\mathop{\wmat@false\mathpalette\wmat@{#1}}%
\!%
\mathop{\wmat@true\mathpalette\wmat@{#1}}%
\nolimits
}
\newif\ifwmat@
\newcommand{\wmat@}[2]{%
\begingroup
\setbox\z@=\vtop{\offinterlineskip
\ialign{%
\hfil$\m@th##$\hfil\cr
#1\mathrm{w}\cr
\noalign{\kern1pt}
\scriptscriptstyle#2\cr
}%
}%
\ifwmat@
\sbox\tw@{$#1\mathrm{w}$}%
\dp\z@=\dp\tw@
\box\z@
\else
\vrule height \ht\z@ depth \dp\z@ width \z@
\fi
\endgroup
}

\begin{document}
\begin{center}
\fboxsep=0pt\fboxrule=0.1pt
$\wmat[2]_i^j$ $\scriptstyle\wmat[2]_i^j$ $\scriptscriptstyle\wmat[2]_i^j$
\fbox{$\wmat[2]_i^j$}
\end{center}
$\wmat[2]_i^j$

\end{document}


The centered subscript is always in \scriptscriptstyle so as to avoid it making for bad spacing.

The strategy is to define a math operator that accepts an optional argument for the centered subscript. However, this subscript should not influence the vertical size of the ‘w’, in order that the standard subscript and superscript are set to the letter.

So I typeset ‘w with the subscript’ twice, one with \wmat@false and one with \wmat@true. The first instance is used to make a phantom that has the same vertical size of ‘w with the subscript’, placed in a \mathop atom of its own. The second instance is also typeset in a \mathop atom; a \! between them removes the space that TeX adds between consecutive \mathop atoms.

I build a box containing ‘w with the subscript’ (details later); in the \wmat@false case this is used to make a vertical rule as high and deep as the box, with zero width. This will reserve the needed vertical space but produce no visible output. In the \wmat@true case the box is resized to have the same vertical dimensions as a plain ‘w’. This will be the last item that TeX will see and to which it will attach the _j^i items that follow.

Now how's the box built? I found that using \operatorname*{w}_2 places the subscript too far down below the ‘w’, so a different method is needed. I use the primitive TeX method for alignments. I open a \vtop, that is a box containing vertical material, with the reference point given by the top item inside it (it will be \mathrm{w}).

Inside the \vtop I build an \ialign (a wrapper around \halign with some initialization). This does a one column table, with no vertical space between rows (by \offinterlineskip) except the explicitly added \kern1pt. The top row contains, as said, \mathrm{w} in the needed size (via \mathpalette); the bottom row contains the subscript, always in \scriptscriptstyle.

The \vtop is assigned to a box register, so it is possible to access its dimensions (for typesetting the zero width rule in the \wmat@false case) or change them (for making it appear the same as ‘w’). Then the box is used in the \wmat@true case).

Information about \halign is found in the TeXbook or in TeX by Topic.

• @JensWagemaker xparse is a relic of a previous attempt and is not needed here. I'll add some explanations. For the @, it is customary to use it in “private” command names, so as not to conflict with user level commands. – egreg Jan 30 at 11:15
• @JensWagemaker I added some explanations. – egreg Jan 30 at 11:38
• Can you explain what \wmat@false is? Because it looks like a control sequence, but the only control sequence that is defined until the first use of \wmat@false is \wmat. – Jens Wagemaker Feb 1 at 11:43
• @JensWagemaker \wmat@false is defined by \newif\ifwmat@ and makes the conditional return false when used. Similarly for \wmat@true. – egreg Feb 1 at 11:47

You code generates a double subscript error. Perhaps what you want is this:

\documentclass{article}
\usepackage{mathtools}
\DeclareMathOperator*{\wmat}{w}
\begin{document}
$\smash{\wmat_2}\mathllap{\phantom{\wmat}}_i^j$
\end{document}


You might want to define a macro if you want to use this more than once.