# find and sort selected words

I am writing a book, where in i have to call back the selected words in different context. Here is my MWE

\documentclass[a4paper,10pt]{article}

%For multiple index
\usepackage{imakeidx}
\makeindex   % is this required?
\makeindex[name=pl,title=Index of plants]
\makeindex[name=ch,title=Index of chemicals]

\begin{document}

I have discussed 3 plants in chapter 1. Rose\index[pl]{Rose},  banana\index[pl]{Banana}
and mango\index[pl]{Mango}.

In chapter 2, i have discussed 2 plants guava\index[pl]{Guava} and coconut\index[pl]{Coconut}.

Totally, i have discussed  \ref{plants-number} plants in \ref{chemicals-number} chemicals in two chapters.

Rose has benzene\index[ch]{Benzene}, toluene\index[ch]{Toluene} and
methane\index[ch]{Methane}. Banana has toluene\index[ch]{Toluene},
ethane\index[ch]{Ethane} and ethyl alcohol\index[ch]{Ethyl alcohol}.
Mango has methanol\index[ch]{Methanol}, ethyl alcohol\index[ch]{Ethyl alcohol}
and benzene\index[ch]{Benzene}. Guava has ethane\index[ch]{Ethane},
acetic acid\index[ch]{Acetic acid}, formalin\index[ch]{Formalin}.
Coconut has acetic acid\index[ch]{Acetic acid}, formalin\index[ch]{Formalin}
and benzene\index[ch]{Benzene}.

Now i would like to have all the plants which has benzene, toluene etc.

\printindex
\indexprologue{\small In this index you’ll find only the plant names}
\printindex[pl]

\indexprologue{\small In this index you’ll find only the checicals}
\printindex[ch]

% We'll count the number of plants and of chemicals
\newcounter{items}
\makeatletter
\begingroup
% Define suitably \indexentry
\newcommand{\indexentry}[2]{%
\@ifundefined{#1}
{\refstepcounter{items}\expandafter\let\csname#1\endcsname\@empty}
{}}

% Let's count the plants
\begingroup
\setcounter{items}{0}
\input{pl.idx}\label{plants-number}
\endgroup

% Let's count the chemicals
\begingroup
\setcounter{items}{0}
\input{ch.idx}\label{chemicals-number}
\endgroup

\endgroup

\end{document}


The question: i would like to have all the plants which has benzene, toluene etc.

-
And the question is? – egreg Jan 31 '12 at 8:33
@egreg, it was already there in the post (before the code). Any way i have included at the end. – Anil Feb 1 '12 at 4:59
You have to link chemicals to plants also in the input. – egreg Feb 1 '12 at 9:14
@egreg, can u elaborate more? – Anil Feb 1 '12 at 9:23

Here is a first prototype:

\documentclass[a4paper,10pt]{book}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\planthaschemicals}{m m}
{
\clist_map_inline:nn { #2 }
{ \plch_add:nn { ##1 } { #1 } }
}
{
\cs_if_exist:cF { g_plch_#1_seq }
{
\seq_new:c { g_plch_#1_seq }
\seq_gput_right:Nn \g_plch_chems_seq { #1 }
}
\seq_gput_right:cn { g_plch_#1_seq } { #2 }
}
\seq_new:N \g_plch_chems_seq
\NewDocumentCommand{\showchemicals}{ }
{
\seq_map_inline:Nn \g_plch_chems_seq { \plch_chems_print:n { ##1 } }
}
\cs_new:Npn \plch_chems_print:n #1
{
\par\medskip
#1~is~connected~to~the~following~plants:
\seq_map_inline:cn { g_plch_#1_seq } { ,~##1 }.
\par\medskip
}
\ExplSyntaxOff

\begin{document}

\planthaschemicals{rose}{benzene,toluene,methane}
\planthaschemicals{banana}{toluene,ethane,ethyl alcohol}
\planthaschemicals{mango}{methanol,ethyl alcohol,benzene}
\planthaschemicals{guava}{ethane,acetic acid,formalin}
\planthaschemicals{coconut}{acetic acid,formalin,benzene}

\showchemicals

\end{document}


Of course you can put \planthaschemicals where you describe the plant.

There are various things to straighten up, but this should be the concept. Essentially, we define a sequence for each new chemical containing the plants to which this chemical is connected and we maintain a sequence containing the names of the chemicals, so that we can print it out.

One should need also to sort the sequences and to get a satisfying printout.

-