Das Ergebnis von Catchfile funktioniert nicht in einer TikzPicture-Umgebung

Das Ergebnis von Catchfile funktioniert nicht in einer TikzPicture-Umgebung

Dies ist eine Fortsetzung davonFrage. Ich versuche, Punkte mithilfe von Daten aus einer externen Datei grafisch darzustellen. Die vertikalen Koordinaten sind Länder, die horizontalen Jahre. Damit dies funktioniert, muss ich der axisUmgebung die Liste der Länder als symbolische Koordinaten bereitstellen, und hier habe ich Probleme.

Im MWE unten gebe ich im ersten Tikzbild die Länderliste manuell ein und alles ist in Ordnung. Im zweiten Tikzbild gebe ich die Liste so ein, wie sie aus einer externen Datei geladen wurde, indem ich \CatchFileDefeinen Befehl aus dem catchfilePaket verwende. Dies erzeugt einen Fehler, obwohl der Befehl \listcountriesscheinbar genau denselben Text enthält wie der, den ich manuell eingegeben habe (nämlich: AUT,GER,FRA).

Können Sie erklären, warum \CatchFileDefdas in diesem Zusammenhang nicht funktioniert?

\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}

Antwort1

Das Argument wird nicht erweitert. Sie müssen es erweitern, bevor \begin{axis}es die Optionen absorbiert:

\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}

verwandte Informationen