![biblatex 条目中的逗号](https://linux22.com/image/316967/biblatex%20%E6%9D%A1%E7%9B%AE%E4%B8%AD%E7%9A%84%E9%80%97%E5%8F%B7.png)
biblatex
首先要提醒一下。这是我遇到的一个非常特殊且独特的问题,只有对和/或正则表达式非常感兴趣的少数人才会感兴趣。
从答案到使用 biblatex 放置 Jr. 和 Sr.,我使用以下代码:
\renewbibmacro{name:last-first}[4]{% put name affixes like 'Jr.' after the name
\usebibmacro{name:delim}{#3#1}%
\ifblank{#3}{#1}{#3 #1}\addcomma\addspace
#2\isdot
\ifblank{#4}{}{\addcomma\addspace #4}\isdot
}
然后从答案当缺少名字时,在 biblatex 中缩写名称,我正在使用这个代码:
\DeclareStyleSourcemap{
\maps[datatype = bibtex]{
\map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
\step[fieldsource = author,
match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
replace = {[$2, $1]}]
}
\map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
\step[fieldsource = author,
match = \regexp{(\w+)\[(.+?)\]},
replace ={$1.}]
}
\map{%% Abbreviate an entry such as [Adam John] Smith to Smith
\step[fieldsource = author,
notmatch = \regexp{\A\[(.+)\]\Z},
final]
\step[fieldsource = author,
match = \regexp{(\A|\,\s)\[(.+?)\]},
replace = {$1}]
}
}
}
这两个代码片段不能完美地协同工作,因为第一个代码片段导致第二个代码片段在括号中的名字后保留或包含逗号,如[John] Lennon
已被删除并替换为Lennon
。在下面的 MWE 中,Lennon (1974)
它打印的不是,而是Lennon, (1974)
。如果您注释掉处理姓名词缀的代码Jr.
,如,则会出现所需的输出Lennon (1974)
。所以问题是,应该如何修改代码,以便它将姓名词缀放在我想要的位置,同时在我删除括号中的名字时不添加逗号?
\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}
\renewbibmacro{name:last-first}[4]{% put name affixes like 'Jr.' after the name
\usebibmacro{name:delim}{#3#1}%
\ifblank{#3}{#1}{#3 #1}\addcomma\addspace
#2\isdot
\ifblank{#4}{}{\addcomma\addspace #4}\isdot
}
\DeclareStyleSourcemap{
\maps[datatype = bibtex]{
\map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
\step[fieldsource = author,
match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
replace = {[$2, $1]}]
}
\map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
\step[fieldsource = author,
match = \regexp{(\w+)\[(.+?)\]},
replace ={$1.}]
}
\map{%% Abbreviate an entry such as [Adam John] Smith to Smith
\step[fieldsource = author,
notmatch = \regexp{\A\[(.+)\]\Z},
final]
\step[fieldsource = author,
match = \regexp{(\A|\,\s)\[(.+?)\]},
replace = {$1}]
}
}
}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@BOOK{lennon1970,
AUTHOR = "J[ohn] Lennon",
TITLE = "My life with the Beatles",
YEAR = "1970",
SORTNAME = "John Lennon"}
@BOOK{lennon1971,
AUTHOR = "[John Lennon]",
TITLE = "Moving on",
YEAR = "1971",
SORTNAME = "John Lennon"}
@BOOK{lennon1972,
AUTHOR = "[J. John Lennon]",
TITLE = "Moving further on",
YEAR = "1972",
SORTNAME = "John Lennon"}
@BOOK{lennon1973,
AUTHOR = "John Lennon",
TITLE = "Still moving on",
YEAR = "1973",
SORTNAME = "John Lennon"}
@BOOK{lennon1974,
AUTHOR = "[John] Lennon",
TITLE = "I'm out of here",
YEAR = "1974",
SORTNAME = "John Lennon"}
@BOOK{lennon1975,
AUTHOR = "Lennon, [John]",
TITLE = "I'm out of here",
YEAR = "1975",
SORTNAME = "John Lennon"}
@BOOK{beatles1970,
AUTHOR = "John W[inston] Lennon and J[ames] Paul McCartney",
TITLE = "Let it be",
YEAR = "1970"}
@BOOK{gauch2012,
AUTHOR = "Gauch, Jr., Hugh G.",
TITLE = "Scientific method in brief",
YEAR = "2012",
LOCATION = "Cambridge",
PUBLISHER = "Cambridge University Press"}
\end{filecontents*}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
答案1
在编写名称宏时,我认为采用标准定义(来自biblatex.def
)并谨慎应用修改总是更安全。标准定义考虑了很多我可能没有考虑到的事情。
在本例中,name:last-first
仅由一个部分组成的名字(如“亚里士多德”、“柏拉图”或经过我们的处理后的“列侬”)效果不佳。在这些情况下,它仍然会添加一个逗号,以高兴地期待后面没有名字。
下面的宏只是name:last-first
将biblatex.def
几行代码重新排列了一下。这些\ifblank{#4}{}{...}
代码一直到最后。此外,还添加了一个\revsdnamepunct
,以突出 Jr 部分。(也许创建一个新的并使用它更合适\bibnameaffixpunct
)
\renewbibmacro*{name:last-first}[4]{%
\ifuseprefix
{\usebibmacro{name:delim}{#3#1}%
\usebibmacro{name:hook}{#3#1}%
\ifblank{#3}{}{%
\ifcapital
{\mkbibnameprefix{\MakeCapital{#3}}\isdot}
{\mkbibnameprefix{#3}\isdot}%
\ifpunctmark{'}{}{\bibnamedelimc}}%
\mkbibnamelast{#1}\isdot
\ifblank{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
\ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}
{\usebibmacro{name:delim}{#1}%
\usebibmacro{name:hook}{#1}%
\mkbibnamelast{#1}\isdot
\ifblank{#2#3}{}{\revsdnamepunct}%
\ifblank{#2}{}{\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
\ifblank{#3}{}{\bibnamedelimd\mkbibnameprefix{#3}\isdot}%
\ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}}
平均能量损失
\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}
\renewbibmacro*{name:last-first}[4]{%
\ifuseprefix
{\usebibmacro{name:delim}{#3#1}%
\usebibmacro{name:hook}{#3#1}%
\ifblank{#3}{}{%
\ifcapital
{\mkbibnameprefix{\MakeCapital{#3}}\isdot}
{\mkbibnameprefix{#3}\isdot}%
\ifpunctmark{'}{}{\bibnamedelimc}}%
\mkbibnamelast{#1}\isdot
\ifblank{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
\ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}
{\usebibmacro{name:delim}{#1}%
\usebibmacro{name:hook}{#1}%
\mkbibnamelast{#1}\isdot
\ifblank{#2#3}{}{\revsdnamepunct}%
\ifblank{#2}{}{\bibnamedelimd\mkbibnamefirst{#2}\isdot}%
\ifblank{#3}{}{\bibnamedelimd\mkbibnameprefix{#3}\isdot}%
\ifblank{#4}{}{\revsdnamepunct\bibnamedelimd\mkbibnameaffix{#4}\isdot}}}
\DeclareStyleSourcemap{
\maps[datatype = bibtex]{
\map{%% Make sure a field like [Adam John Smith] comes out as [Smith, Adam John]
\step[fieldsource = author,
match = \regexp{\A\[(.+)\s+([^\s]+)\]\Z},
replace = {[$2, $1]}]
}
\map{%% Abbreviate an entry such as Adam J[ohn] Smith to Smith, Adam J.
\step[fieldsource = author,
match = \regexp{(\w+)\[(.+?)\]},
replace ={$1.}]
}
\map{%% Abbreviate an entry such as [Adam John] Smith to Smith
\step[fieldsource = author,
notmatch = \regexp{\A\[(.+)\]\Z},
final]
\step[fieldsource = author,
match = \regexp{(\A|\,\s)\[(.+?)\]},
replace = {$1}]
}
}
}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@BOOK{lennon1970,
AUTHOR = "J[ohn] Lennon",
TITLE = "My life with the Beatles",
YEAR = "1970",
SORTNAME = "John Lennon"}
@BOOK{lennon1971,
AUTHOR = "[John Lennon]",
TITLE = "Moving on",
YEAR = "1971",
SORTNAME = "John Lennon"}
@BOOK{lennon1972,
AUTHOR = "[J. John Lennon]",
TITLE = "Moving further on",
YEAR = "1972",
SORTNAME = "John Lennon"}
@BOOK{lennon1973,
AUTHOR = "John Lennon",
TITLE = "Still moving on",
YEAR = "1973",
SORTNAME = "John Lennon"}
@BOOK{lennon1974,
AUTHOR = "[John] Lennon",
TITLE = "I'm out of here",
YEAR = "1974",
SORTNAME = "John Lennon"}
@BOOK{lennon1975,
AUTHOR = "Lennon, [John]",
TITLE = "I'm out of here",
YEAR = "1975",
SORTNAME = "John Lennon"}
@BOOK{beatles1970,
AUTHOR = "John W[inston] Lennon and J[ames] Paul McCartney",
TITLE = "Let it be",
YEAR = "1970"}
@BOOK{gauch2012,
AUTHOR = "Gauch, Jr., Hugh G.",
TITLE = "Scientific method in brief",
YEAR = "2012",
LOCATION = "Cambridge",
PUBLISHER = "Cambridge University Press"}
\end{filecontents*}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\printbibliography
\end{document}