1

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}

1 Answer 1

1

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}


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .