Commands
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.
Introduction
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
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}
starts 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[\S]
does the same as item, except that inside the brackets is \S
that changes the black dot before the line for a special character.
Defining a new command
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.
Simple commands
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
- This is the name of the new command.
\mathbb{R}
- This is what the new command does. In this case the letter R will be written in blackboard boldface style. (to use
\mathbb
the package amssymb is needed)
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, good practice is to put all your user-defined commands in the preamble of your document.
Commands with parameters
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
- This is the name of the new command.
[1]
- The number of parameters the new command will take.
\mathbb{#1}
- This is what the command actually does. In this case the parameter, referenced as #1, will be written using blackboard boldface characters. If the defined new command needs more than one parameter, you can refer each parameter by #1, #2 and so on, up to 9 parameters are supported.
Commands with optional parameters
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
- This is the name of the new command.
[3]
- 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
- This 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 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
.
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
- 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
- Debugging Compilation timeout errors
- How-to guides
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
- 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
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 in LaTeX
- Bibliography management with biblatex
- Biblatex bibliography styles
- Biblatex citation styles
- Bibliography management with natbib
- Natbib bibliography styles
- Natbib citation styles
- Bibliography management with bibtex
- Bibtex bibliography styles
Languages
- Multilingual typesetting on Overleaf using polyglossia 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 and equations
- 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
- Typing 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
- Tips