Usando \pgfplotstablesort com uma macro nomeada dinamicamente

Usando \pgfplotstablesort com uma macro nomeada dinamicamente

Eu tenho um número moderadamente grande de arquivos de dados que preciso ler \pgfplotstablesorte, em cada caso, os dados lidos devem ser atribuídos a uma macro para uso posterior em um gráfico. Os nomes dos arquivos seguem um padrão previsível, então eu gostaria de fazer uma macro que leia os dados e os atribua a uma macro cujo nome esteja relacionado ao nome do arquivo. Até agora tenho trabalhado com isso:

\def\readdata#1#2#3#4{
 \def\filenamepattern##1{#1}
 \pgfplotstablesort{\csname #4himu\endcsname}{\filenamepattern{#3}}
 \pgfplotstablesort{\csname #4lomu\endcsname}{\filenamepattern{#2}}
 % other stuff
}
\readdata{testfile#1.dat}{10}{50}{testdata}

Isso não compila, é claro, porque \pgfplotstablesortnão consegue lidar \csnamecom o primeiro argumento. PDFLaTeX apresenta muitos erros, começando com Missing \begin{document}e nunca termina. Eu sei que essa é a raiz do problema porque se eu inserir o nome da macro manualmente,

\def\readdata#1#2#3#4{
 \def\filenamepattern##1{#1}
 \pgfplotstablesort{\testdatahimu}{\filenamepattern{#3}}
 \pgfplotstablesort{\testdatalomu}{\filenamepattern{#2}}
 % other stuff
}

Funciona bem.

Então agora eu tento um \expandafter, pensando que isso fará com que \csname...\endcsnameseja expandido antes \pgfplotstablesort:

\def\readdata#1#2#3#4{
 \def\filenamepattern##1{#1}
 \expandafter\pgfplotstablesort{\csname #4himu\endcsname}{\filenamepattern{#3}}
 \expandafter\pgfplotstablesort{\csname #4lomu\endcsname}{\filenamepattern{#2}}
 % other stuff
}

mas isso não faz nenhuma mudança. Acho que isso tem algo a ver com a definição de \pgfplotstablesort, talvez como pode ser um argumento opcional, mas me perco tentando descobrir.

Alguém pode me mostrar se há alguma maneira de fazer o que estou tentando fazer funcionar e, de preferência, explicar por que o \expandafterque tentei não fez acontecer? Gostaria de aproveitar isso como uma oportunidade para entender melhor a expansão da macro, além de poder carregar meus arquivos de dados para plotagem.


MWE completo:

\documentclass{article}
\usepackage{filecontents,pgfplots,pgfplotstable}
\begin{filecontents*}{testfile10.dat}
pT Y mu2 lomean lostddev loerrbound nlomean nlostddev nloerrbound
0.35 2.2 10 2.8920929e+00 0 1.7555822e-04 1.1823986e+00 1.6578794e-01 6.7619291e-02
\end{filecontents*}
\begin{filecontents*}{testfile50.dat}
pT Y mu2 lomean lostddev loerrbound nlomean nlostddev nloerrbound
0.35 2.2 10 2.8920929e+00 0 1.7555822e-04 1.1823986e+00 1.6578794e-01 6.7619291e-02
\end{filecontents*}

\def\readdata#1#2#3#4{
 \def\filenamepattern##1{#1}
 \pgfplotstablesort{\csname #4himu\endcsname}{\filenamepattern{#3}}
 \pgfplotstablesort{\csname #4lomu\endcsname}{\filenamepattern{#2}}
 % other stuff
}
\readdata{testfile#1.dat}{10}{50}{testdata}
\begin{document} 
 \begin{tikzpicture}
  \begin{axis}[ymode=log]
   \addplot table[x=pT,y=lomean] {\testdatahimu};
   \addplot table[x=pT,y=lomean] {\testdatalomu};
  \end{axis}
 \end{tikzpicture}
\end{document}

Responder1

eu acho que

\newcommand\readdata[4]{%
  \def\filenamepattern##1{#1}%
  \expandafter\pgfplotstablesort\csname #4himu\endcsname{\filenamepattern{#3}}%
  \expandafter\pgfplotstablesort\csname #4lomu\endcsname{\filenamepattern{#2}}%
  % other stuff
}
\readdata{testfile#1.dat}{10}{50}{testdata}

servirá, pois \pgfplotstablesortserá apresentado um nome de macro.

Por que seu \expandaftertrabalho não funcionou? Porque você tentou expandir uma chave. Se for necessária uma cinta (acho que não), use

\expandafter\pgfplotstablesort\expandafter{\csname #4himu\endcsname}{\filenamepattern{#3}}%

Não tenho certeza sobre a função de \filenamepattern, mas a macro é sua.

informação relacionada