() or {} after a command

I was wondering why for some commands it is "(...)" that follows, while for other commands it is "{...}". For example, "\exp(x)" and "\bar{x}".

Are both () and {} for specifying the arguments for the commands before them?

Are there other similar representations?

-
Is your example, the command \exp doesn't take any arguments. It just prints “exp” (in upright font). Then \exp(x) prints “exp(x)” and \exp x prints “exp x” (a better example would be \sin where both forms are commonly used in math). – Caramdir Feb 16 '11 at 0:42
@Caramdir IMO, your comment should be an answer. – Aaron Feb 16 '11 at 1:45

While Martin's answer has a lot of technical information, let me say something about your specific examples.

The command \bar does something to something else. Namely it puts a bar over it. So it needs to take an argument. In TeX, arguments are usually marked by curly braces. So \bar{x} produces and \bar{xx} produces . If no braces are given, TeX (more-or-less) takes the next non-whitespace symbol as argument. For example, \bar x again produces and \bar(x) produces .

On the other hand, \exp simply typesets and doesn't take any arguments. The (x) are afterwards typeset completely independently. As, such TeX doesn't see x as an argument of \exp; even though from a semantic viewpoint it is the argument of the exponential function. This is useful, because it allows you to write, e.g., \exp\colon\mathfrak{g} \to G for . Also consider the example of \sin where both and are common.

-

The braces { } are for normal arguments and the parentheses ( ) are only used for picture macros to specify coordinates and such. Another set are the brackets [ ] for optional arguments.

Normal LaTeX macros always await braces, which are the normal argument and grouping characters. You can't define new macros awaiting parentheses using \newcommand and the similar LaTeX macros.

There is, however, the possibility to define arbitrary patterns for macro with the lower-level plainTeX macro \def. This pattern is called a parameter text. If this text doesn't follow the macro an error is generated. The syntax for \def is:

\def<macro><parameter text>{<macro content>}


The parameter text must contain the used arguments #1, #2 and can also contain some other text like parentheses.

An example of an macro which awaits a ( ) would be:

\def\myvector(#1,#2){\dosomething{#1}{#2}}


The macro \myvector awaits a text (<something>,<something else>) direct afterwards, where <something> is stored in #1 and <something else> is stored in #2.

See also this question for an discussion about the differences of \def and \newcommand.

One important difference between { } and ( ) or [ ] is:
The { } can be nested, but the others can't. Because { } are handled by the lower-level system every { increases an internal counter which is then decreased when } is found. This way every { is matched with its }. However in \myvector above the first ) will close the parameter text independent of the number of (. The same is true for optional arguments which can't include [ ] for the same reason. One way to avoid this is to surround the inner text with { } which must be matched first before the rest of parameter text can be found. In other words the parameter text scanner is taking everything in braces as one element, so they effective hide their content from the scanner.

Example:

\somemacrowithoptionalargument[ \someother[opt]{...} ]{ ... }


will take \someother[opt as optional argument and the text token as mandatory one. The correct way to write it is:

\somemacrowithoptionalargument[ {\someother[opt]{...}} ]{ ... }

-
One tiny addition: When having a macro that accepts parameters with {...}, and you only want to give a single token as an argument (a single letter or a control sequence), you can leave the braces away. (Or otherwise around, when you forget the braces, only single characters are taken as argument. Remarkable for things like '\textbf. – Paŭlo Ebermann Feb 16 '11 at 11:32
One more addition: the xparse package takes care of nesting [...[...]...] properly, so commands defined using its very flexible interface will not have the problem Martin describes. – Bruno Le Floch Feb 17 '11 at 22:38
@Bruno: How does it do this exactly? Does it define the brackets temporarily to be catcode 1 & 2? Or does it parse the content for unmatched [`? – Martin Scharrer Feb 18 '11 at 14:42
changing catcodes would break horribly in most situations. So it goes the hard way and parses the content using only TeX's delimited arguments (and without counters... I still need to understand how). Joseph Wright knows more about this (he's the main author right now). – Bruno Le Floch Feb 19 '11 at 10:41
@Bruno: I thought so. Catcode changes should be avoided here. – Martin Scharrer Feb 19 '11 at 10:43