[Tex/LaTex] Why does \mathbb{R} cause ‘! Undefined control sequence. – \GenericError’

errorspgfplots

When I compile the following document, I get

! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.39 ...an, y=variance, col sep=comma] {data.csv};
                                                  ^^M
? 

But when I change \mathbb{R} to mathbb{R} everything is fine. And the document also compiles when I put $\mathbb{R}$ over the tikz picture.

What is the problem and how do I fix it?

Code

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout

\usepackage{filecontents}  % only for this question

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{filecontents}{data.csv}
label,mean,variance
B,0.66,0.14
C,0.85,0.17
\pi,1.29,0.35
\mathbb{R},0.75,0.22
\end{filecontents}

\begin{tikzpicture}
    \begin{axis}[
            width=15cm, height=8cm,
            xlabel=mean,
            ylabel=variance,
            visualization depends on={value \thisrow{label} \as \label},
            every node near coord/.append style={font={\tiny}},
            nodes near coords={$\label$},
         ]
          \addplot[scatter,
                   mark=x,only marks,
                   mark size=1,
                   ]
                   table [x=mean, y=variance, col sep=comma] {data.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

Best Answer

pgfplots extensively uses \edef and it's not easy to find which one is responsible for this.

My suggestion is to redefine \mathbb so it uses \protected instead of the traditional LaTeX protection mechanism, which fails in \edef. The \protected method, instead, is safe.

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout

\usepackage{filecontents}  % only for this question

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}% why not 1.11?

% redefine \mathbb to be a \protected macro rather than a `robust' command
\protected\edef\mathbb{%
  \unexpanded\expandafter\expandafter\expandafter{%
    \csname mathbb \endcsname
  }%
}

\begin{document}

\begin{filecontents}{\jobname.csv}
label,mean,variance
B,0.66,0.14
C,0.85,0.17
\pi,1.29,0.35
\mathbb{R},0.75,0.22
\end{filecontents}

\begin{tikzpicture}
    \begin{axis}[
            width=15cm, height=8cm,
            xlabel=mean,
            ylabel=variance,
            visualization depends on={value \thisrow{label} \as \label},
            every node near coord/.append style={font={\tiny}},
            nodes near coords={$\label$},
         ]
          \addplot[scatter,
                   mark=x,only marks,
                   mark size=1,
                   ]
                   table [x=mean, y=variance, col sep=comma] {\jobname.csv};
    \end{axis}
\end{tikzpicture}
\end{document}

However, pgfplots should probably use \protected@edef instead of \edef.

enter image description here

If you have several commands that need this kind of workaround, it's better to use etoolbox and its \robustify function:

\usepackage{etoolbox}
\robustify{\mathbb}

will do the same as in the above code.