答案1
其实并不容易,因为我们需要注意下标可能出现的超调。
\documentclass{article}
\usepackage{xparse,amsmath}
\DeclareMathOperator*{\emaxop}{emax}
\NewDocumentCommand{\emax}{oe{_}}{%
\IfNoValueTF{#2}
{\emaxop\nolimits\IfValueT{#1}{^{#1}}}
{\IfNoValueTF{#1}{\emaxop_{#2}}{\emaxcomplex{#1}{#2}}}%
}
\makeatletter
\newcommand{\emaxcomplex}[2]{\mathop{\mathpalette\emax@{{#1}{#2}}}}
\newcommand{\emax@}[2]{\emax@@{#1}#2}
\newcommand{\emax@@}[3]{% #1 = style, #2 = superscript, #2 = subscript
\ifx#1\displaystyle\emax@@@{#2}{#3}\else\emaxop_{#3}^{#2}\fi
}
\newcommand{\emax@@@}[2]{%
\begingroup\m@th
\sbox0{$\displaystyle\emaxop$}%
\sbox2{$\displaystyle\emaxop_{#2}$}%
\dimen@=\dimexpr(\wd2-\wd0)/2\relax
\sbox4{$^{#1}$}%
\ifdim\wd4>\dimen@ \dimen@=\dimexpr\wd4-\dimen@ \else \dimen@=0pt\fi
\operatorname*{emax^{#1\kern-\wd4}}_{#2}\kern\dimen@
\endgroup
}
\makeatother
\begin{document}
\[
\emax y\quad\emax_{x} y\quad\emax[\mathcal{E}] y\quad
\emax[\mathcal{E}]_{x} y\quad \emax[\mathcal{E}]_{Q^{\gets},Q^{\to}} y
\]
\end{document}