3

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.

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

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

% this works:
\tablereadA{}

% this gives  error Extra \endcsname
\tablereadB{}

\end{document}

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:

\pgfplotstableread{\pathONE}{\tabONE} 

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

2

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}
\newcommand\tablereadBaux[1]{\pgfplotstableread{data.txt}{#1}}

Here is the solution for the follow-up question:

\documentclass{article}
\usepackage{pgfplots}
\newcommand\tableread[1]%
  {\edef\tablereadX
     {\noexpand\pgfplotstableread
       {\csname path#1\endcsname}%
       \csname tab#1\endcsname
     }%
   \tablereadX
  }
\begin{document}
\newcommand\pathONE{data.txt}
\tableread{ONE} % expands to \pgfplotstableread {data.txt}\tabONE
\end{document}
  • 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.