3

This is a follow-up to this question. I am trying to get points to be graphed using data from an external file. The vertical coordinates are countries, and the horizontal ones are years. For this to work I need to supply the axis environment with the list of countries as symbolic coordinates, and this is where I have problems.

In the MWE below, in the first tikzpicture I supply the list of countries manually, and everything is fine. In the second tikzpicture, I supply the list as loaded from an external file using \CatchFileDef command from the catchfile package. This creates an error even though the command \listcountries seems to hold the exact same text as the one I was putting manually (namely : AUT,GER,FRA).

Can you explain why \CatchFileDef doesn't work in this context?

\documentclass{standalone}
\usepackage{filecontents,catchfile}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}

\begin{filecontents}{testcountries.txt}
AUT,GER,FRA
\end{filecontents}

\begin{filecontents}{CountryYears.csv}
    #########################################################
    country,year
    AUT,1998
    AUT,1999
    AUT,2000
    GER,1999
    GER,2000
    GER,2001
    GER,2002
    FRA,2000
    FRA,2001
    FRA,2002
    FRA,2003
    #########################################################
\end{filecontents}

\begin{document}
    \pgfplotstableset{columns/country/.style={string type}}
    \pgfplotstableread[col sep=comma]{CountryYears.csv}\loadeddata

    \CatchFileDef{\listcountries}{testcountries.txt}{}

    This is the list of countries from testcountries.txt:   \listcountries 

    \bigskip

    \begin{tikzpicture}
    \begin{axis}[symbolic y coords={AUT,GER,FRA
    },ytick=data,
    x tick label style={/pgf/number format/.cd,%
        scaled x ticks = false,
        set thousands separator={},
        fixed},]
    \addplot[only marks] table[x=year,y=country] \loadeddata;
    \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
    \begin{axis}[symbolic y coords={\listcountries
        },ytick=data,
    x tick label style={/pgf/number format/.cd,%
        scaled x ticks = false,
        set thousands separator={},
        fixed},]
    \addplot[only marks] table[x=year,y=country] \loadeddata;
    \end{axis}
    \end{tikzpicture}

\end{document}
1
  • 1
    TikZ styles do not expand their arguments. Jun 7 '16 at 19:59
3

The argument is not expanded; you need to expand it before \begin{axis} absorbs the options:

\documentclass{standalone}
\usepackage{filecontents,catchfile}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}

\begin{filecontents}{testcountries.txt}
AUT,GER,FRA
\end{filecontents}

\begin{filecontents}{CountryYears.csv}
    country,year
    AUT,1998
    AUT,1999
    AUT,2000
    GER,1999
    GER,2000
    GER,2001
    GER,2002
    FRA,2000
    FRA,2001
    FRA,2002
    FRA,2003
\end{filecontents}

\begin{document}
    \pgfplotstableset{columns/country/.style={string type}}
    \pgfplotstableread[col sep=comma]{CountryYears.csv}\loadeddata

    \CatchFileDef{\listcountries}{testcountries.txt}{}

    This is the list of countries from testcountries.txt:   \listcountries 

    \bigskip

    \begin{tikzpicture}
    \begin{axis}[
      symbolic y coords={AUT,GER,FRA},
      ytick=data,
      x tick label style={
        /pgf/number format/.cd,
        scaled x ticks = false,
        set thousands separator={},
        fixed
      },
    ]
    \addplot[only marks] table[x=year,y=country] \loadeddata;
    \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
    \begingroup\edef\x{\endgroup
      \noexpand\begin{axis}[
        symbolic y coords={\unexpanded\expandafter{\listcountries}},
        ytick=data,
        x tick label style={
          /pgf/number format/.cd,%
          scaled x ticks = false,
          set thousands separator={},
          fixed
        },
      ]}\x
    \addplot[only marks] table[x=year,y=country] \loadeddata;
    \end{axis}
    \end{tikzpicture}

\end{document}
2
  • Thank you! Is there a specific reason for not including the \addplot and \end{axis} into the edef of \x ?
    – Peutch
    Jun 7 '16 at 20:11
  • 1
    @Peutch Because it's not needed and may cause problems.
    – egreg
    Jun 7 '16 at 20:14

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