Latex typesetting is made by using special tags or commands that provide a handful of ways to format your document. Sometimes standard commands are not enough to fulfil some specific needs, in such cases new commands can be defined and this article explains how.
Contents |
Most of the LaTeX commands are simple words preceded by a special character.
In a document there are different types of \textbf{commands} that define the way the elements are displayed. This commands may insert special elements: $\alpha \beta \Gamma$
In the previous example there are different types of commands. For instance, \textbf
will make boldface the text passed as parameter to the command. In mathematical mode there are special commands to display Greek characters.
Commands are special words that determine LaTeX behaviour. Usually this words are preceded by a backslash and may take some parameters.
The command \begin{itemize}
defines an environment, see the article about environments for a better description. Below the environment declaration is the command \item
, this tells LaTeX that this is an item part of a list, and thus has to be formatted accordingly, in this case by adding a special mark (a small black dot called bullet) and indenting it.
Some commands need one or more parameters to work. The example at the introduction includes a command to which a parameter has to be passed, textbf
; this parameter is written inside braces and it's necessary for the command to do something.
There are also optional parameters that can be passed to a command to change its behaviour, this optional parameters have to be put inside brackets. In the example above, the command <item> \item[\S]</item> does the same as item, except that inside the brackets is \S
that changes the black dot before the line for a special character.
LaTeX is shipped with a huge amount of commands for a large number of tasks, nevertheless sometimes is necessary to define some special commands to simplify repetitive and/or complex formatting.
New commands are defined by \newcommand
statement, let's see an example of the simplest usage.
\newcommand{\R}{\mathbb{R}} The set of real numbers are usually represented by a blackboard bold capital r: \( \R \).
The statement \newcommand{\R}{\mathbb{R}}
has two parameters that define a new command
\R
\mathbb{R}
After the command definition you can see how the command is used in the text. Even tough in this example the new command is defined right before the paragraph where it's used, is a good practice put all your user-defined commands in the preamble of your document.
It is also possible to create new commands that accept some parameters.
\newcommand{\bb}[1]{\mathbb{#1}} Other numerical systems have similar notations. The complex numbers \( \bb{C} \), the rational numbers \( \bb{Q} \) and the integer numbers \( \bb{Z} \).
The line \newcommand{\bb}[1]{\mathbb{#1}}
defines a new command that takes one parameter.
\bb
[1]
\mathbb{#1}
User-defined commands are even more flexible than the examples shown above. You can define commands that take optional parameters:
\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1} To save some time when writing too many expressions with exponents is by defining a new command to make simpler: \[ \plusbinomial{x}{y} \] And even the exponent can be changed \[ \plusbinomial[4]{y}{y} \]
Let's analyse the syntax of the line \newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}
:
\plusbinomial
[3]
[2]
(#2 + #3)^#1
If you define a command that has the same name as an already existing LaTeX command you will see an error message in the compilation of your document and the command you defined will not work. If you really want to override an existing command this can be accomplished by renewcommand
:
\renewcommand{\S}{\mathbb{S}} The Riemann sphere (the complex numbers plus $\infty$) is sometimes represented by \( \S \)
In this example the command \S
(see the example in the commands section) is overwritten to print a blackboard bold S.
renewcommand
uses the same syntax as newcommand
.
For more information see: