Consider this MWE:

enter image description here


% DATA 1
1 1
2 4
3 9
4 16
5 25

% DATA 2
1 15
2 10
3 7
4 5
5 2
6 1

% see https://tex.stackexchange.com/questions/393622/using-key-values-in-newdocumentcommand
\pgfkeys{/myplot/.is family, /myplot,

\NewDocumentCommand\myplot{m} {
    \pgfqkeys{/myplot}{#1}% keep key changes local
        \addplot [\myplotvals{linecolor},mark=none] table {\myplotvals{filename}};




Using pgfkeys I've created a command called \myplot which takes data from a .txt as an input and you can optionally set the line color.

This works well for 1 data set. My question is, does anyone know how I could adapt this method to allow adding multiple data sets.

In my MWE, I've included a second data set called data2.txt. What I want the command to do is essentially generate something like this:

\addplot [red,mark=none] table {data1.txt};
\addplot [blue,mark=none] table {data2.txt};

Basically, it would be great if I could specify a second .txt file (or potentially n text files) and it would automatically add more \addplot commands as required. Is this even possible to do? I don't know what the input would have to look like, maybe something like \myplot{filename={data1.txt,data2.txt},linecolor={red,blue}} ? But perhaps that doesn't work?


Just to be clear, although in my MWE the file names are sequential, in my real example the file names do not iterate numerically. Therefore I have to specify each file name individually.

  • pgfplots already has cycle lists to deal with the color list so all you need is to rewrite the command in such a way that it loops over file names, which is straightforward. – marmot May 20 at 17:49

Your Answer

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

Browse other questions tagged or ask your own question.