[英]Pandoc template: different separator for last item
我正在编写一个自定义 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
但我想要它 output:
My title
Author 1, Author 2 and Author 3
Document body
您必须使用 Pandoc 模板语法中的所谓“管道”(请参阅https://pandoc.org/MANUAL.html )。
我们将为数组中的 all-but-last 项创建一个 for 循环( 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$
我稍微更改了 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 书制作扉页模板。
_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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.