[Tex/LaTex] Nested CSV with csvsimple

csvcsvsimpleloopsnestingtables

I have two csv files.
I want to use the first (countries.csv) to generate sections, and then filter the second (bottles.csv) by the name of the section.
Below is an MWE – you can see that the nested file is read correctly, but csvsimple does not continue with the next row of the parent file (only reads "Argentina").
I also include the sample with filtering, which seems to indicate that as soon as reading the nested CSV everything else about the parent CSV is forgotten.

How to nest (loop?) through two different tables and apply filters?

Thanks!

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}

\begin{filecontents*}{countries.csv}
country
Argentina
Belgium
\end{filecontents*}

\begin{filecontents*}{bottles.csv}
bottlename, bottlecountry
Bottle A1, Argentina
Bottle B1, Belgium
Bottle A2, Argentina
Bottle C1, China
\end{filecontents*}

\begin{document}

Table of countries.csv (Parent)\\
\csvreader[head to column names]{countries.csv}{}{%
\country\\
}

Table of bottles.csv (Child)\\
\csvreader[head to column names]{bottles.csv}{}{%
\bottlename \bottlecountry\\
}

Table of bottles nested in table of countries. Parent file is not read after \emph{Argentina}\\
\csvreader[head to column names]{countries.csv}{}{%
\section{\country}
\csvreader[head to column names]{bottles.csv}{}{%
\bottlename \bottlecountry\\
}
}

Table of bottles nested in table of countries, with filtering (manually chose "Argentina")\\
\csvreader[head to column names]{countries.csv}{}{%
\section{\country}
\csvreader[filter equal={\bottlecountry}{Argentina},head to column names]{bottles.csv}{}{%
\bottlename \bottlecountry\\
}
}

Table of bottles nested in table of countries, with filtering. Does not remember the country from the parent file.\\
\csvreader[head to column names]{countries.csv}{}{%
\section{\country}
\csvreader[filter equal={\bottlecountry}{\country},head to column names]{bottles.csv}{}{%
\bottlename \bottlecountry\\
}
}

\end{document}

Best Answer

First, you cannot nest a \csvreader into another \csvreader. But, you could read the countries, remember them, and then read the bottles.

In my following suggestion, I use the list features from the etoolbox package to store the country names into a \countrylist. Next, we loop through this list using \dolistloop which itself uses the \do macro. This \do macro contains the reading of the bottles:

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}
\usepackage{etoolbox}

\begin{filecontents*}{countries.csv}
country
Argentina
Belgium
\end{filecontents*}

\begin{filecontents*}{bottles.csv}
bottlename, bottlecountry
Bottle A1, Argentina
Bottle B1, Belgium
Bottle A2, Argentina
Bottle C1, China
\end{filecontents*}

\begin{document}

\csvreader[head to column names]{countries.csv}{}{%
  \listeadd{\countrylist}{\country}%
}

\renewcommand*{\do}[1]{%
  \section{#1}%
  \csvreader[filter equal={\bottlecountry}{#1},head to column names]{bottles.csv}{}{%
    \bottlename\ (from \bottlecountry)\\
  }%
}
\dolistloop{\countrylist}

\end{document}

enter image description here