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

6

我正在编写一个自定义的 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

你找到解决方案了吗? - cderv
@cderv 抱歉,我没有。如果您有,请发布。 - Fredrik Savje
2个回答

7

您需要使用Pandoc模板语法中的所谓“管道”(请参见https://pandoc.org/MANUAL.html)。

我们将为数组中除最后一个项之外的所有项创建一个循环(it在循环内被视为代词,指代来自authors/allbutlast的值)。对于我们authors数组的这些元素,我们将使用", "作为分隔符,但是在循环外面,我们将简单地放置“最终分隔符”(在本例中为“and”)和authors数组的最后一个元素(如果您要输出到LaTeX,则~表示不可打破的空格)。

$for(authors/allbutlast)$$it$$sep$, $endfor$ and~$authors/last$

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

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

如果总是有多个作者,这个解决方案效果很好。但是,如果数组只有一个项目,则会在该唯一作者前面添加“and”,因为它既是数组中的第一个又是最后一个项目。 - Nick Barreto
1
@NickBarreto的回答已经编辑,包括了这个案例。 - Jan Netík
太棒了。我一直在玩弄一些if语句,试图得到这样的解决方案,但还没有完全掌握。一旦有机会,我会尝试这个解决方案。谢谢! - Nick Barreto
经过编辑的答案对我来说不起作用(pandoc 3.1.6)。即使只有一个作者,起始条件仍然被评估为真,并且我会得到两次相同的姓名,中间带有“和”。 - frabjous

0
我稍微改了一下Jan的回答以使它在我的用例中能正常工作(一个Quarto模板)。为什么我需要by-author?不确定,但这是我正在更改的Quarto模板所使用的。
    $if(by-author/allbutlast)$
    $for(by-author/allbutlast)$
    {\large{$by-author.name.literal$}}$sep$, 
    $endfor$
    $for(by-author/last)$
    {and \large{$by-author.name.literal$}}
    $endfor$
    $else$
    ${by-author/last}
    $endif$

这是我的完整用例。我知道这比要求的要多得多,但如果您最终看到了这个答案并正在制作pandoc模板,这可能会有所帮助。从完整的用例中我学到了很多。我正在为Quarto书籍设计一个标题页模板。
YAML _quarto.yml
project:
  type: book

book:
  title: The title
  subtitle: The subtitle
  author:
    - name: Jane Doe
    - name: Eva Nováková
    - name: Matti Meikäläinen
  chapters:
    - index.qmd
    - chap1.qmd
    - chap2.qmd
    - references.qmd

bibliography: references.bib

format:
  pdf:
    documentclass: scrbook
    template-partials: ["partials/before-body.tex"]
    toc: true
    include-in-header: 
      text: |
        \newcommand*{\plogo}{\fbox{$\mathcal{PL}$}} % logo
        \usepackage[utf8]{inputenc} % international characters
        \usepackage[T1]{fontenc} % international characters
        \usepackage{hyphenat} % don't hyphenate the title
        \usepackage{authblk}

partials/before-body.tex

\begin{frontmatter} % why not titlepage? dunno, titlepage threw errors.

    \raggedleft % Right align the title page
    
    \rule{1pt}{\textheight} % Vertical line
    \hspace{0.05\textwidth} % Whitespace between the vertical line and title page text
    \parbox[b]{0.85\textwidth}{ % Paragraph box for holding the title page
    
        {\large\bfseries\nohyphens{$title$}}\\[2\baselineskip] % Title
        $if(subtitle)$
        {\large\textit{$subtitle$}}\\[4\baselineskip] % Subtitle
        $endif$
        
        $if(by-author/allbutlast)$
        $for(by-author/allbutlast)$
        {\large{$by-author.name.literal$}}$sep$, 
        $endfor$
        $for(by-author/last)$
        {and \large{$by-author.name.literal$}}
        $endfor$
        $else$
        ${by-author/last}
        $endif$
        
        \vspace{0.5\textheight} % Whitespace
        
        {\noindent The Publisher~~\plogo}\\[\baselineskip] % Publisher+logo
    }

\end{frontmatter}

title page


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接