6

This is probably a silly way of doing it but bear with me.

I have a document in which I define values in lua tables to reuse them either in a tables or in a pgf plot after applying some calculations to them through.

My issue is that although this method seems to work for almost every thing, when I define my ybar plot in pgf and specifically the symbolic x coords values, it fails to work in that it doesn't parse anything given at that point.

I thought at first it was a directlua thing but in fact even a LaTeX command defined with the same values fails to work either.

It is only when I manually enter the values I want, that the document compiles. Otherwise I get an error along the lines of

! Package pgfplots Error: Sorry, the input coordinate `1 day' has not been defined with 'symbolic x coords={\xcoordlabelsii }... Maybe it has been misspelled? Or did you mean something like [normalized]1 day?.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgf}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{units}

\directlua{
    labelset={"1 day","1 month","2 months","3 months","6 months","1 year"}
    worstcasecache = {3.40, 103.30, 206.61 ,309.91, 619.82, 1239.65 }
    bestcasecache = {0.74, 22.37, 44.74, 67.11, 134.22, 268.44}
}

\begin{document}
\begin{tikzpicture}
\newcommand{\xcoordlabelsi}{\directlua{tex.print{table.concat(labelset, " , ")}}}
\newcommand{\xcoordlabelii}{1 day,1 month,2 months,3 months,6 months,1 year}
\begin{axis}[
width = \textwidth,
height = 0.65\textwidth,
ylabel near ticks,
use units,
%x tick label style={/pgf/number format/1000 sep},
ylabel=Data volume,
y unit = B,
y unit prefix = Ti,
symbolic x coords={1 day, 1 month, 2 months, 3 months, 6 months, 1 year}, % works as expected
% symbolic x coords = \directlua{tex.print("{" .. table.concat(labelset, " , ") .. "}")}, % doesn't work
%symbolic x coords = {\xcoordlabelsi}, % doesn't work
%symbolic x coords = {\expandafter\xcoordlabelsii}, % doesn't work
%enlargelimits=0.15,
ybar,
bar width=10pt,
xtick=data,
x tick label style={rotate=45,anchor=east},
ymin = 1,
%xscale = 2,
legend style = {draw=none},
legend cell align = left,
legend pos = north west,
]
\addplot coordinates {\directlua{
    for i=1,6 do
        tex.print("(" .. labelset[i] .. "," .. bestcasecache[i]*100/1024 .. ")")
    end}
};
\addplot coordinates {\directlua{
    for i=1,6 do
        tex.print("(" .. labelset[i] .. "," .. worstcasecache[i]*100/1024 .. ")")
    end}
};

\legend{Best case, Worst case}
\end{axis}
\end{tikzpicture}
\end{document}
  • surprised noone has anything to say about this one at all. – ArTourter Jun 10 '16 at 22:34
  • 2
    The usual issue here: key values are not expanded when they are read, so you are passing the literal \xcoordlabelsi not its expansion. – Joseph Wright Jun 13 '16 at 9:11
  • @JosephWright why does it work with \textwidth for the width and height values then? is there a way to pass the expansion? I tries with \expandafter but without really knowing what it would do. – ArTourter Jun 13 '16 at 9:15
  • Or are you saying that it is something in pgfplots itself which doesn't expand the values for certain keys? – ArTourter Jun 13 '16 at 9:17
4
+50

Some keys don't execute/expand their argument but parse them immediately. Imagine you had some fragile macros in your symbolic labels which would lead to errors if you have expanded them or some other scenario might lead to problems. Hence you have to provide the expanded version.

For the second macro you have defined, if you actually force the macro to be expanded it works as expected:

symbolic x coords/.expand once = \xcoordlabelii,

gives

enter image description here

For the first argument, if you expand it once, you get the typical braced version of the array hence pgfplots think of it as a single entity and we get

symbolic x coords/.expand once = \xcoordlabelsi,

enter image description here

That we can expand further. So if you use expand twice or expanded instead you will see that it will complain because a day is not the same as <_>a day<_> with <_> denoting the whitespace characters. Hence you have to remove the whitespace in the directlua code

\newcommand{\xcoordlabelsi}{\directlua{tex.print{table.concat(labelset,",")}}}

and then use

symbolic x coords/.expand twice = \xcoordlabelsi, 

it works with the output same as the first picture.

  • 1
    Thanks! That works brilliantly. One can even get the \direclua line directly after the \.expand once bit. it does make perfect sense now. – ArTourter Jun 13 '16 at 11:12
  • @ArTourter My pleasure. – percusse Jun 13 '16 at 11:21

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.