
これはこれの続編です質問外部ファイルのデータを使用してポイントをグラフ化しようとしています。垂直座標は国、水平座標は年です。これを機能させるには、axis
国のリストを記号座標として環境に提供する必要がありますが、ここで問題が発生します。
以下の MWE では、最初の tikzpicture で国のリストを手動で指定していますが、すべて問題ありません。2 番目の tikzpicture では、パッケージ\CatchFileDef
のコマンドを使用して外部ファイルからロードしたリストを指定しています。このコマンドには手動で指定したものとまったく同じテキスト (つまり、AUT、GER、FRA) が含まれているように見えますcatchfile
が、これによりエラーが発生します。\listcountries
\CatchFileDef
このコンテキストではなぜ機能しないのか説明していただけますか?
\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
引数は展開されません。\begin{axis}
オプションを吸収する前に展開する必要があります。
\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}