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

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.