检查 biblatex 字段是否是第一个被打印的?

检查 biblatex 字段是否是第一个被打印的?

我正在尝试写出自己的biblatex风格,并且......

  • 每个参考书目条目的第一个字段必须大写;
  • 当没有作者或编辑的时候,我必须先打印组织机构,并且必须大写;
  • 如果没有作者、编辑或者组织,则标题在前且为大写;
  • 如果有一位作者对于一个组织来说,作者在前,组织在最后。

因此,这就是我尝试做的事情:

\begin{filecontents}{\jobname.bib}
@book{withorg,
 year    =1985,
 title   ={The History and Social Influence of the Potato},
 organization = {Some Organization}}
@book{withoutorg,
 year    =1985,
 title   ={The History and Social Influence of the Potato}}
\end{filecontents}

\documentclass{article}

\usepackage[backend=biber, bibstyle=standard]{biblatex}
\addbibresource{\jobname.bib}

\DeclareFieldFormat{uppercase}{\MakeUppercase{#1}}
\DeclareListFormat{uppercase}{\MakeUppercase{#1}}

\DeclareBibliographyDriver{book}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor+others/organization}% 1.
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{maintitle+title}% 2.
  \newunit\newblock
  % The organization is always printed here,
  % but it should be printed only when it wasn't already
  % in \usebibmacro{author/editor+others/organization}
  \printlist{organization}% 3.
  \usebibmacro{finentry}}

\newbibmacro*{author/editor+others/organization}{% 1.
    \ifboolexpr{%
        test {\ifnameundef{author}}
        and
        test {\ifnameundef{editor}}
    }
    {\printlist[uppercase]{organization}}
    %\clearlist{organization}} This causes the title to be capitalized when it shouldn't.
    {\usebibmacro{author/editor+others}}}

\renewbibmacro*{title}{% 2.
    \ifboolexpr{%
        test {\iffieldundef{title}}
        and
        test {\iffieldundef{subtitle}}
    }
        {}
        {\printtext[title]{%
            \ifboolexpr{%
                test {\ifnameundef{author}}
                and
                test {\ifnameundef{editor}}
                and
                test {\iflistundef{organization}}
            }
                {\printfield[uppercase]{title}}
                {\printfield[titlecase]{title}}
            \normalfont{\setunit{\subtitlepunct}%
            \printfield[noformat]{subtitle}}}%
        \newunit}%
    \printfield{titleaddon}}

\begin{document}
  \nocite{*}
  \printbibliography
\end{document}
  1. 首先,author/editor+others/organization宏检查是否没有作者或编辑者,如果没有,则打印组织。

  2. 然后title宏检查是否没有作者、编辑或者组织,如果没有,则以大写形式打印标题。

  3. 然后打印组织,并且它总是被打印,但是只有当它之前在宏中尚未打印时我才需要它author/editor+others/organization

所以我的第一个想法是在打印组织后使用\clearlist{organization},在项目 1 的末尾。问题是项目 2 检查是否有组织来决定标题是否应该大写。所以如果我使用\clearlist它,即使组织已经打印出来也会大写。

如果我不使用,我会得到以下结果\clearlist

无明确清单

该组织在第一个条目中被印刷了两次。

如果我使用的话,我会得到以下结果\clearlist

使用 clearlist

标题在第一个条目中被大写,但实际上不应该这样。

底线是:如果我清除了之前的字段,我需要一种方法来判断标题是否是第一个打印的字段,而无需检查之前的字段是否未定义,这样我才能在它是第一个时将其设为大写。或者,如果我不清除之前的字段,我需要一种方法来判断组织是否已经打印,这样我就不会再次打印它。有什么想法吗?

(我知道我可以在必要时使用作者/编辑者字段进行组织,但我不想这样做...希望其他人会使用我的风格并且他们的参考书目已经在使用这样的组织字段,而且这会让我的自定义名称格式感到头疼...而且组织毕竟不是一个名字,所以我认为这样更好。)

答案1

您可以保存删除该organization字段,但在执行此操作之前将其保存在宏中以供比较。

\newbibmacro*{author/editor+others/organization}{% 1.
   \savelistcs*{organization}{savedorg}%
    \ifboolexpr{%
        test {\ifnameundef{author}}
        and
        test {\ifnameundef{editor}}
    }
    {\printlist[uppercase]{organization}%
     \clearlist{organization}}%
    {\usebibmacro{author/editor+others}}}

那么你不检查\iflistundef{organization}而是检查\ifcsundef{savedorg}

\renewbibmacro*{title}{% 2.
    \ifboolexpr{%
        test {\iffieldundef{title}}
        and
        test {\iffieldundef{subtitle}}
    }
        {}
        {\printtext[title]{%
            \ifboolexpr{%
                test {\ifnameundef{author}}
                and
                test {\ifnameundef{editor}}
                and
                test {\ifcsundef{savedorg}}
            }
                {\printfield[uppercase]{title}}
                {\printfield[titlecase]{title}}%
            \normalfont{\setunit{\subtitlepunct}%
            \printfield[noformat]{subtitle}}}%
        \newunit}%
    \printfield{titleaddon}}

请注意,uppercase列表格式可能最好为

\DeclareListFormat{uppercase}{%
  \usebibmacro{list:delim}{#1}%
  \MakeUppercase{#1}\isdot
  \usebibmacro{list:andothers}}

平均能量损失

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{withorg,
 year    =1985,
 title   ={The History and Social Influence of the Potato},
 organization = {Some Organization and Another One}}
@book{withoutorg,
 year    =1985,
 title   ={The History and Social Influence of the Potato}}
\end{filecontents}
\addbibresource{\jobname.bib}

\DeclareFieldFormat{uppercase}{\MakeUppercase{#1}}
\DeclareListFormat{uppercase}{%
  \usebibmacro{list:delim}{#1}%
  \MakeUppercase{#1}\isdot
  \usebibmacro{list:andothers}}

\DeclareBibliographyDriver{book}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor+others/organization}% 1.
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{maintitle+title}% 2.
  \newunit\newblock
  % The organization is always printed here,
  % but it should be printed only when it wasn't already
  % in \usebibmacro{author/editor+others/organization}
  \printlist{organization}% 3.
  \usebibmacro{finentry}}

\newbibmacro*{author/editor+others/organization}{% 1.
   \savelistcs*{organization}{savedorg}%
    \ifboolexpr{%
        test {\ifnameundef{author}}
        and
        test {\ifnameundef{editor}}
    }
    {\printlist[uppercase]{organization}%
     \clearlist{organization}}%
    {\usebibmacro{author/editor+others}}}

\renewbibmacro*{title}{% 2.
    \ifboolexpr{%
        test {\iffieldundef{title}}
        and
        test {\iffieldundef{subtitle}}
    }
        {}
        {\printtext[title]{%
            \ifboolexpr{%
                test {\ifnameundef{author}}
                and
                test {\ifnameundef{editor}}
                and
                test {\ifcsundef{savedorg}}
            }
                {\printfield[uppercase]{title}}
                {\printfield[titlecase]{title}}%
            \normalfont{\setunit{\subtitlepunct}%
            \printfield[noformat]{subtitle}}}%
        \newunit}%
    \printfield{titleaddon}}

\begin{document}
  \nocite{*}
  \printbibliography
\end{document}

相关内容