我的问题是,我有一个作者列表,我会在几个地方用它做几件事。例如,我想生成一个作者索引。在下面的代码中,我想“重新处理”每个作者的文本,例如将姓氏放在前面,后面加一个逗号,然后是首字母。最后将这些重新处理的名字放入索引中。我希望我可以使用 Latex 和forcsvlist
扩展来完成它。我有以下示例 tex 文件:
\documentclass{book}
\usepackage{etoolbox}
\usepackage{multicol}
\usepackage{makeidx}
\usepackage{multind}
\makeindex{a}
\makeindex a
\newcommand{\pr}[2]{#1}
\begin{document}
\newcommand{\lastnameelem}{}
\DeclareListParser*{\fordotlist}{.}
\newcommand{\ccgetlastfunc}[1]{\renewcommand{\lastnameelem}{#1}}
\newcommand{\ccgetlast}[1]{\renewcommand{\lastnameelem}{}\fordotlist{\ccgetlastfunc}{#1}\lastnameelem}
\newcommand{\ccgetexceptlastfunc}[1]{\lastnameelem\renewcommand{\lastnameelem}{#1.}}
\newcommand{\ccgetexceptlast}[1]{\renewcommand{\lastnameelem}{}\fordotlist{\ccgetexceptlastfunc}{#1}}
\newcommand{\ccsurnametofront}[1]{\ccgetlast{#1}, \ccgetexceptlast{#1}}
\newcommand{\ccauthorformattedmakefunc}[1]{\ccsurnametofront{#1};}
\newcommand{\ccauthorformattedmake}[1]{\forcsvlist\ccauthorformattedmakefunc{#1}}
\DeclareListParser*{\forsemmicommalist}{;}
\newcommand{\ccauthorlabela}{}
\newcommand{\ccauthormakefunca}[1]{\index{a}{\protect#1|pr{\protect\ref{\ccauthorlabela}}}}
\newcommand{\ccauthormakea}[2]{\renewcommand{\ccauthorlabela}{#1}\forsemmicommalist\ccauthormakefunca{\ccauthorformattedmake{#2}}}
\chapter{XXXXXX}
\label{aaa}
\ccauthormakea{aaa}{A. AAAA, B. BBBBB, C. CCCCC, D. DDDDD, E. EEEE}
Something, something something something something something something
something something something something something
\clearpage
\setlength{\columnsep}{3mm}
\setlength{\columnseprule}{0mm}
\begin{multicols}{2}
\input{a.ind}
\end{multicols}
\end{document}
我希望该\ccauthormakea
部分扩展为以下内容:
\index{AAAA, A.|\ref{aaa}}
\index{BBBB, B.|\ref{aaa}}
\index{CCCC, C.|\ref{aaa}}
\index{BBBB, D.|\ref{aaa}}
\index{EEEE, E.|\ref{aaa}}
但是我将在 a.ind 文件中得到以下内容:
\indexentry{\ccauthorformattedmake {A. AAAA, B. BBBBB, C. CCCCC, D. DDDDD, E. EEEE}|pr{\ref {aaa}}}{1}
我试图将\expandafter
其放入\ccauthormakea
,但就是无法让它工作。不知何故,我无法让一个forcsvlist
使用另一个的扩展结果forcsvlist
。有人能帮忙吗?
答案1
我不懂etoolbox
语法,而且我的系统上也没有,multind
所以不太确定你想要的语法(我不知道|
这里的意思)但无论如何,下面将生成这个.idx
文件:
\indexentry{AAAA, A.|pr{\ref{aaa}}}{1}
\indexentry{BBBBB, B.|pr{\ref{aaa}}}{1}
\indexentry{CCCCC, C.|pr{\ref{aaa}}}{1}
\indexentry{DDDDD, D.|pr{\ref{aaa}}}{1}
\indexentry{EEEE, E.|pr{\ref{aaa}}}{1}
您可能希望用\ccauthorparse
更强大的东西来替换它,因为我不知道输入(首字母)是否有多个点,并且没有考虑到这种可能性。
\documentclass{book}
\usepackage{makeidx}
\makeindex
\newcommand{\pr}[2]{#1}
\usepackage{xinttools}
\newcommand\ccauthormakea[2]{%
\xintFor ##1 in {#2}:
{%
\begingroup
\ccauthorparse{##1}%
\index{\surname, \initials|pr{\string\ref{#1}}}%
\endgroup
}%
}%
\makeatletter
\newcommand\ccauthorparse[1]{\ccauthor@parse #1\ccauthor@parse}
\def\ccauthor@parse #1. #2\ccauthor@parse
{%
\def\surname{#2}\def\initials{#1.}%
}%
\makeatother
\begin{document}
\chapter{XXXXXX}
\label{aaa}
\ccauthormakea{aaa}{A. AAAA, B. BBBBB, C. CCCCC, D. DDDDD, E. EEEE}
Something, something something something something something something
something something something something something
\printindex
\end{document}
答案2
不确定如何使用 来实现etoolbox
,但expl3
和xparse
可以帮助您实现:
\documentclass{book}
\usepackage{multicol}
\usepackage{makeidx}
\usepackage{multind}
\usepackage{xparse}
\makeindex{a}
\newcommand{\pr}[2]{#1}
\ExplSyntaxOn
\NewDocumentCommand{\ccauthormakea}{mm}
{
\seq_gset_split:Nnn \g_ivanyi_authors_seq { ; } { #2 }
\seq_map_inline:Nn \g_ivanyi_authors_seq
{
\__ivanyi_authors_index:nn { #1 } { ##1 }
}
}
\seq_new:N \g_ivanyi_authors_seq
\cs_new_protected:Nn \__ivanyi_authors_index:nn
{% #1 is the label, #2 is the author
\index{a}
{
#2
|pr { \exp_not:N \protect \exp_not:N \ref { #1 } }
}
}
\ExplSyntaxOff
\begin{document}
\chapter{XXXXXX}
\label{aaa}
\ccauthormakea{aaa}{AAAA, A.; BBBBB, B.; CCCCC, C.; DDDDD, D.; EEEE, E.}
Something, something something something something something something
something something something something something
\clearpage
\setlength{\columnsep}{3mm}
\setlength{\columnseprule}{0mm}
\begin{multicols}{2}
\InputIfFileExists{a.ind}{}{\typeout{Remember to run `makeindex a'}}
\end{multicols}
\end{document}
我强烈建议您不要依赖诸如 这样的作者姓名格式A. AAAA
,因为如果有多个首字母或姓氏重复,这种格式将非常脆弱:完全不清楚在哪里分割条目。 诸如
AAAA, A.; BBBB, B.
更加清晰,使解析更加容易。
版本包含imakeidx
以下内容:
\documentclass{book}
\usepackage{multicol}
\usepackage{imakeidx}
\usepackage{xparse}
\makeindex[name=a,title=Author Index]
\newcommand{\pr}[2]{#1}
\ExplSyntaxOn
\NewDocumentCommand{\ccauthormakea}{mm}
{
\seq_gset_split:Nnn \g_ivanyi_authors_seq { ; } { #2 }
\seq_map_inline:Nn \g_ivanyi_authors_seq
{
\__ivanyi_authors_index:nn { #1 } { ##1 }
}
}
\seq_new:N \g_ivanyi_authors_seq
\cs_new_protected:Nn \__ivanyi_authors_index:nn
{% #1 is the label, #2 is the author
\index[a]
{
#2
|pr { \exp_not:N \protect \exp_not:N \ref { #1 } }
}
}
\ExplSyntaxOff
\begin{document}
\chapter{XXXXXX}
\label{aaa}
\ccauthormakea{aaa}{AAAA, A.; BBBBB, B.; CCCCC, C.; DDDDD, D.; EEEE, E.}
Something, something something something something something something
something something something something something
\printindex[a]
\end{document}