3

Sometimes I see [] before {}, such as:

\documentclass[letter,12pt]{article}

Sometimes I see {} before [], such as:

\newcommand{name}[num]{definition}

Which should go first, [] or {} ? Does it matter?

Roxy
  • 809
  • 8
    It depends very much on the command and yes, order does matter. – Henri Menke Nov 17 '18 at 23:46
  • 6
    Welcome to TeX.SE. The normal style is the have the optional parameters (using the []) first followed by all the mandatory parameters (using the {}). But this can be changed (see for example the xparse pacakge). So, for your own macros stick to defining the optional paramters first followed by the mandatory ones, unless there is a very good reason to do otherwise. For exisitng macros, you would need to consult the documentation as to the required order. – Peter Grill Nov 18 '18 at 00:19
  • In case you were wondering, this is typically implemented using \@ifnextchar[{\@withoptionalarg}{\@without} but with xparse you won't have to do that. – John Kormylo Nov 18 '18 at 00:36
  • 2
    @PeterGrill It is ironic, is it not, that \newcommand doesn't follow that advice? – cfr Nov 18 '18 at 00:52
  • @JohnKormylo I'm sure that will be illuminating to somebody trying to grasp LaTeX's syntax. – cfr Nov 18 '18 at 00:54
  • @JohnKormylo Of course. But there's no reason that \newcommand couldn't be defined so that users of \newcommand used it with standard LaTeX syntax, rather than having to put the optional arguments in the middle of the mandatory ones. – cfr Nov 18 '18 at 22:15
  • @HenriMenke Oh wow. Very annoying that you can't assume standard syntax. – Roxy Nov 18 '18 at 23:11

1 Answers1

3

The number and position of arguments is part of the command definition, so the order definitely matters.

\documentclass for example has two optional arguments, one before, and one after, the mandatory argument.

\documentclass[letter,12pt]{article}[2020/01/01]

for example would specify that you need a version of the class file no older than next year (this will generate an error that the version is not available, just so you see the effect).

so if you use

 \documentclass{article}[letter,12pt]

then you get an error not because the optional argument is last, but because letter,12pt is not a valid date.

 \documentclass{article}[2000/01/01]

would work without error.

The basic \newcommand definition form only allows the definition of commands with a single optional argument, which appears first, however that restriction was just to keep the syntax of \newcommand simple, very few standard latex commands that have optional arguments are defined via \newcommand.

For a documented user interface that allows the specification of commands with multiple optional arguments in arbitrary position with respect to the mandatory arguments, see the xparse package.

David Carlisle
  • 757,742