> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/commands/01-commands.md).

# 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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+commands+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIn+a+document+there+are+different+types+of+%5Ctextbf%7Bcommands%7D+%0Athat+define+the+way+the+elements+are+displayed.+This+%0Acommands+may+insert+special+elements%3A+%24%5Calpha+%5Cbeta+%5CGamma%24%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Basic text formatting using LaTeX](/files/0wofl1NyK8PdBaXNkbic)

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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+itemized+list+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AA+list+example%3A%0A%5Cbegin%7Bitemize%7D%0A++%5Citem%5B%5CS%5D+First+item%0A++%5Citem+Second+item%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![A basic list typeset in LaTeX](/files/Sx4TdUXnelwAdnbx4gCJ)

The command `\begin{itemize}` starts an `itemize` *environment*—see the [article about environments](/latex/commands/02-environments.md) 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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Creating+a+simple+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5CR%7D%7B%5Cmathbb%7BR%7D%7D%0AThe+set+of+real+numbers+are+usually+represented+%0Aby+a+blackboard+bold+capital+R%3A+%5C%28+%5CR+%5C%29.%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from a new command](/files/EexeyNuOgKqqomyR0aNN)

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 the `amssymb` 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](/latex/document-structure/07-management-in-a-large-project.md#inputting-and-including-files).

### Commands with parameters

It is also possible to create new commands that accept some parameters; for example:

```latex
\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}
```

[Open this example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+command+with+1+parameter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5Cbb%7D%5B1%5D%7B%5Cmathbb%7B%231%7D%7D%0AOther+numerical+systems+have+similar+notations.+%0AThe+complex+numbers+%5C%28+%5Cbb%7BC%7D+%5C%29%2C+the+rational+%0Anumbers+%5C%28+%5Cbb%7BQ%7D+%5C%29+and+the+integer+numbers+%5C%28+%5Cbb%7BZ%7D+%5C%29.%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from a LaTeX command with a parameter](/files/LCCBhcxoJag8tPz1jzW7)

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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+command+with+optional+parameters\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%0AWhen+writing+many+expressions+with+exponents+we+can+simplify+our+task%2C+and+save+time%2C+by+defining+a+suitable+command%3A%0A%0A%5Cnewcommand%7B%5Cplusbinomial%7D%5B3%5D%5B2%5D%7B%28%232+%2B+%233%29%5E%231%7D%0A%0AWe+can+use+it+like+this%3A+%5C%5B+%5Cplusbinomial%7Bx%7D%7By%7D+%5C%5D%0A%0AAnd+even+the+exponent+can+be+changed%3A%0A%0A%5C%5B+%5Cplusbinomial%5B4%5D%7Ba%7D%7Bb%7D+%5C%5D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from a command with optional parameters](/files/J7y1ZRt1JG1x0y5aeNeP)

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:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Duplicate+command+name\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cnewcommand%7B%5Ctextbf%7D%5B1%5D%7B%231%7D%25+This+will+not+work%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%7BThis+will+fail%7D%0A%5Cend%7Bdocument%7D)

Compiling this example produces the following output:

![Error generated by duplicate LaTeX command definition](/files/zcPJc1xjOjodETMAHJNc)

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`:

```latex
\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.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Redefine+existing+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Crenewcommand%7B%5CS%7D%7B%5Cmathbb%7BS%7D%7D%0A%0AThe+Riemann+sphere+%28the+complex+numbers+plus+%24%5Cinfty%24%29+is+%0Asometimes+represented+by+%5C%28+%5CS+%5C%29.%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from a redefined LaTeX command](/files/MGEdxII0jAgvFb1HjDWY)

In this example the command `\S` (see the example in the [commands](#commands) section) is overwritten to print a blackboard bold S.

## Further reading

For more information see:

* [Environments](/latex/commands/02-environments.md)
* [Understanding packages and class files](/latex/class-files/01-understanding-packages-and-class-files.md)
* [Writing your own package](/latex/class-files/03-writing-your-own-package.md)
* [Writing your own class](/latex/class-files/04-writing-your-own-class.md)
* [Lengths in LaTeX](/latex/formatting/01-lengths-in-latex.md)
* [Using colours in LaTeX](/latex/formatting/13-using-colors-in-latex.md)
* [Page size and margins](/latex/formatting/07-page-size-and-margins.md)
* [List of packages and class files](/latex/class-files/02-overleaf-and-tex-live.md)
* [The not so short introduction to LaTeX2ε](http://www.ctan.org/tex-archive/info/lshort/)
* [LaTeX/Creating\_Packages on WikiBooks](http://en.wikibooks.org/wiki/LaTeX/Creating_Packages)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/commands/01-commands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
