Commands
Introduction
This article provides a first introduction to commands, which are a vital component of LaTeX's typesetting capabilities. Most LaTeX commands are simple words preceded by a special character, usually \
, the backslash character. Let's look at some examples:
\documentclass{article}
\begin{document}
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$
\end{document}
Open this example in Overleaf.
This example produces the following output:
In the previous example, there are different types of commands; for instance, \textbf
will embolden the text passed as parameter to the command. In mathematical mode, there are special commands, such as \alpha
, \beta
and \Gamma
to display Greek characters.
Commands
Here is an example of LaTeX code with commands to create a bulleted list:
\documentclass{article}
\begin{document}
A list example:
\begin{itemize}
\item[\S] First item
\item Second item
\end{itemize}
\end{document}
Open this example in Overleaf.
This example produces the following output:
The command \begin{itemize}
starts an itemize
environment—see the article about environments for more detail. Below the environment declaration is the command \item
which tells LaTeX to start a new list entry: to format it, add a special mark (a small black dot called bullet) and indent the entry.
Some commands require one or more parameters; for example, the \textbf
command (used above) which takes a single parameter—the text to show in bold type, which is written inside braces like this: \textbf{make this bold}
.
There are also optional parameters which can be passed to a command to change its behaviour; those optional parameters have to be put inside brackets: [...]
. In the example above, the command \item[\S]
does the same as \item
, except that inside the brackets is \S
, which substitutes another character to replace the black dot before the text of the entry.
Defining a new command
Although LaTeX is shipped with a huge number of commands it often becomes necessary to define your own special commands to simplify your work, reduce repetitive tasks or perform some complex formatting.
Simple commands
New commands are defined by \newcommand
; so let's see a simple example:
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\R}{\mathbb{R}}
The set of real numbers are usually represented
by a blackboard bold capital R: \( \R \).
\end{document}
Open this example in Overleaf.
This example produces the following output:
The statement \newcommand{\R}{\mathbb{R}}
has two parameters that define a new command, where:
\R
: is the name of the new command.\mathbb{R}
: is what the new command does. In this case, the letter R will be written in blackboard boldface style. Use of the\mathbb
command requires theamssymb
package, which was added to the example above (in the preamble).
After the command is defined we can use it in the text, as demonstrated above. In this example, the new command is defined immediately before the paragraph in which it is used; however, scattering such definitions throughout your document is not considered best practice. Generally, definitions for new commands are collected together in the document preamble or, for larger collections, placed into a separate file that can be imported into your main document.
Commands with parameters
It is also possible to create new commands that accept some parameters; for example:
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\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} \).
\end{document}
This example produces the following output:
The line \newcommand{\bb}[1]{\mathbb{#1}}
defines a new command that takes one parameter, where:
\bb
is the name of the new command.[1]
is the number of parameters the new command will take.\mathbb{#1}
is what the command actually does—its definition.
In this case, the parameter, referenced as #1
, will be written using blackboard boldface characters. If a command needs more than one parameter, you can refer to each parameter using #1
, #2
and so on. Up to 9 parameters are supported.
Commands with optional parameters
User-defined commands can be made even more flexible than the examples shown so far: you can define commands that take optional parameters:
\documentclass{article}
\begin{document}
When writing many expressions with exponents we can simplify our task, and save time, by defining a suitable command:
\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}
We can use it like this: \[ \plusbinomial{x}{y} \]
And even the exponent can be changed:
\[ \plusbinomial[4]{a}{b} \]
\end{document}
Open this example in Overleaf.
This example produces the following output:
Let's analyse the syntax of the line \newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}
:
\plusbinomial
is the name of the new command.[3]
is the number of parameters the command will take, in this case 3.[2]
is the default value for the first parameter. This is what makes the first parameter optional, if not passed it will use this default value.(#2 + #3)^#1
is what the command does. In this case it will put the second and third parameters in a "binomial format" to the power represented by the first parameter.
Overwriting existing commands
If you try to define a new command which has the same name as an already existing LaTeX command then the compilation will fail with an error message. This is demonstrated in the following example which tries to define the (existing) \textbf
command:
\documentclass{article}
\newcommand{\textbf}[1]{#1}% This will not work
\begin{document}
\section{This will fail}
\end{document}
Open this (error-generating) example in Overleaf.
Compiling this example produces the following output:
As shown in the screen image, our attempt to define \textbf
has failed: LaTeX generated the error message LaTeX Error: Command \textbf already defined.
If you really want to override an existing command this can be accomplished by \renewcommand
, which uses the same syntax as \newcommand
:
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\renewcommand{\S}{\mathbb{S}}
The Riemann sphere (the complex numbers plus $\infty$) is
sometimes represented by \( \S \).
\end{document}
Open this example in Overleaf.
This example produces the following output:
In this example the command \S
(see the example in the commands section) is overwritten to print a blackboard bold S.
Further reading
For more information see:
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Using the Overleaf project menu
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Using the History feature
- Debugging Compilation timeout errors
- How-to guides
- Guide to Overleaf’s premium features
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Matrices
- Fractions and Binomials
- Aligning equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
- Using the Symbol Palette in Overleaf
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management with bibtex
- Bibliography management with natbib
- Bibliography management with biblatex
- Bibtex bibliography styles
- Natbib bibliography styles
- Natbib citation styles
- Biblatex bibliography styles
- Biblatex citation styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- Multilingual typesetting on Overleaf using babel and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections, equations and floats
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typesetting exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class