6

With newcommand I can define customized command like \ABC. Is there any way to define a command with arguments. For example I want \brat{A}{B} be the same as [\![A,B]\!].

user69453
  • 235

2 Answers2

14

The general form of \newcommand is

\newcommand\commandname[number of arguments][value of optional argument]{code}

(Most of the time there won't be an optional argument, in which case this is omitted.) The arguments are given as #1, #2 etc in code.

To define your \brat command you would write

\newcommand\brat[2]{[\![#1,#2]\!]}

You then use this macro by writing \brat{A}{B}, \brat{A}{C} and so on. If almost all of the time you wanted the first argument to be A then you could instead use an optional first argument and define

\newcommand\Brat[2][A]{[\![#1,#2]\!]}

You use this version of the macro n almost exactly the same way except that you do not need to specify A: so \Brat{B} produces the same as \brat{A}{B} before. To change the value of the optional argument from A to C, say, you would write \Brat[C]{B}. This is the same as \brat{C}{B} using the first macro.

1

In TeX, we can define macros with arguments by

 \def\macro#1#2#3#4{body with #1 argument, #2 argument, #3 argument, #4 argument}

and usage is:

 \macro{first}{second}{third}{four}

or

\macro fstf

which is equivalent to \macro{f}{s}{t}{f}.

There is another method to declaring "delimited" parameters (which is not supported by LaTeX \newcommand), see (for example) TeX in a Nutshell, pages 9 and 10.

wipet
  • 74,238