pgfplotstable:在‘preproc 单元格内容’中添加‘mathsf’不起作用

pgfplotstable:在‘preproc 单元格内容’中添加‘mathsf’不起作用

我有一个单元格处理程序,它将 X 列的值以上标的形式写入 A 列,其工作原理如下:

preproc cell content/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\mytable%
\xdef\X{\ensuremath{^{\pgfplotsretval}}}%
\edef\temp{%
\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%% 
  ##1\X
}%%

如果我添加\mathsf在这里,这意味着^\mathsf{\pgfplotsretval}方法失败。
我该怎么做才能添加\mathsf

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{colortbl}
\usepackage{amsmath}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep = semicolon, header=true]{
A;    X
a;     x
b;     x
c;     x
d;     x
e;     x
}{\mytable}


\begin{document}

\section{Without mathsf -- works}
\pgfplotstabletypeset[string type, font=\sffamily, 
columns/A/.style={%%
preproc cell content/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\mytable%
\xdef\X{\ensuremath{^{\pgfplotsretval}}}%
\edef\temp{%
\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%% 
  ##1\X
}%%
}\temp},
},%%
]{\mytable}


\section{With mathsf -- works not}
\pgfplotstabletypeset[string type, font=\sffamily, 
columns/A/.style={%%
%preproc cell content/.code={
%\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\mytable%
%\xdef\X{\ensuremath{^\mathsf{\pgfplotsretval}}}% PROBLEM HERE!
%\edef\temp{%
%\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%% 
%  ##1\X
%}%%
%}\temp},
},%%
]{\mytable}
\end{document}

答案1

正如 David Carlisle 在评论中提到的,通常不能将\edef\xdef与 LaTeX 宏一起使用,因为大多数命令不受 e-TeX 保护(与几乎所有内容都受保护的 ConTeXt 不同)。相反,LaTeX 具有“强健”命令的概念,但\edef必须\xdef明确告知 和 来处理它们,这是使用\protected@edef和完成的\protected@xdef

\documentclass[a4paper]{article}
\usepackage{colortbl}
\usepackage{amsmath}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread[col sep = semicolon, header=true]{
A;    X
a;     x
b;     x
c;     x
d;     x
e;     x
}{\mytable}


\begin{document}

\section{Without mathsf -- works}
\makeatletter
\pgfplotstabletypeset[string type, font=\sffamily, 
columns/A/.style={%%
preproc cell content/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\mytable%
\protected@xdef\X{\ensuremath{^{\pgfplotsretval}}}%
\protected@edef\temp{%
\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%% 
  ##1\X
}%%
}\temp},
},%%
]{\mytable}
\makeatother

\section{With mathsf -- works not}
\makeatletter
\pgfplotstabletypeset[string type, font=\sffamily, 
columns/A/.style={%%
preproc cell content/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\mytable%
\protected@xdef\X{\ensuremath{^\mathsf{\pgfplotsretval}}}% PROBLEM HERE!
\protected@edef\temp{%
\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%% 
  ##1\X
}%%
}\temp},
},%%
]{\mytable}
\makeatother

\end{document}

相关内容