1

I am quite new to LaTeX and as I was reading the LaTeX Wikibook I came upon the following general definition for a command in LaTeX here:

\commandname[option1,option2,...]{argument1}{argument2}...

However, later on in the Wikibook the syntax for creating a new command is given as:

\newcommand{name}[num][default]{definition}

Now given the general definition for a command in LaTeX I would have instead expected the following syntax for creating a new command:

 \newcommand[num,default]{name}{definition}

A bit later on in the same section on creating macros in the LaTeX wikibook the syntax for creating a new environment is given as:

\newenvironment{name}[num][default]{before}{after}

Again, given the general definition I would have expected this instead:

 \newenvironment[num,default]{name}{before}{after}

I would appreciate it greatly if somebody could clarify this inconsistency for me.

  • 2
    Welcome to TeX.SX! The nice thing about TeX is that it has almost no syntax of itself. The syntax is defined by you (or in this case, by LaTeX). Commands defined with \newcommand will invariably have the syntax \cmd{<arg1>}{<arg2>}... or \cmd[<opt>]{<arg1>}{<arg2>}.... Commands defined with xparse have much more flexibility, and you can also define the commands by hand to have virtually any syntax you want. – Phelype Oleinik Oct 19 '19 at 14:59
  • 7
    Welcome to TeX.SX! I don't find the LaTeX Wikibook a very reliable guide. Some commands accept comma separated options, but the vast majority doesn't. And a command can have multiple optional arguments, one case is indeed [math]\newcommand[/math]. – egreg Oct 19 '19 at 15:00
  • 3
    The only somewhat consistent convention is to use [] for optional arguments and {} for mandatory ones. Third type is () for coordinates, but those are pretty rare in the LaTeX format itself (picture-mode). There are some classes and packages however that break this general rule and add optional braced arguments, so not even those are safe. And the concept of key=value optional arguments is relatively young, so the older the macro the less likely they are used. – Skillmon Oct 19 '19 at 15:05
  • Also, while it is possible in theory to use optional arguments in fully expandable macros, a key=value syntax will most likely never be fully expandable, so there might be macros which take normal and possibly multiple optional arguments in the distant future, when key=value has otherwise fully took over. – Skillmon Oct 19 '19 at 15:10
  • Could you explain what key=value means? – The Riddler Oct 19 '19 at 15:11
  • It should be noted that \commandname is not a standard macro, but rather an example of how to use a macro, while \newcommand is a LaTeX macro used to create new commands (replacing the original TeX \def\name#1#2...{definition}). – John Kormylo Oct 19 '19 at 15:14
  • @TheRiddler suppose I want to make a command to include images in a document. I probably want to scale them several different optional ways, e.g. specify a scale factor, or the required width or height, or rotate the image through an angle, or whatever. The easiest way to do that in TeX or LaTeX is make a command with one (optional) argument that might look like width=2in,rotate=90 etc rather than having several arguments, or several different commands for different options. – alephzero Oct 19 '19 at 15:38
  • Another unusual syntax are in the lipsum commads (e.g. \lipsum[1][1-3]) where are not allowed any arguments. And you an also face with constructs like \cmidrule{1-3}(rl) with the booktabs package, where the part in () is optional too. To clarify the question (or may be help to the confusion) think about this code: \def\bold*#1*{{\bfseries #1}} \def\italics<#1>{\textit{#1}} this allow you to use \bold*foo* or \italics<baz>. It is hard maintain a consistent syntax in hundreds of packages when you can make almost anything. – Fran Oct 19 '19 at 19:38

0 Answers0