4

I have a bunch of plots in a datavisualization environment with different styles for each plot. When I collect them into a legend, they all show up in black. I've constructed a quick MWE based on an example in the manual

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

Do I need to specify the colour information somewhere again? When using style sheets, the colours show up correctly in the legend but I would greatly prefer to set colours separately.

enter image description here

  • 1
    are you sure? Because, when I compiled your MWE, it works as you wanted. I see no problems. – Raaja Aug 2 '18 at 14:07
  • Strange, I get the behaviour shown in the image. Can you tell me your TeX version? – Huang_d Aug 2 '18 at 14:09
  • MiKTex-2.9.6300 compiled with Texstudio 2.11.2. I hope that helps. – Raaja Aug 2 '18 at 14:12
  • 2
    I confirm @RaajaG's statement with my TeXLive 2018 distribution. Wait! I do get the OP's output when compiling with xelatex, but with pdflatex and lualatex the colors show correctly. (I do not know if that is related, but it also happens with pstricks that xelatex sometimes fails to produce the correct result.) – marmot Aug 2 '18 at 14:13
  • 1
    @RaajaG Just to be absolutely certain: when you compile with xelatex, the legend curves are colored and not black (as in the OP's screen shot)? – marmot Aug 2 '18 at 14:24
3

As a workaround, you could create a style sheet that uses the same colors as your lines. Then also xelatex creates a colorful legend.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{Huang d}{
  1/.style={green!50!black},
  2/.style={yellow!90!black},
  3/.style={red!80!black},
  default style/.style={black}
}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=Huang d,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

enter image description here

A much simpler possibility is provided by Huang_d's answer, which can be further simplified to

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{xelatex workaround}{
  default style/.style={black}
}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=xelatex workaround,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

This gives a universal output, i.e. same for pdflatex, xelatex and lualatex.

enter image description here

  • You are a wonderful marmot! Thank you very much! I guess I will have to take care of the order of plots now, but at least it works with all constraints. – Huang_d Aug 2 '18 at 14:48
  • @Huang_d Thanks, yes, that's just a workaround. There seems also an issue with xelatex in pstricks, see e.g. here. To the best of my knowledge, this has never been resolved. And I do not know if these issues are related, let alone how to fix them. But I am glad to hear that this workaround works for you. ;-) – marmot Aug 2 '18 at 14:54
  • Playing around with it, you actually still need the normal={style={green}} part, otherwise it is still black. This is some really funky cross-interaction. Setting the style sheet to all black and defining colours in the styles gives the/a desired behaviour, black text and correctly coloured lines. – Huang_d Aug 2 '18 at 14:59
  • 1
    @Huang_d I confirm your observation and am wondering if you want to post this better workaround as an answer. – marmot Aug 2 '18 at 15:02
3

The problem seems to be an interaction between Xe(La)TeX and TikZ. In order to get black text with correctly coloured labels, a black style sheet needs to be used. The colours can then be set as expected:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{allblack}{default style/.style={black}}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=allblack,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

This yields correctly coloured lines and black text. The text colour can be changed with the style sheet if necessary.

  • 1
    You can do it even shorter: \pgfdvdeclarestylesheet{Huang d}{ default style/.style={black} }. – marmot Aug 2 '18 at 15:13
  • I guess you want to either name the style allblack or use style sheet=Huang d in the plot. (I also appended that to my answer, of course giving credit to your answer.) – marmot Aug 2 '18 at 15:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.