TransWikia.com

pgfplotstable: Read out and define colors from table

TeX - LaTeX Asked on October 1, 2021

In a table, the 3rd column indicates a color.

pgfplotstableread[col sep=comma, header=false]{
a, 1, blue
b, 2, cyan
c, 3,  
d, 3, 
e, 5, red
f, 6,
g, 7,
h, 8,
i, 9,
j, 10,
k, 11, yellow
}mydata

How can I do that the previous color is kept as long as no new one is defined?

I don’t want to have to write the color everywhere. Only once, for example red, then red should be kept until e.g. yellow stands.

enter image description here

documentclass[a4paper]{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}
begin{document}
pgfplotstableread[col sep=comma, header=false]{
a, 1, blue
b, 2, cyan
c, 3,  
d, 3, 
e, 5, red
f, 6,
g, 7,
h, 8,
i, 9,
j, 10,
k, 11, yellow
}mydata

pgfplotstablegetrowsof{mydata}
pgfmathtruncatemacro{RowsNo}{pgfplotsretval-1}

Actual: 
pgfplotsinvokeforeach{0,...,RowsNo}{%%
pgfplotstablegetelem{#1}{[index] 2}of{mydata}
pgfplotsretval,  
%colorlet{barcolor#1}{pgfplotsretval}
}%%

Target: 
blue, cyan, cyan, cyan, red, red, red, red, red, red, red,
end{document}

One Answer

I get it:

documentclass[a4paper]{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}
begin{document}
pgfplotstableread[col sep=comma, header=false]{
a, 1, blue
b, 2, cyan
c, 3,  
d, 3, 
e, 5, red
f, 6,
g, 7,
h, 8,
i, 9,
j, 10, yellow
k, 11, 
}mydata

pgfplotstablegetrowsof{mydata}
pgfmathtruncatemacro{RowsNo}{pgfplotsretval-1}


xdeftempcolor{black}% 
pgfplotsinvokeforeach{0,...,RowsNo}{%%
pgfplotstablegetelem{#1}{[index] 2}of{mydata}
xdeftempcolor{ifx pgfplotsretvalempty tempcolor else pgfplotsretval fi}
colorlet{barcolor#1}{tempcolor}% define
colorbox{tempcolor}{tempcolor}, % show
}%%
end{document}

Answered by cis on October 1, 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