为什么 \mathbb{R} 会导致 '! Undefined control serial. - \GenericError'?

为什么 \mathbb{R} 会导致 '! Undefined control serial. - \GenericError'?

当我编译以下文档时,我得到

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

但是当我更改\mathbb{R}为时,mathbb{R}一切都正常。当我将 $\mathbb{R}$ 放在 tikz 图片上时,文档也会编译。

这是什么问题?如何解决?

代码

\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}

答案1

pgfplots被广泛使用\edef,而且很难找出哪一个对此负责。

我的建议是重新定义\mathbb它,\protected而不是使用传统的 LaTeX 保护机制,因为后者在 中失败了\edef\protected相反,这种方法是安全的。

\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}

不过,pgfplots可能应该使用\protected@edef而不是\edef

在此处输入图片描述

如果您有几个命令需要这种解决方法,最好使用etoolbox及其\robustify功能:

\usepackage{etoolbox}
\robustify{\mathbb}

将执行与上述代码相同的操作。

相关内容