在写中文论文时,可以引用中文和英文的论文。然而,样式略有不同。例子如下:
Cite an English article (Smith et al. 2022), and cite a Chinese article (张三 等 2018).换句话说,对于多位作者的论文,et al.用于英语论文,而等用于中文论文。考虑到引文风格语言不能处理多种语言,我会向Lua求助。
一个名为test.md的Markdown文件作为一个示例:
Cite an English article [@makarchev2022], and cite a Chinese article [@luohongyun2018].然后运行以下命令:
pandoc -C -t native test.md以及主体的产出:
[ Para
[ Str "Cite"
, Space
, Str "an"
, Space
, Str "English"
, Space
, Str "article"
, Space
, Cite
[ Citation
{ citationId = "makarchev2022"
, citationPrefix = []
, citationSuffix = []
, citationMode = NormalCitation
, citationNoteNum = 1
, citationHash = 0
}
]
[ Str "(Makarchev"
, Space
, Str "et"
, Space
, Str "al."
, Space
, Str "2022)"
]
, Str ","
, Space
, Str "and"
, Space
, Str "cite"
, Space
, Str "a"
, Space
, Str "Chinese"
, Space
, Str "article"
, Space
, Cite
[ Citation
{ citationId = "luohongyun2018"
, citationPrefix = []
, citationSuffix = []
, citationMode = NormalCitation
, citationNoteNum = 2
, citationHash = 0
}
]
[ Str "(\32599\32418\20113"
, Space
, Str "et"
, Space
, Str "al."
, Space
, Str "2018)"
]
, Str "."
]因为@luohongyun2018是一个中文书目,所以我想替换它后面的最后一个英文et al.,即:
, Str "et"
, Space
, Str "al."一个中文单词等
, Str "\31561"有可能通过Lua过滤器吗?按照Lua滤波器页面中的示例,我尝试过,但没有自己完成。
如有任何建议,将不胜感激。提前谢谢。
发布于 2022-11-20 08:49:41
下面的过滤器做两件事:它检查引文是否包含汉字,如果包含,则继续替换et al.。
对汉字的测试有点脆弱;使用标准Lua库的utf8.codepoint函数可以使它更加健壮。
function Cite (cite)
return cite:walk{
Inlines = function (inlines)
local has_cjk = false
inlines:walk {
Str = function (s)
has_cjk = has_cjk or
pandoc.layout.real_length(s.text) > pandoc.text.len(s.text)
end
}
-- do nothing if this does not contain wide chars.
if not has_cjk then
return nil
end
local i = 1
local result = pandoc.Inlines{}
while i <= #inlines do
if i + 2 <= #inlines and
inlines[i].text == 'et' and
inlines[i+1].t == 'Space' and
inlines[i+2].text == 'al.' then
result:insert(pandoc.Str '等')
i = i + 3
else
result:insert(inlines[i])
i = i + 1
end
end
return result
end
}
endhttps://stackoverflow.com/questions/74489784
复制相似问题