Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 at 18:02
    
\noexpand\mathbb{R} seems to do the trick in this case. –  Mark Wibrow Aug 16 at 18:36
    
@MarkWibrow Adding \noexpand before \mathbb{R} in the CSV file does not work for me. I still get the same error. –  moose Aug 16 at 19:04
1  
Three \noexpands might work. Also related tex.stackexchange.com/questions/52424/… –  percusse Aug 16 at 22:01
1  
@percusse Or, maybe better, \noexpand\protect\noexpand (which works). –  egreg Aug 16 at 22:39

1 Answer 1

up vote 12 down vote accepted

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.

share|improve this answer
    
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} –  moose Aug 17 at 16:16
    
Or the follwing ones (with the necessary packages): '\celsius', '\degree', '\ohm', '\venus', '\mars', '\astrosun', '\fullmoon', '\leftmoon', '\female', '\male', '\checked', '\diameter', '\sun', '\Bowtie', '\mathscr{[letter]}', '\mathfrak{[letter]}', '\sqrt{}', 'cong', '\copyright', '\dag', '\parr', '\notin', '\dotsc', '\mathds{[letter]}' –  moose Aug 17 at 16:19
1  
@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 at 16:59
    
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 –  moose Aug 17 at 19:08
    
@moose I get no error from \lightning –  egreg Aug 17 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.