
這是此內容的後續內容問題。我正在嘗試使用外部文件中的數據來繪製點。縱座標是國家,橫座標是年份。為此,我需要向axis
環境提供國家/地區列表作為符號座標,這就是我遇到問題的地方。
在下面的 MWE 中,在第一個 tikzpicture 中,我手動提供了國家/地區列表,一切都很好。在第二張 tikzpicture 中,我使用套件\CatchFileDef
中的指令提供從外部檔案載入的清單catchfile
。即使該命令\listcountries
似乎包含與我手動輸入的文字完全相同的文字(即:AUT、GER、FRA),這也會產生錯誤。
您能解釋一下為什麼\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}