我有一个单元格处理程序,它将 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}