Templates Pandoc模板:最后一项的不同分隔符

Templates Pandoc模板:最后一项的不同分隔符,templates,pandoc,Templates,Pandoc,我正在编写一个自定义的Pandoc模板,希望它打印一个作者列表,以便所有作者都用“,”分隔,但最后一位作者除外,他用“and”分隔。这能做到吗 假设这是我的模板: $if(title)$ $title$ $endif$ $if(author)$ $for(author)$$author$$sep$, $endfor$ $endif$ $body$ 哪些产出: My title Author 1, Author 2, Author 3 Document body 但我希望它能输出: My ti

我正在编写一个自定义的Pandoc模板,希望它打印一个作者列表,以便所有作者都用“,”分隔,但最后一位作者除外,他用“and”分隔。这能做到吗

假设这是我的模板:

$if(title)$
$title$
$endif$
$if(author)$
$for(author)$$author$$sep$, $endfor$
$endif$
$body$
哪些产出:

My title
Author 1, Author 2, Author 3
Document body
但我希望它能输出:

My title
Author 1, Author 2 and Author 3
Document body
您必须使用Pandoc模板语法中的所谓“管道”(请参阅)

我们将为数组中除最后一项外的所有项创建一个for循环(
it
被视为循环中的代词,引用
authors/allbutlast
中的值)。对于
authors
数组中的那些元素,我们将使用“,”作为分隔符,但在循环之外,我们将简单地使用“最终分隔符”(“在本例中为and”),而
authors
数组的最后一个元素(
~
表示牢不可破的空间,如果要输出到LaTeX)

编辑: 如果只有一位作者,请使用以下内容:

$if(author/allbutlast)$
$for (author/allbutlast)$
${it}$sep$, 
$endfor$
and ${author/last}
$else$
${author/last}
$endif$

你找到解决办法了吗?@cderv对不起,我没有。如果有,请发帖。如果总是有多个作者,这个解决方案效果很好。但是,如果数组只有一个项目,它将在唯一作者前面添加“and”,因为它是数组中的第一个和最后一个项目。@NickBarreto Response编辑为包含此案例。太棒了。我在玩弄一些假设,试图得到这样的解决方案,但还没有完全成功。我一有机会就会试试这个。谢谢
$if(author/allbutlast)$
$for (author/allbutlast)$
${it}$sep$, 
$endfor$
and ${author/last}
$else$
${author/last}
$endif$