PGFPlots – How to Write Square Brackets in Xlabel or Ylabel

bracketspgfplots

The following works fine:

\begin{tikzpicture}
  \begin{axis}[xlabel=$k$, ylabel=$x(k)$]
    \addplot coordinates {(0,1) (1,4) (2,2) (3,-1) (4,2)};
  \end{axis}
\end{tikzpicture}

But as soon as I replace $x(k)$ with $x[k]$, I get an error.

Is it possible to use square brackets in xlabel or ylabel?

Best Answer

Ensure you put the labels within braces:

xlabel={$k$}, ylabel={$x[k]$}

From the PGFPlots manual, on page 200, you have the following

/pgfplots/xlabel={<text>}
/pgfplots/ylabel={<text>}
/pgfplots/zlabel={<text>}

Here is MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xlabel={$k$}, ylabel={$x[k]$}]
    \addplot coordinates {(0,1) (1,4) (2,2) (3,-1) (4,2)};
  \end{axis}
\end{tikzpicture}
\end{document}