2

I am trying to load a whole lot of CSV's using \pgfplotstableread and dynamically assign them a macro name from a \pgfplotsinvokeforeach loop. I tried \csname but suspect this is totally the wrong approach!

\newcommand{\PlotsAuto}[1]{
  \pgfplotsinvokeforeach{NoGender,Female,Male}{
    \pgfplotstableread{../Data/My#1data-##1.csv}{\csname \##1\endcsname}
  }
}

I cannot do it with \foreach as the loaded tables would be local to that environment. (I tried with \foreach \gender/\name in {NoGender/\NoGender.... with no success)

And then how can I access these in future loops which create the graphs?

5
  • remove the backslash from ##1 and use only one e.g. #1
    – percusse
    Feb 3, 2015 at 11:05
  • @percusse, with: \pgfplotstableread{../Data/Mydata-##1.csv}{\csname #1\endcsname} I get an error: 'too many }'s and Extra \endcsname \PlotsAuto
    – SpmP
    Feb 3, 2015 at 21:08
  • Also edited question to fully reflect the command, using #1 in the filename worked when manually doing \pgfplotstableread for every line, how can this work when using #1 from pgfplotsinvokeforeach?
    – SpmP
    Feb 3, 2015 at 21:15
  • ...also if (as it sometimes does) the filename contains a number straight after where the ##1 would be expanded. i.e. MySubData-NoGender2yr.csv, how can I handle this as it looks like ``My{}Data-##12yr.csv`
    – SpmP
    Feb 3, 2015 at 21:25
  • Put a space afterwards
    – percusse
    Feb 3, 2015 at 21:39

1 Answer 1

5

It appears to be an expansion issue as usual. But please don't do this number involving macro stuff. It is really unnecessary complication. Consider a batch file name treatment instead.

\documentclass{article}
\usepackage{pgfplotstable,filecontents}
\begin{filecontents*}{Myreddata-Female4yr.csv}
a b
1 2
3 4
\end{filecontents*}
\begin{filecontents*}{Myreddata-Male23.csv}
a b
5 6
7 8
\end{filecontents*}

\newcommand{\PlotsAuto}[1]{%
  \pgfplotsinvokeforeach{Female4yr,Male23}{%
    \begingroup\edef\temp{%
       \endgroup\noexpand\pgfplotstableread{My#1data-##1.csv}{\csname ##1\endcsname}}%
    \temp%
  }%
}
\begin{document}
\PlotsAuto{red}
\expandafter\pgfplotstabletypeset\csname Male23\endcsname
\expandafter\pgfplotstabletypeset\csname Female4yr\endcsname
\end{document}

enter image description here

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.