18

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}
2
  • 1
    It's an expansion issue; see also tex.stackexchange.com/questions/195080/… . I don't know how to fix it in your case, though.
    – JPi
    Aug 16, 2014 at 18:02
  • 2
    @percusse Or, maybe better, \noexpand\protect\noexpand (which works).
    – egreg
    Aug 16, 2014 at 22:39

1 Answer 1

17

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.

3
  • Is \sun from wasysym the same problem? At least the error is another one: ! Missing control sequence inserted. <inserted text> \inaccessible l.49 \end{axis} Aug 17, 2014 at 16:16
  • 2
    @moose That's why I said pgfplots should use \protected@edef and not \edef. Look at my edit for an abstraction of the workaround.
    – egreg
    Aug 17, 2014 at 16:59
  • 1
    Thanks. That made some more work. But \lightning from stmaryrd combined with other symbols from wasysym still has problems. That might be related to wasysym symbols render to something different Aug 17, 2014 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .