Replace "and" in a List of Authors with Comma Except the Last One

I have to format the bibliography data in many ways! One case is APA format as below!

Q1

A bibtex entry has a bibtex-author field, normally authors name are separated with and, I have to format the output

to mimic the real situation I give below an example.

\define results() $(first)$, and $(last)$
<$vars authorlist="Maarten R. Dobbelaere and Pieter P. Plehiers and Ruben Van de Vijver and Christian V. Stevens and Kevin M. Van Geem">
<$vars first={{{ [<authorlist>split[ and ]butlast[]join[, ]] }}} last={{{ [<authorlist>split[ and ]last[]] }}}>
-- <<results>>

</$vars>
</$vars>

Do you propose a simpler solution?

Do you have a link to the APA style guide?

I did find this one: Reference List: Author/Authors // Purdue Writing Lab

It says the last Author should be separated with &

Since the last element has to be treated different “search-replace” isn’t possible. … So may be there is no better way.

Maybe not simpler, but different …

<$vars authorlist="Maarten R. Dobbelaere and Pieter P. Plehiers and Ruben Van de Vijver and Christian V. Stevens and Kevin M. Van Geem"
replace1=" and "
replace2=",([^,]*)$"  
>
<$vars first={{{ [<authorlist>search-replace:g:regexp<replace1>,[, ]search-replace:g:regexp<replace2>,[, and $1]]}}} 
>
<<first>>
</$vars>
</$vars>

@pmario
I use this one https://guides.library.uq.edu.au/referencing/apa6 but there are others like
https://apastyle.apa.org/

Hi Mark,
Your solution is shorter! while uses sophisticated regexp :wink:
Small and smart!

Thank you!

That’s true, the hard part is to show the author name as surname Initials , e.g. Nguyen, T., Carnevale, J. J., Scholer, A. A., Miele, D. B., & Fujita, K.

Assume a name like Ruben Van de Vijver where the last three words are surname! :sweat_smile:
Of course some bibtex generator uses Ruben {Van de Vijver}

So I gave up! :wink:

Your solution will not look right if there is only one author, I think.

I check the number of authors! if greater than 1, then I use the solution!

\define show-authors-in-references()
<!-- display authors as comma separated list, removes "and" -->
\whitespace trim
<$vars number-authors={{{[{!!bibtex-author}split[ and ]count[]]}}}>
<$list filter="[<number-authors>compare:number:gt[1]]" emptyMessage="""<$view  field="bibtex-author"/>""" variable=null>
<$vars authorlist={{!!bibtex-author}}>
<$vars first={{{ [<authorlist>split[ and ]butlast[]join[, ]] }}} last={{{ [<authorlist>split[ and ]last[]] }}}>
<$text text={{{ [<first>addsuffix[, and ]addsuffix<last>] }}}/>
</$vars>
</$vars>
</$list>
</$vars>
\end

The regexp solution takes care of that case automatically.

Ah, I did not know this!
So, your solution is shortest in this context!

Mark what do you propose for the hard part below?

I don’t suppose we can assume no middle names?

This requires human knowledge. You would need a pre-process that analyzes standard known special forms (e.g. Van de xxxxxxx} and maybe converts them all into something like Van#de#xxxxxxx. After that you can run your process to convert first and middle names into initials. At the end you would convert “#” into spaces.

I chose “#” because I know of no name with that symbol. I did know someone with an apostrophe in her name, so punctuation can sometimes be part of names.

There are some references with no middle name!
Normally these bibtex entries are downloaded from online databases like
https://www.sciencedirect.com/science/article/abs/pii/S0197458021002955

many authors have no middle name!

I heard there were people stimulating their brains electrically with home-made equipment.

Tested and worked great!

I replaced my solution with yours and acknowledged your invaluable help in Refnotes plugin! :wink:

What do you think, if we process the {surname with space}? It seems bibtex uses {...} to indicate this is a multi words surname or a name which contains spaces!

If the source author list is already formatted for you, that should make things easier. But you’ll have to use some extra escaping in regexp because { } are used in regexp.

Thank you!
Yes, the multi words surname are wrapped into {}.
I will go through and will let you know the result!