[Tex/LaTex] Defining macros with arguments


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]\!].

Best Answer

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


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


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.

Related Question