TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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.

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

\indexprologue{\small In this index you’ll find only the checicals}

% We'll count the number of plants and of chemicals
% Define suitably \indexentry

% Let's count the plants

% Let's count the chemicals



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

share|improve this question
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
up vote 2 down vote accepted

Here is a first prototype:


\NewDocumentCommand{\planthaschemicals}{m m}
   \clist_map_inline:nn { #2 }
     { \plch_add:nn { ##1 } { #1 } }
\cs_new:Npn \plch_add:nn #1 #2
   \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
   \seq_map_inline:cn { g_plch_#1_seq } { ,~##1 }.


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



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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.