0

I would like to create some bar charts using pgfplots. Each chart will have a similar format, but with some different labels and values. I am trying to make a command that will generate my chart from this input data. For example, I would like to be able to type \drawchart{Foo/10, Bar/20} and have this produce an appropriate bar chart. I'm having trouble taking this input "Foo/10, Bar/20" and converting it into a list "Foo,Bar" that symbolic coords can use. Here is a MWE, that doesn't work:

\documentclass{article}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=1.9}

\def\lab{Foo,Bar}
\let\labb\lab

\def\ylabelscmd{\foreach \label/\val in {Foo/1,Bar/2} {{\label},}}
\global\let\ylabels\ylabelscmd


\begin{document}
\ylabelscmd --- \ylabels
\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            %symbolic y coords/.expand once = {\lab}, % works
            %symbolic y coords/.expand once = {\labb}, % works
            %symbolic y coords/.expand once = {\ylabelscmd}, % error: ! Missing \endcsname inserted.
            symbolic y coords/.expand once = {\ylabels}, % error: ! Missing \endcsname inserted.
            ytick = data,
            ]
            \addplot+ coordinates { (1,Foo) (2,Bar) };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

For some reason, it works if the command in the symbolic coords is a hard-coded list, but if I generate it using \foreach it complains. As you can see in the code, I've tried to evaluate the command using \let and using that, but it seems to make no difference (at least with the expand once option).

Any ideas?

Ozzin
  • 23
  • I have different error when compiling your code ! Package pgfplots Error: Sorry, the input coordinate 'Foo' has not been defined with 'symbolic y coords={\foreach \label /\val in {Foo/1,Bar/2} {{\label },}} ... Maybe it has been misspelled? Or did you mean something like [normalized]Fo o?. – Hoang-Ngan Nguyen Jul 04 '16 at 18:34

1 Answers1

0

I guess all you want is to store the symbolic coordinate list in \ylabels. In order to achive that you can use \xdef as in my code below. enter image description here

\documentclass{article}
\usepackage{pgffor,tikz, pgfplots}
\pgfplotsset{compat=1.9}

\def\lab{Foo,Bar}
\let\labb\lab

% use this to store the result of your for loop to \ylabelscmd
\def\ylabelscmd{}
\foreach \label/\val in {Foo/1,Bar/2}{\xdef\ylabelscmd{\ylabelscmd,\label}}

\global\let\ylabels=\ylabelscmd


\begin{document}
\ylabelscmd --- \ylabels

\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            %symbolic y coords/.expand once = {\lab}, % works
            %symbolic y coords/.expanded = {\labb}, % works
            %symbolic y coords/.expand once = {\ylabelscmd}, % error: ! Missing \endcsname inserted.
            symbolic y coords/.expand once={\ylabels}, % error: ! Missing \endcsname inserted.
            ytick = data,
            ]
            \addplot+ coordinates { (1,Foo) (2,Bar) };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
  • Many thanks -- this does just what I want.I don't quite understand why the original code I posted doesn't work. I guess it has something to do with expansion/evaluation, but I was under the impression that the \let version of the command should have expanded the command and evaluate it at the time of \letting. – Ozzin Jul 05 '16 at 08:32
  • As I understand, \let doesn't expand anything, it is just an alias: http://tex.stackexchange.com/questions/258/what-is-the-difference-between-let-and-def . You can try to put the whole for loop inside the axis option symbolic y coords/.expand once={\foreach ...}, it doesn't work too. Even if you use expanded instead of expand once, it still doesn't work. I guess it is related to how pgffor behaves. I am not sure. – Hoang-Ngan Nguyen Jul 05 '16 at 14:46