Invocando funções indiretamente (usando uma string) no BibTeX

Invocando funções indiretamente (usando uma string) no BibTeX

É possível fazer chamadas indiretas de funções nos estilos BibTeX, ou seja, obter o nome da função de um campo no .bib? EXECUTEseria capaz de fazer isso, mas essas instruções não funcionam em funções (?).

Minha solução atual é buscar o nome do campo, compará-lo com uma string literal e chamar a respectiva função, se corresponder. Isso funciona, mas é horrível (provavelmente certo, já que é um .bst? :)
Por exemplo:

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
}

Responder1

Como você adivinhou, você não pode.

BibTeX, assim como TeX, salva funções em uma tabela hash. O TeX precisa \csname<control-sequence>\endcsnamebuscar a entrada da tabela hash que corresponde à string literal <control-sequence>.
BibTeX não :-)

Para ter isso, você precisaria adicionar essa funcionalidade ao próprio BibTeX (o que pode não ser a tarefa mais divertida).


Como prêmio de consolação, aqui está uma strcasefunção que pode tornar essa sua tarefa menos chata.

A sintaxe é:

{ Other cases } "{OTHERWISE}"
{ Case n } <string n>
...
{ Case 2 } <string 2>
{ Case 1 } <string 1>
<string> strcase

A função strcaseirá comparar com <string>cada um <string n>(de 1a n: esta é a notação polonesa reversa, então ela irá ler os itens de trás para frente) e, uma vez encontrada uma correspondência, o código for Case nserá executado e o restante será descartado (mesmo que várias strings correspondam ). Se nenhuma correspondência for encontrada, o Other casescódigo é executado. Você pode usar quantos{ Case n } <string n> pares quiser, mas o final { Other cases } "{OTHERWISE}"é obrigatório, para que a função saiba onde parar.

Seu caso pode ser escrito como (com uma verificação prévia de 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
}

Aqui está um exemplo compilável com o .bstcódigo:

\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}

informação relacionada