6

The most common notation for commands I have seen is the following:

\command[optional]{mandatory}

What I am trying to understand is how the optional arguments are understood. For example they can be passed as:

[opt1=val1, opt2=val2, opt3=val3, etc]

or as:

[opt1, opt2=val2, opt3]

I was thinking the first case as **kwargs in python. But, there are cases that also the second form is used and I can't understand how Latex understands the order (if matters at all). Also I was using beamer and I noticed that if I write:

\begin{frame}{Title}{Subtitle}

the command also works without {Subtitle} argument. If a mandatory argument isn't specified shouldn't that trigger an error?

5
  • 1
    The key-value options, see the package xkeyval. For the beamer 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 set title=A and subtitle=B.) Jan 24 at 2:10
  • 2
    LaTeX is a macro expansion language, think of \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 Jan 24 at 4:40
  • In general, for questions like this, you should look into the source code of the packages, or the compiled "source code documentation" version. For the basic, read TeXbook/TeX by topic and LaTeX unofficial reference manual/the LaTeX companion.
    – user202729
    Jan 24 at 6:53
  • 1
    @user202729 I think it's a reasonable question: it's I suspect more at the design-of-language than the implementation level
    – Joseph Wright
    Jan 24 at 10:22
  • @JosephWright "How does it understand" means "how does it work internally" right? Unless the question is "which syntax is allowed", then for this one the user-interface documentation works well
    – user202729
    Jan 24 at 13:37

1 Answer 1

9

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).

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.