- \newcommand{cmd}[req_args][opt_args]{def}
- \renewcommand{cmd}[req_args][opt_args]{def}
- \providecommand{cmd}[req_args][opt_args]{def}
The three above methods contain the same syntax, where "cmd" stands for the name of the command, it should usually start with a backslash; "req_args" represents the number of arguments the new command carries, if unspecified, then the command takes no argument; "opt_args" denotes the default values of some arguments, if they are not presented when applying the command; finally, "def" is the detailed definition/description of the command.
The three commands are very similar in function, but they have to be used in different conditions. "\newcommand" helps to update the meaning of an already existing command, while "\newcommand" and "\providecommand" can only be applied to construct *new* commands, that's to say, the ones that do not exist before. If applied to an existing command, "\newcommand" will yield a compiling mistake, while "\providecommand" simply does not do anything.
If you are creating commands with arguments, then you have to specify the number of arguments in the "req_args" part, and refer each argument with anchors such as "#1", "#2", "#3", etc., the number corresponds to the linear order of arguments.
The "opt_args" is optional, just in case when the argument is not explicitly mentioned, some default value will take its place.
Some examples of new commands:
- \newcommand{\abc}{hello world!}
- \newcommand{\def}[2][default]{\textit{#1}+\textbf{#2}}
- \newcommand{\def}[3][default]{\textit{#1}+\textbf{#2}-\texttt{#3}}
- \renewcommand{\abc}{Bonjour Tout le Monde!}
Reference:
没有评论:
发表评论