# forcsvlist to use the expanded result of another forcsvlist

My problem is, that I have a list of authors, that I use at several places for several things. For example I would like to generate an Author's index. In the code below I would like to "rework" the text of every author, for example bring the surname forward, put a comma afterwards and then the initials. Finally put these reworked names into an index. I was hoping that I can do it with Latex with forcsvlist and expansion. I have the following sample tex file:

\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{\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}}}}

\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}


I would like the \ccauthormakea part to expand to the following:

\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}}


However I will get in the a.ind file the following:

\indexentry{\ccauthorformattedmake {A. AAAA, B. BBBBB, C. CCCCC, D. DDDDD, E. EEEE}|pr{\ref {aaa}}}{1}


I was trying to place \expandafter into \ccauthormakea, but I just could not make it work. Somehow I cannot make one forcsvlist to use the expanded result of another forcsvlist. Can somebody help?

I don't know etoolbox syntax, and I don't have multind on my system so not so sure about the syntax you want (I don't know what the | means here) but anyway, this below will produce this .idx file:

\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}


You may want to replace its \ccauthorparse by something more robust, as I don't know if input (initials) may have multiple dots and did not account for that possibility.

\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}

• I understand now the strange thing with \pr... you want to replace page number by using \ref macro. The problem is that this was contradictory with that part of the question saying "I would like the \ccauthormakea part to expand to the following:", hence I focused on what was asked there .... – user4686 Jan 11 at 8:00

Not sure how you can do it with etoolbox, but expl3 and xparse can get you through:

\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}


I warmly suggest you not to rely on an author name format such as A. AAAA, because this will be very fragile in case there are more than one initial or a surname is double: it will not be clear at all where to split the entry. A format such as

AAAA, A.; BBBB, B.


is much clearer and makes parsing much easier.

The version with 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}

• @PeterIvanyi As I said, I can't recommend the A. AAAA` format. – egreg Jan 10 at 22:51