2

I am currently learning, playing with xparse, and have the following demo command partially working:

\let\oldlog\log
\RenewDocumentCommand{\log}{s m e{_^}}{%
    \ensuremath{%
        \mathrm{log}%
        \IfValueT{#3}{_#3}%
        \IfValueT{#4}{^#4}%
        \IfBooleanTF{#1}{\!\left(#2\right)}{#2}%
    }%
}

My issue is the current result. What I expect is that \log{x}^{yy}_{zzz} gives:

expected

But instead, I get this:

actual

Why doesn't the embellishment argument, e{_^} capture the whole content in the braces for ^{yy} and _{zzz}?

  • What's the advantage over typing \log_{zzz}^{yy}(x)? – egreg Sep 07 '21 at 16:18
  • 1
    @egreg In this case, nothing. I am just learning xparse, and this was easy enough to do. I spent 30 or more minutes wrapping my head around this issue with embellishments, thinking it was some kind of extra layer of complexity. – TimeTravelPenguin Sep 07 '21 at 16:28
  • 1
    Just remember that the correct syntax for subscript and superscripts is always with braces, that sometimes can be omitted. More to the point, the braces around a mandatory argument are always stripped off by TeX and in this case you need to supply them back. – egreg Sep 07 '21 at 16:35
  • 1
    For an example of a non-trivial use of embellishments, see this answer: https://tex.stackexchange.com/a/602341/202780 – Don Hosek Sep 07 '21 at 16:41
  • @egreg Yup! Just one of those silly mistakes that is obvious in hindsight! – TimeTravelPenguin Sep 07 '21 at 16:44
  • You should also seriously use either \oldlog or \operatorname{log} instead of \mathrm{log}. Also avoid using \left...\right in general and stick to manual scaling. And if you must use automatic scaling, use the variants from the package mleftright where some spacing issues are being fixed. – Gaussler Sep 07 '21 at 16:47
  • @Gaussler I didn't know about \operatorname, thanks for that! I read the amsmath package and it said \mathrm is a better alternative to many others, so that is nice to know. As for \left ... \right, I have never ever had any issues with it. Can you provide some extra material as to why I should have that opinion? Thanks :) – TimeTravelPenguin Sep 07 '21 at 16:51
  • The main problem with \left...\right is that they have a tendency to make the delimiters way too big. Look at \[ \left( \sum_{n=0}^{\infty} \frac{1}{n^2} \right) \] to see what I mean. In this case, \Bigl( ... \Bigr) or \biggl(...\biggr) yield better results. Apart from that, there are certain spacing issues, but they can be fixed. – Gaussler Sep 07 '21 at 16:57
  • My recommendation on \left and \right is to prefer them over manual scaling unless otherwise necessary. They are usually correct and there are some instances where they're necessary (e.g., if you want to write the open interval ]-1,0] which will be typeset incorrectly without using \left] and \right]. Not to mention cases where \Bigg isn't bigggg enough or incorrect sizing without amsmath in anything that's not ten point. – Don Hosek Sep 07 '21 at 17:59
  • 1
    @DonHosek Well, it seems to me that most TeX gurus give the opposite recommendation. As you know, commands \left and \right don’t even give the delimiters \mathopen and \mathclose status. On top of that, \left...\right change the positioning of indices: \left(...\right)^2 vs. (...)^2. The interval ]–1,0] can be typed \mathopen]-1,0], or, preferably, via some command (e.g. via \DeclarePairedDelimiter, the interval package, or some custom solution; nobody will be surprised to learn that mine is based on semantex). – Gaussler Sep 07 '21 at 18:47
  • @Gaussler If most TeX gurus ar giving the opposite recommendation, they're wrong. See, for example, The TeXbook p. 149 (although perhaps you don't consider that author to be particularly knowledgable about TeX). You may also want to familiarize yourself with the definitions of the various biggg commands, and I have no idea where your claim about \mathopen and \mathclose comes from. That same unknowledgeable author gives as his primary suggestion for the open interval \left]\right with \mathopen and \mathclose as a secondary suggestion. – Don Hosek Sep 10 '21 at 05:08
  • @DonHosek The spacing issues with \left...\right are documented everywhere on this site. I agree that my previous formulation about not making them behave like \mathopen and \mathclose was misleading, but it doesn’t change the facts. I know that the biggg commands are defined in terms of \left...\right; that does not change the fact that \left...\right lead to both bad spacing and too big delimiters. I have no doubt that Knuth intended them to be used the way you suggest, but nowadays, the common consensus is that he made a wrong design choice in this particular case. – Gaussler Sep 10 '21 at 11:53
  • @Gaussler You're making a bunch of claims without being able to back them up. I'm not going to waste time arguing about phantom issues and phantom gurus. Your one specific claim is a spurious one. No one would say that all delimiters should be marked with \left and \right, just ones that need to be resized. (btw you misspelled wrong in your reply, it doesn't start with m-i-s-) – Don Hosek Sep 10 '21 at 13:48
  • @DonHosek What phantom claims? The spacing issues are well-documented (and there are 500 other questions here documenting the same). And @DavidCarlisle explicitly states in the comments here that there are no benefits of the \mathinner atom that is added by \left...\right. – Gaussler Sep 10 '21 at 13:56

1 Answers1

3

Ooof I worked it out; Turns out you need _{#3} and ^{#4}.

\let\oldlog\log
\RenewDocumentCommand{\log}{s m e{_^}}{%
    \ensuremath{%
        \mathrm{log}%
        \IfValueT{#3}{_{#3}}%
        \IfValueT{#4}{^{#4}}%
        \IfBooleanTF{#1}{\!\left(#2\right)}{#2}%
    }%
}