我正在写一本书,其中我必须在不同的上下文中调用选定的单词。这是我的 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}
问题:我想要所有含有苯、甲苯等的植物。
答案1
这是第一个原型:
\documentclass[a4paper,10pt]{book}
\usepackage{xparse}
\ExplSyntaxOn
\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
{
\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}
当然,你可以把它放在\planthaschemicals
你描述植物的地方。
有很多事情需要理清,但概念应该是这样的。本质上,我们为每种新化学物质定义一个序列,其中包含与该化学物质相关的植物,并维护一个包含化学物质名称的序列,以便我们可以将其打印出来。
还需要对序列进行排序并获得令人满意的打印输出。