Tx · LaTeX & TikZ

How to Write Square Brackets in a pgfplots Axis Label

Published By the 9to5Science editorial team

At a glance

  1. 1. Group the label

    Put the complete xlabel or ylabel value inside braces within the axis options.

  2. 2. Keep brackets literal

    Write square brackets normally; you do not need to escape them in text or math mode.

  3. 3. Compile the plot

    Check that the label's braces close before the comma or closing bracket of the axis options.

Advertisement

Square brackets are ordinary characters in TeX text and math. The problem is usually not the brackets themselves, but how pgfplots reads the comma-separated options in an axis environment. A comma in an ungrouped label can be treated as an option separator, while a closing ] can prematurely end the axis option list.

Enclose the entire label value in braces. This keeps its contents together while pgfplots parses the surrounding options.

The basic syntax

Write the label like this:

\begin{axis}[
  xlabel={Time [s]},
  ylabel={$f(x) \in [0,1]$},
]

The outer square brackets begin and end the axis option list. The braces around each label value tell the key-value parser where that value starts and ends. Keep those braces even if the label has no punctuation; it is a reliable way to write axis labels.

Complete working example

This minimal document uses pgfplots and includes square brackets in both axis labels:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  domain=0:1,
  xlabel={Time [s]},
  ylabel={$f(x) \in [0,1]$},
]
  \addplot {x^2};
\end{axis}
\end{tikzpicture}

\end{document}

The package required is pgfplots; the compat setting selects a compatibility level. Compile the example with a LaTeX engine that supports TikZ and PGFPlots, such as pdfLaTeX.

In the example, [s] is part of the text label, and [0,1] is part of a math expression. Neither needs special escaping. The braces surrounding each value distinguish these brackets from the square brackets that delimit the axis options.

Why grouping fixes it

An axis option list has the form [...], with individual options separated by commas. If you write a value without braces, punctuation inside the value can be interpreted as a separator or as the end of the option list. This can lead to errors or to a label being parsed incorrectly.

For example, prefer:

xlabel={Position [m]},

over an ungrouped value such as:

xlabel=Position [m],

The same rule applies to ylabel, zlabel, and other pgfplots keys that take text or math as their value. It also applies if a label contains a comma, as in xlabel={Distance, $x$ [m]}.

Common mistakes

  • Trying to escape the brackets: In ordinary text and math, [ and ] are typed directly. Adding backslashes is unnecessary and may change what TeX does with the character.
  • Grouping only part of the label: Use xlabel={...}, not xlabel=Text {[s]}. Group the complete value so the parser sees it as one unit.
  • Removing the axis-list delimiters: The [ after \begin{axis} and its matching ] are still required. The braces group individual option values inside that list.
  • Forgetting to close braces: Each label’s opening { must have a matching } before the next option or the closing ].

If the label includes a macro or other TeX expression, the same principle holds: put the complete expression inside the key’s braces, for example ylabel={$\text{Pressure} [\mathrm{Pa}]$}. The \text command is provided by amstext, which is also loaded by amsmath; for ordinary text labels it is not needed.

References

  1. PGFplots Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.