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

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>
1 Like

@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!

1 Like

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.

1 Like

There are some references with no middle name!
Normally these bibtex entries are downloaded from online databases like

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!

If you can separate each author into a list, as tiddler titles, even if you do not create matching tiddlers, then store this as the list of authors. In the above examples you would initially replace “and” or “&” with a comma, then delimit on “,”. Keep that list, you can reorder with the list-links-dragable macro. Now it is trivial to give your self a macro to list the authors with the comma ", " except for the second last " & " and last “.”.

This expectation is appropriate in English for any list especially when “inline”. Such a macro can then be reused in other cases, the order and number of authors can change…

I can write the display/punctuation macro if you want, I possibly will anyway. You @Mohammad just need to turn the authors into a real list of items without English punctuation.

This follows my approach to extract reusable components from and solution to satisfy more cases. “Abstract and Instantiate”.

Pass this macro list-sentence the input-list or set <<input-list>> and it will do what you ask.

list-sentence.json (1.0 KB)

\define list-sentence(input-list and:" and ")
<$set name=input-list value="$input-list$" emptyValue=<<input-list>> >
<$set name=items filter="""[enlist<input-list>count[]]""">
<$set name=and-position filter="""[<items>subtract[1]]""">
<$list filter="[enlist<input-list>]" counter=position variable=item>
   <<item>><$text text={{{ [<position>match<and-position>then[$and$]] ~[<position>match<items>then[ ]] ~[[, ]] }}}/>
</$list>
</$set></$set></$set>
\end

<<list-sentence "a b c [[fred blogs]]">>

author-list (field) ="<$text text={{!!author-list}}/>"

;Output of list-sentence
<$macrocall $name=list-sentence input-list={{!!author-list}} and=" & "/>

;List links draggable example on author-list field.
<$macrocall $name=list-links-draggable tiddler=<<currentTiddler>> field=author-list >>

Note

  • use macrocall for any input list
  • no regex so bigger audience will understand
  • reusable to make any list read like a sentence.
  • customisable and/&
  • escape the overuse of commas in the standard.

Hi @TW_Tones

Thank you for the snippet!

The application here is to read a bibtex entry and show it in the reference list!

@Mohammad I know little about bibtex but hope you could adapt it.