TransWikia.com

Using a pre-defined list inside a pgfplotset style

TeX - LaTeX Asked by fafroo on March 9, 2021

I would like to place defined markers on certain datapoints in a plot using a predefined list of tuples defdatalist{datapoint/mark}. I found a suitable solution of the same problem with labels which relies upon the nodes near coords. Unfortunately, it works only if the datalist is explicitly contained in the addplot+[nodes near some coords={datapoint/mark}]. The code does not compile if the macro datalist is used directly in addplot+[nodes near some coords=datalist]. I guess a solution is related to Using Macro Defined Lists in TikZ/PGFplots but none of the workarounds worked for me.

A MWE:

documentclass[tikz,border=10pt]{standalone}
usepackage{pgfplots}
usetikzlibrary{shapes}

defdatalist{0/rectangle,
           2/circle,
           4/diamond}

pgfplotsset{
    compat=1.16,
    %%% 
    node near coord/.style args={#1/#2}{% Style for activating the label for a single coordinate
        nodes near coords*={
            ifnumcoordindex=#1 fi
        },
        scatter/@pre marker code/.append code={
            ifnumcoordindex=#1 pgfplotsset{every node near coord/.append style=fill, draw,#2}fi
        }
   },
   nodes near some coords/.style={ % Style for activating the label for a list of coordinates
       scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
       scatter/@post marker code/.code={},%
       node near coord/.list={#1}% Run "node near coord" once for every element in the list
   }
}
begin{filecontents}{datafile.csv}
1.00    1.00
2.00    2.00
3.00    3.00
4.00    4.00
5.00    5.00
end{filecontents}

begin{document}
begin{tikzpicture}
  begin{axis}[]
      addplot
      +[
          %nodes near some coords=datalist, % doesn't compile, returns 'File ended while scanning use of pgfkeys@code. par  mwe.tex'
          nodes near some coords={0/rectangle,  % works
                                 2/circle,
                                 4/diamond}, 
      ]
        table []{datafile.csv};
end{axis}
end{tikzpicture}
end{document}

One Answer

You need the /.expanded key handler, i.e. nodes near some coords/.expanded=datalist.

documentclass[tikz,border=10pt]{standalone}
usepackage{pgfplots}
%pgfplotsset{compat=1.17} %<-consider adding
usetikzlibrary{shapes}

defdatalist{0/rectangle,
           2/circle,
           4/diamond}

pgfplotsset{
    compat=1.16,
    %%% 
    node near coord/.style args={#1/#2}{% Style for activating the label for a single coordinate
        nodes near coords*={
            ifnumcoordindex=#1 fi
        },
        scatter/@pre marker code/.append code={
            ifnumcoordindex=#1 pgfplotsset{every node near coord/.append style=fill, draw,#2}fi
        }
   },
   nodes near some coords/.style={ % Style for activating the label for a list of coordinates
       scatter/@pre marker code/.code={},% Reset the default scatter style, so we don't get coloured markers
       scatter/@post marker code/.code={},%
       node near coord/.list={#1}% Run "node near coord" once for every element in the list
   }
}
begin{filecontents}[overwrite]{datafile.csv}
1.00    1.00
2.00    2.00
3.00    3.00
4.00    4.00
5.00    5.00
end{filecontents}

begin{document}
begin{tikzpicture}
  begin{axis}[]
      addplot
      +[
          nodes near some coords/.expanded=datalist, % works
      ]
        table []{datafile.csv};
end{axis}
end{tikzpicture}
end{document}

Correct answer by user230294 on March 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP