I'm using pgfplots in a report to make similar plots for many different data sets, for each of which the data is included in a separate .txt file. I use \pgfplotstableread to read data tables and assign to a macro name for later use in the plots.

I would like to be able to define a command that takes one text string argument, and uses this argument to refer to both the data file and to name the table. I can not seem to get anything to work for defining the stored name of the table dynamically inside the macro (e.g. using \csname).

At its simplest, perhaps I just need to understand why \tablereadB in the following MWE produces an error? Here I assume there is a data.txt file to be read.




\newcommand{\tablereadA}{ \pgfplotstableread{data.txt}{\tabA} }
\newcommand{\tablereadB}{ \pgfplotstableread{data.txt}{\csname tabB\endcsname} }

% this works:

% this gives  error Extra \endcsname


I am assuming this has something to do with the expansion order, but have not been able to get anything working using \expandafter.

As a follow up to gernot's answer, ultimately, for the string input "ONE", I would like the command to execute the equivalent of the following:


Here, \pathONE would be previously defined manually, among a long list of paths (one per data file). How can I achieve this?


The problem is that \pgfplotstableread stores the table under the given name. In your approach \csname is redefined and tabB\endcsname is left over, with \endcsname leading to the error message.

Try it this way:

\newcommand\tablereadB{\expandafter\tablereadBaux\csname tabB\endcsname}

Here is the solution for the follow-up question:

       {\csname path#1\endcsname}%
       \csname tab#1\endcsname
\tableread{ONE} % expands to \pgfplotstableread {data.txt}\tabONE
  • How would I adapt this to allow macro input of both the data name and the resulting table name? That is, for the string input "ONE", I would like to be able to generate the effective output: – ope Oct 11 '16 at 23:20
  • \pgfplotstableread{\pathONE}{\tabONE} – ope Oct 11 '16 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.