This is somewhat of an expansion of the previous question:Macro to apply a custom command across a comma-separated list
I'm working on creating a garden journal in LaTex. I have a .csv database file of plants (db-1.csv) from which I create subset tables for each area of my garden using conditions from the datatool package, which has been working great.
I would like to index the plants that are pulled from the database in each of my tables. I am currently using the makeidx package. This way, my final index will include where a particular plant was mentioned even if the mention occurred in a table and not in the text directly.
How can I make my apply macro work on the results of my datatool conditions (\theback)?
Here's my MWE:
\documentclass[12pt]{book}
\usepackage{datatool}
\usepackage{colortbl}
%necessary to color a table
\definecolor{lightmauve}{rgb}{0.86, 0.82, 1.0}
\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing
% This command is for applying a single command to a comma-separated list of tokens, and listing the results separated by ", " (I removed the ``,'').
\makeatletter
\newcommand{\apply}[3][]{
% #1: optional separator to print between applications; default=[, ]
% #2: command to apply;
% #3: list to apply command to
\def\itemsep{\def\itemsep{#1}} % first call to \itemsep prints nothing; later calls print #1
\def\zz{\itemsep#2}%
\@for \listelement:=#3\do{\expandafter\zz\expandafter{\listelement}}%
}
\makeatother
%%%%%%%%%%%%%%%%%%%
\DTLloaddb{dbsample}{db-1.csv}
\begin{document}
%First I'm creating my custom comma-separated list, the result of datatool conditions. Can be called using \theback
\def\theback{\DTLforeach*[\DTLisSubString{\Location}{back}]{dbsample}%
{\Name=Name,\Variety=Variety,\Location=Location,\Established=Established}%
{\Name!\Variety, }}
\vspace{10mm}
%%% Now ideally I'd like to have
%\apply{\index}{\theback}
%to work in my table, but it does not. This did not work, I may have to figure out how to set a macro (or definition?) within a macro???
%Next, this is my table.
\begin{table}[htbp]
\caption{Plants that are pre-existing are highlighted in \colorbox{lightmauve}{lightmauve}}
\vspace{5mm}
\centering
\begin{tabular}{lllr}
\bfseries Name &
\bfseries Variety&
\bfseries Location &
\bfseries Established %
\DTLforeach*[\DTLisSubString{\Location}{back}]{dbsample}%
{\Name=Name,\Variety=Variety,\Location=Location,\Established=Established}
{%
\DTLifSubString{\Established}{yes}{\\\rowcolor{lightmauve}}{\\\rowcolor{white}}%
\Name & \Variety & \Location & \Established}
\end{tabular}
\apply{\index}{Daffodil!apricot,St. John's Wort}
%the above will work, but what I'm hoping for is instead, something along the lines of:
%\apply{\index}{\theback}
\end{table}
\printindex % Output the index
\end{document}
Sorry, I'm not sure if I'm referring to things with the correct names. Any suggestions or help would be greatly appreciated. Thank you so much!