I have the following command definition (in the MWE)


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

  % \hE^{2}{3},\qquad
  % \hE_{1}{3},\qquad.
  % \hE^{2}_{1}{3},\qquad
  % \hE{3}

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


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.






enter image description here

The output with \, for comparison:

enter image description here

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


and type in the brackets.


As you see, there is no gain with the proposed syntax (other than the possibility to insert the thin space).

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.