# Pre-expansion of moving macro argument

I am using databib package (datatool bundle) to get information out of a BibTeX database for arbitrary use in a LaTeX file. It provides a command to loop over the BibTeX entries. Within such a loop, one can call the (undocumented) macro \DTLformatauthorlist that formats the list of authors for that entry (according to pre-defined format).

Now, I would like to use this list in the table of contents (using the \chapterprecistoc macro of the memoir package). Because the table of contents is typeset from data found in the toc-file, the typesetting occurs outside of the loop body. Therefore, the argument of \chapterprecistoc must contain the expanded version of \DTLformatauthorlist. However, I do not know how to do this.

Based on some discussions about expansions on this site, I tried

\expandafter\chapterprecistoc\expandafter{\DTLformatauthorlist}


but that results in error due to the line

\precistoctext {\let }


in the toc-file.

I do know, however, that \precistoctext does some expansion of its arguments: with databib one can access BibTeX entry fields using the \DTLbibfield macro. Then

\chapterprecistoc{\DTLbibfield{Author}}


does put the expansion of \DTLbibfield{Author} in the toc file. So I guess that \DTLformatauthorlist needs ‘more’ expansion.

A requested minimal example:

\begin{filecontents}{data.bib}
@inproceedings{test,
Author = {Author One and Auteur Twee},
Title = {An expansive approach to author list transportation},
}
\end{filecontents}
\documentclass{memoir}
\usepackage{databib}

\begin{document}
\tableofcontents

% GET DATA OUT OF BIBFILE INTO DB
\nocite{*}

\DTLforeachbibentry*{data}{
% GET DATA OUT OF DB INTO MACROS
\providecommand{\Authors}{\DTLbibfield{Author}}
\providecommand{\Title}{\DTLbibfield{Title}}
\chapterprecistoc{\Authors} % works, just for illustration
%     \chapterprecistoc{\DTLformatauthorlist} % does not work
}
\end{document}

• No, that's not going to work: just one expansion is apparently insufficient; and the \let tells me that you don't get at the list by pure expansion. A minimal example of what you want to do is necessary. – egreg May 23 '15 at 20:52
• I added a minimal example. – equaeghe May 23 '15 at 21:22

Instead of trying to get the expanded authorlist in the toc, you could pass only the cite key and then call the database again in the toc:

\documentclass{memoir}
\usepackage{databib}
\newcommand\printauthorlist{}
\DeclareRobustCommand\printauthorlist[1]{%
\DTLforeachbibentry[\DTLbibfieldiseq{CiteKey}{#1}]{data}{\DTLformatauthorlist}}

\begin{document}
\nocite{*}

\tableofcontents

\DTLforeachbibentry*{data}{
% GET DATA OUT OF DB INTO MACROS
\providecommand{\Authors}{\DTLbibfield{Author}}
\providecommand{\Title}{\DTLbibfield{Title}}
\chapterprecistoc{\printauthorlist{\DBIBcitekey}} % works, just for illustration
}
\end{document}


(I don't know databib well enough to know if the test against CiteKey is the best way to print a single item. I normally use biblatex for things like this.)

• This works well enough for me, but as suggested may not be the most efficient approach. In case someone needs to make this more efficient: for each iteration in the loop, the index DTLbibrow is available, it could perhaps be used with \dtl@gathervalues{data}{DTLbibrow} to more efficiently access the wanted entry (cf. source of \DTLformatthisbibentry). – equaeghe May 25 '15 at 21:07

The \DTLformatauthorlist seems to be meant just for printing immediately the current author list.

I propose to get its definition and convert it to another macro with an argument, which we extract from the database in expandable form with \DTLbibfieldlet.

The same macro should be used for the title and the temporary macros should be expanded (once) immediately, or you'd get only the last set, in case more entries are present in the .bib file.

\begin{filecontents}{\jobname.bib}
@inproceedings{test,
Author = {Author One and Auteur Twee},
Title = {An expansive approach to author list transportation},
}
\end{filecontents}
\documentclass{memoir}
\usepackage{databib}

\makeatletter
\protected\def\formatauthorlist#1{%
\DTLstartsentencespace
\@dtl@authorcount=0\relax
\@for\@dtl@author:=#1\do{%
\@dtl@tmpcount=0\relax
\ifnum\@dtl@authorcount>\c@DTLmaxauthors
{%
\@for\@dtl@author:=#1\do{%
\ifnum\@dtl@tmpcount=1\relax
\expandafter\DTLformatauthor\@dtl@author
\else
\ifnum\@dtl@tmpcount>\c@DTLmaxauthors
\DTLandnotlast \etalname
\expandafter\DTLcheckendsperiod\expandafter{\etalname}%
\@endfortrue
\else
\DTLandnotlast \expandafter\DTLformatauthor\@dtl@author
\fi
\fi
}%
}%
\else
\@for\@dtl@author:=#1\do{%
\ifnum\@dtl@tmpcount=1\relax
\expandafter\DTLformatauthor\@dtl@author
\else
\ifnum\@dtl@tmpcount=\@dtl@authorcount
\ifnum\@dtl@authorcount=2\relax
\DTLtwoand
\else
\DTLandlast
\fi
\expandafter\DTLformatauthor\@dtl@author
\else
\DTLandnotlast \expandafter\DTLformatauthor\@dtl@author
\fi
\fi
}%
\fi
}
\makeatother

\begin{document}

\tableofcontents

% GET DATA OUT OF BIBFILE INTO DB
\nocite{*}

\DTLforeachbibentry*{data}{%
% GET DATA OUT OF DB INTO MACROS
\DTLbibfieldlet\thisauthor{Author}%
\DTLbibfieldlet\thistitle{Title}%