4

In my LaTeX document I will need often a specific value in a table. So I can define the following macro :

\newcommand{\values}{\pgfplotstablegetelem{0}{0}\of\mydata\pgfplotsretval}

The pgfplotstable manual recommands not to use this command in a loop :

Attention: If possible, avoid using this command inside of loops. It is quite slow.

It would be wise to compute only once the value of \value and then use it as often as I want without slowing down the compilation. What is the best way to do this ?

Here is a sample code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\pgfplotstableread[col sep=comma]{
1,15,53
5,74,12
}\mydata
\renewcommand{\value}{\pgfplotstablegetelem{0}{0}\of\mydata\pgfplotsretval}
\begin{document}
This is the first values : \values
\end{document} 

1 Answers1

5

When you do

\pgfplotstablegetelem{0}{0}\of\mydata

the macro \pgfplotsretval expands just to the computed value. Thus you just need to define your macro to be the current expansion of \pgfplotsretval:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\pgfplotstableread[col sep=comma]{
1,15,53
5,74,12
}\mydata
\pgfplotstablegetelem{0}{0}\of\mydata

\makeatletter
\@ifdefinable{\myvalue}{\let\myvalue\pgfplotsretval}
\makeatother

\begin{document}
This is the first value: \myvalue
\end{document}

Important warning

Never use \renewcommand if you don't know precisely what you're doing. The macro \value is a very important function of the LaTeX kernel. Redefining it will break a huge number of internal functions.

egreg
  • 1,121,712