beamer add tag
5 years ago samcarter

After reading https://www.texdev.net/2020/08/19/the-good-the-bad-and-the-ugly-creating-document-commands I’m now looking for an example how I would use \NewDocumentCommand to create an overlay-aware macro for beamer.

Top Answer
5 years ago samcarter

Here an example with both the classic \newcommand syntax and \NewDocumentCommand. The key difference is that within the macro definition the #1 needs to be surrounded by <>.

5 years
user 3.14159 replying to Joseph Wright — Saturday, 22nd Aug 2020 19:33

Sure, it always comes down to weighing options against each other. I just want to express that I am not very excited about the mess I seem to run into when my documents contain a wild mix of \newcommand and \NewDocumentCommand lines and so on. Maybe I will end up like one of those who only use plain TeX, at least I start seeing what may make them do that: you only have a manageable set of functions the pitfalls of which you need to understand.

9 hours
Joseph Wright replying to user 3.14159 — Saturday, 22nd Aug 2020 10:19

I understand your point about stars, etc., but there are places where positional arguments still ‘win’ (see for example xcoffins for comparing the two approaches)

16 hours
user 3.14159 replying to Joseph Wright — Friday, 21st Aug 2020 18:28

Well, sure, the pattern is a bit awkward, but has a clear motivation: you can super easily “beamerize” existing commands. If you were to make the overlay specification argument number 1, you would have to shift the argument number of all other arguments by 1, so it would look very different from the non-beamer command.

user 3.14159 replying to Joseph Wright — Friday, 21st Aug 2020 18:26

Right. But frankly I am using beamer a lot (I’ve been using it for 17 years) and never needed stars. On the other hand, I am pretty sure that my source codes will become more messy with the \NewDocumentCommand vs. \newcommand conundrum,. (Frankly I am not too excited about these stars, in different contexts they mean very different things. Whenever I encounter a situation in which one could use a star, I use a switch with pgf keys, To me the stars are deprecated anyway.)

8 hours
Joseph Wright replying to user 3.14159 — Friday, 21st Aug 2020 10:52

At a technical level, beamer does some slightly awkward/risky things: it’s not good with LaTeX2e robust commands. Till likely didn’t know about them when he wrote the code, and I have tightened up to some extent using e-TeX, but there are issues there.

Joseph Wright replying to user 3.14159 — Friday, 21st Aug 2020 10:51

The contrast betweenbeamer and xparse is that with the former you still can’t do stars, any kind of optional argument other than in position 1, etc. I think creating starred commands in particular is fine, so that’s an issue.

Joseph Wright replying to user 3.14159 — Friday, 21st Aug 2020 10:50

Well if you don’t like the \NewDocumentCommand interface at all, there is not much to say. What I can point to is that beamer does a couple of questionable things (for me). First, the action spec ends up as the last argument number, even though it’s clearly the first argument: that’s anti-pattern. Second, it passes the arg spec _including the <> tokens`, rather than just the ‘grabbed argument’.

13 hours
user 3.14159 replying to Joseph Wright — Thursday, 20th Aug 2020 22:02

Yes, this conversation with @samcarter is one of the scenarios. Newcomers would almost certainly conclude that beamer is crappy and the \NewDocumentCommand is the clean thing, an opinion that I do not share.

30 minutes
Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 21:32

I might add to the post, and reference this question (as I have with other parts of the post and the sources of examples)

Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 21:32

Not a problem, I should probably know these things 😃

samcarter replying to Joseph Wright — Thursday, 20th Aug 2020 21:30

Thanks a lot for the suggestion with D!

Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 21:27

That would work too, and avoids the test, yes

samcarter replying to Joseph Wright — Thursday, 20th Aug 2020 21:26

So maybe D<>{1-} will be better

Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 21:23

Probably we should fix that

samcarter replying to Joseph Wright — Thursday, 20th Aug 2020 21:22

Given that \only<>{text} fails with the same error, that seems to be the case 😃

Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 21:14

I’d not tested … perhaps Till didn’t allow an empty argument for <...>

16 minutes
samcarter replying to Joseph Wright — Thursday, 20th Aug 2020 20:58

Thanks!

samcarter — Thursday, 20th Aug 2020 20:58

Am I using this incorrectly?

samcarter replying to Joseph Wright — Thursday, 20th Aug 2020 20:57

With D<>{} I get an error:

Joseph Wright replying to user 3.14159 — Thursday, 20th Aug 2020 20:54

I’m sure you have examples in mind, but I don’t see them: my point was that we can cover all ‘reasonable’ use cases for other tools. (newcommand.py does u-type arguments, which we are not carrying forward to the kernel, and can set up to grab more than 9 arguments, but I can’t recommand that as an interface either)

Joseph Wright replying to samcarter — Thursday, 20th Aug 2020 20:53

I’ll fix

samcarter — Thursday, 20th Aug 2020 20:44

@Joseph There is a tiny typo in the second code block of https://www.texdev.net/2020/08/20/newdocumentcommand-versus-newcommand-versus \NewDocoumentCommand (actually this occurs 3 times) - If you want alternative spelling, I suggest Duckument 😃

user 3.14159 — Thursday, 20th Aug 2020 20:36

What I want to say that in the discussion of \newcommand variants you have certain applications and problems in mind which you are often dealing with. This perspective may suit many but perhaps not everyone. Even more, these structures will rather inevitably lead to situations you seem to wish to avoid: whenever users want to do something beyond standard, they will inevitably have to mix some low-level commands in, and then it may even be that the \NewDocumentCommand facilities are more harmful than helpful.

12 minutes
user 3.14159 replying to Joseph Wright — Thursday, 20th Aug 2020 20:25

I thought the upshot of your blog was something along the lines of “one size fits all”. That is, the \NewDocumentCommand is able to replace practically all cousins.

14 minutes
Joseph Wright replying to user 3.14159 — Thursday, 20th Aug 2020 20:10

I’m not sure what you mean: beamer's modified \newcommand is unchanged (with the attendant issues: a lot of Till’s code is a bit … problematic for stuff like robust commands)

user 3.14159 replying to Joseph Wright — Thursday, 20th Aug 2020 20:08

That would be good (but IMHO also shows what the problem is: as soon as one “tilts” the LaTeX kernel in a certain direction, the hell breaks loose).

2 hours
Joseph Wright — Thursday, 20th Aug 2020 18:36

I wonder if I should add 'versus beamer’ to the blog post

an hour
Joseph Wright — Thursday, 20th Aug 2020 17:14

@samcarter, re: your answer, Or you could test for -NoValue- and branch for the inclusion of \color, of course

Joseph Wright — Thursday, 20th Aug 2020 17:14

@samcarter, re: your answer, You might want to use D<>{} as at present you’ve not worried about -NoValue-

Joseph Wright — Thursday, 20th Aug 2020 17:13

I have wondered about some ‘add an overlay’ stuff for xparse, but at the moment I’ve felt it’s not such a great plan

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.