# Define command with optional characters

I have the following command definition (in the MWE)

\documentclass{article}
\usepackage{amsmath}

\def\hE_#1^#2#3{\hat {\textrm{E}}_{#1}^{#2}\,[#3]}

\begin{document}
$\hE_{1}^{2}{3},\qquad % \hE^{2}{3},\qquad % \hE_{1}{3},\qquad. % \hE^{2}_{1}{3},\qquad % \hE{3}$
\end{document}


Which outputs a particular expression with superscripts and subscripts. I want to define other usages of this command that allow for the commented variants. I thought I could do this

\def\hE^#1_#2#3{\hE_{#2}^{#1}{#3}}
\def\hE^#1#2{\hE_{}^{#1}{#2}}
\def\hE_#1#2{\hE_{#1}^{}{#2}}


But that gave me a TeX capacity exceeded error. Is there a way to do this? I prefer to use internal tex definitions rather than external packages if possible.

• Probably a better idea to use xparse than to try to do this via \def – daleif Jul 7 '20 at 11:07
• Honestly? Just write \newcommand*{\hE}{\hat{\mathrm{E}}} and fill the rest manually. Much more readable code than what you are trying to define (IMO). But if you really want that you might be interested in the e argument of xparse. – campa Jul 7 '20 at 11:07
• I would like to not have to repeat \,[...] for every usage of this command. Especially since I might decide to change the spacing later or change the delimiter. IMO, my usage is more readable from the point of view of content-focused writing. – Tohiko Jul 7 '20 at 11:10
• @daleif, I am not too familiar with xparse. While I prefer to use the standard TeX engine, I am not opposed to packages if they make life much simpler. I will xparse manual and see what I can do. – Tohiko Jul 7 '20 at 11:12
• Your def will only be able to take stuff in a certain order. But the e type in xparse can handle ^_ options in any order. xparse will be worth your time. – daleif Jul 7 '20 at 11:17

You can do it with xparse. The e argument type takes a list of tokens and in the body #1 refers to the item after the first token, #2 to the item after the second token and so on. You can test whether they are present with \IfValueT.

I'd not add the \, thin space. If you prefer, add it in the following code.

\documentclass{article}
\usepackage{amsmath,xparse}

\NewDocumentCommand{\hE}{e{^_}m}{%
\mathop{}\!\hat{\mathrm{E}}%
\IfValueT{#1}{^{#1}}%
\IfValueT{#2}{_{#2}}%
[#3]%
}

\begin{document}

$\hE_{1}^{2}{3},\quad \hE^{2}{3},\quad \hE_{1}{3},\quad \hE^{2}_{1}{3},\quad \hE{3}$

\end{document}


The output with \, for comparison:

I believe it's evident that the thin space is out of place, but you're the final judge.

On the other hand, I'd simply do

\newcommand{\hE}{\mathop{}\!\hat{\mathrm{E}}}


and type in the brackets.

\hE_{1}^{2}[3],\quad

More importantly, \textrm{E} is not the right tool; \mathrm{E} is.
Why \mathop{}\!? Because this way \hE is treated as a math operator like \sin or \log. I opted to not doing \operatorname{\hat{E}}, because in this way the superscript would be too high.
• Thanks! This is exactly what I wanted. I want to combine this syntax with \DeclarePairedDelimiterXPP from mathtools to allow subscripts and superscript on the operator and also to have the possibility to change the delimiters, which is why I asked this question. – Tohiko Jul 7 '20 at 12:51