0

I have cell processing which writes the value of the X column to the A column in superscript, that works as far:

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

If I add \mathsf here, that means ^\mathsf{\pgfplotsretval} the method breaks down.
What do I have to do for adding \mathsf?

enter image description here

\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
  • 1
    as has been said before if you use \xdef or \edef you will break most latex commands. You can not use \ensuremath here: \xdef\X{\ensuremath Jun 7, 2021 at 7:41

1 Answer 1

1

As also mentioned by David Carlisle in the comments, you generally cannot use \edef and \xdef with LaTeX macros, as most commands are not e-TeX protected (in contrast to ConTeXt where almost everything is protected). Instead LaTeX has the concept of “robust” commands, but \edef and \xdef have to explicitly be told to take care of them which is done using \protected@edef and \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}

You must log in to answer this question.

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