
是否可以以 BibTeX 樣式進行間接函數調用,即從 中的欄位取得函數名稱.bib
?EXECUTE
可以做到這一點,但這些語句在函數中不起作用(?)。
我當前的解決方法是獲取字段名稱,將其與字串文字進行比較,如果匹配則調用相應的函數。這可行,但很糟糕(所以可能是對的,因為它是一個.bst
?:)
例如:
FUNCTION {format.thesis.type}
{ this.to.prev.status
this.status.std
type empty$
{
% Leave warnings to callers
""
}
{
type "bthesis" =
{
bbl.bthesis
}
{
type "mthesis" =
{
bbl.mthesis
}
{
type "phdthesis" =
{
bbl.phdthesis
}
{
type "Unknown thesis type " swap$ * ", printing it verbatim" * warning$
type
}
if$
}
if$
}
if$
}
if$
cap.status.std
}
答案1
正如您所猜測的,您不能。
BibTeX 與 TeX 相同,將函數保存在雜湊表中。 TeX 必須\csname<control-sequence>\endcsname
取得與字串文字相對應的雜湊表條目<control-sequence>
。
BibTeX 沒有:-)
為此,您需要將該功能新增至 BibTeX 本身(這可能不是最有趣的任務)。
作為安慰獎,這裡有一個strcase
功能可以讓您的任務不再那麼無聊。
語法是:
{ Other cases } "{OTHERWISE}"
{ Case n } <string n>
...
{ Case 2 } <string 2>
{ Case 1 } <string 1>
<string> strcase
該函數strcase
將比較<string>
每個<string n>
(from 1
to n
:這是反向波蘭表示法,因此它將向後讀取項目),一旦找到匹配項,Case n
就會執行for 的代碼,並丟棄其餘的代碼(即使多個字符串匹配)。如果未找到匹配項,則Other cases
執行代碼。您可以{ Case n } <string n>
根據需要使用任意數量的對,但最後一個{ Other cases } "{OTHERWISE}"
是強制性的,以便函數知道在哪裡停止。
您的案例可以寫為(事先檢查type missing$
:
FUNCTION {format.thesis.type}
{ this.to.prev.status
this.status.std
type missing$
{
% Do something when type is not given
}
{
{ "Unknown thesis type " type ", printing it verbatim" * * warning$ }
"{OTHERWISE}"
{ bbl.bthesis } "bthesis"
{ bbl.mthesis } "mthesis"
{ bbl.phdthesis } "phdthesis"
type strcase
}
if$
cap.status.std
}
這是一個帶有程式碼的可編譯範例.bst
:
\begin{filecontents*}{test.bst}
ENTRY { type name } { } { }
INTEGERS { strcase.next } % the code requires an integer
STRINGS { s t } % and two strings
FUNCTION { not }
{ { #0 }
{ #1 }
if$
}
FUNCTION { pop.next } { swap$ pop$ }
FUNCTION { strcase.end }
{ #0 'strcase.next :=
#1
swap$
'skip$
if$
}
FUNCTION { strcase }
{ #1 'strcase.next :=
{ strcase.next }
{ 's :=
't :=
s t =
{ { swap$ "{OTHERWISE}" = not }
{ pop.next }
while$
pop.next
strcase.end
}
{ "{OTHERWISE}" t =
{ strcase.end }
{ pop$ s }
if$
}
if$
}
while$
}
FUNCTION { thesis }
{
type missing$
{ "Type missing for entry " cite$ * warning$ }
{
{ "Unknown thesis type " type ", printing it verbatim" * * warning$ }
"{OTHERWISE}"
{ "This is a bachelor thesis" warning$ } "bthesis"
{ "This is a master thesis" warning$ } "mthesis"
{ "This is a doctor thesis" warning$ } "phdthesis"
type strcase
}
if$
}
READ
ITERATE {call.type$}
\end{filecontents*}
\begin{filecontents*}{test.bib}
@thesis{bach, type = {bthesis}}
@thesis{mast, type = {mthesis}}
@thesis{doct, type = {phdthesis}}
@thesis{unkn, type = {unknown}}
@thesis{void, name = {me}}
\end{filecontents*}
\documentclass{article}
\begin{document}
\nocite*
\bibliography{test}
\bibliographystyle{test}
\end{document}