LaTeX is written in TeX, and TeX has no built-in concept of an optional argument. What is does have is the ability to 'look ahead' for a token, and a way to grab delimited arguments. So the way LaTeX finds an optional argument is to look ahead for a [
, and if it finds one to grab everything up to the next ]
. This uses two internal macros, one of which takes no 'optional' argument at all, and one which takes a delimited argument and so requires [...]
.
The basic approach can get confused by nested optional arguments: code in ltcmd
allows for that and ensures that for example
\foo[\cmd[bar]]
correctly grabs \cmd[bar]
as the optional argument.
It is then down to the 'consumer' code what to do with the optional argument. For example, if processed as a key-value argument, one could look at the keys in the order given by the user or the order the keys were defined by the programmer. What is important to understand is that the entire content between the [...]
tokens is a single argument.
The case of an optional argument inside the usually-mandatory {...}
pair is special. It can't use exactly the same mechanism as normal, as TeX doesn't allow that. Instead, we look ahead for a {
then branch between two internal commands, one which takes one more mandatory argument than the other.
Grabbing an optional argument in {...}
is outside the normal LaTeX argument pattern. As such, it's not encouraged by the LaTeX team. For example, recent LaTeX releases provide ltcmd
which can grab a range of 'basic' argument types easily. It however doesn't support an optional {...}
, which is provided only as a legacy add on for that code (as the team did explore whether to provide it out-of-the-box).
xkeyval
. For thebeamer
example, it is briefly described in the documentation in the beginning of section 8.1. Basically, they are not arguments to\begin{frame}
; instead, inside the frame, if the first thing it sees is an open brace, it is assumed what follows is meant to be the title; and after that if the first thing it sees is an open brace, it is assumed what follows is meant to be the subtitle. (If they were actual arguments then\begin{frame}AB
would settitle=A
andsubtitle=B
.)\cmd
is a token, and individual letters as tokens.{stuff}
means treat stuff as one. Basically commands do things tokens. There are no built in 'functions' or kwargs per se, just a commands that process a series of tokens differently