A very convenient tool in LaTeX is the ability to create macros or user defined commands. This turns out to be very productive and can save a lot of time, especially when you are using certain expressions frequently. This can also save a lot of headache if halfway through your document, you decide that you want to change notation. Also, when you have very long expressions that you are repeatedly using, it makes no sense to type it over and over again.
The main commands used for this purpose are and .
The user defined commands must be defined in the preamble, i.e., the place after the but before .
As an example, say that you are using an expression like to denote a quantity. You can define a command like
Whenever you want to use the expression, simply type out . Note that you can use this only in the math mode and LaTeX will give you an error if you use it in the normal mode. However, this can be solved using the command. Use
and you can use \mk in both cases.
Note that this command can be used for text also. For example, say you want to replace “Direct sum decomposition” by a compact representation. Use
and you’re done. This command turns out to be really useful when you want to specify arguments. As an example, say that you want to define a function . You can define a user defined command of the form
But in some other place, you need . You don’t have to define a new command. Instead, you can specify arguments in the following manner:
Here, , represent the two arguments. When you want to use the same in an equation, simply use to get . You can specify upto 9 arguments numbered to .
The command behaves similarly. But a word of caution. If you use a command name that corresponds to a default LaTeX command, will give an error whereas will simply overwrite the default definition. The syntax (for the first example) is
Note that there are no braces right after , unlike . To specify arguments,
When you want to invoke this in your document, use .
G. Gratzer, Math into LaTeX, 4th ed, Springer