使用 citecounter 在 biblatex 中进行排序

使用 citecounter 在 biblatex 中进行排序

我想定义一个这样的\DeclareSortingScheme函数,按照:参考文献中被引用最多的列在最上面,对引用进行排序。biblatex\printbibliographycitecounter

有办法吗?我试过的所有方法都不起作用。

答案1

那里moewe 的答案解释了一种解决问题的方法,但它需要一个额外的文件和多次运行。

基本上,计划是:编写一个单独的文件(我给它的扩展名为.smp),其中包含在打印参考书目时编写的源映射,以及当时可用的引用计数。然后在稍后的运行中读入它,并使用它来允许 biber 将该信息包含在数据中.bib,然后使用它进行排序。

这个需要多种的运行以稳定:LaTeX、biber、LaTeX、LaTeX、LaTeX、biber、LaTeX。数量如此之多是因为数据需要通过连续处理向前“传播”:进入文件.smp,返回 LaTeX,然后进入.bcfbiber 读取的文件,然后进入 biber,最后返回 LaTeX。

显然,为了正确使用样式,这需要进行各种清理。TeXperts 能否原谅我完全采用反复试验的方法,让事情正确扩展!

\documentclass{article}
\usepackage[citecounter]{biblatex}

\makeatletter
% This command (1) sets up an .smp file to receive the relevant
% sourcemaps that will be later written out, and (2) reads that
% file in if it is available so that the sourcemap directives
% are implemented. It needs to be executed **inside the preamble**
% because that is where sourcemaps must be.
\newcommand{\preparecitationsfile}{%
  \IfFileExists{\jobname.smp}
    {\@@input \jobname.smp}
    {\typeout{No sourcemap file found. Citations will not be sorted properly.}}
  \newwrite\autosourcemaps
  \immediate\openout\autosourcemaps=\jobname.smp
  \immediate\write\autosourcemaps{\writesmppreamble}%
}
% These commands are simply a convenient way to write the necessary preamble
% and ending part of the sortmaps file
\edef\writesmppreamble{%
  \string\DeclareSourcemap\string{%
    \string\maps[datatype=bibtex]\string{%}}
  }
\edef\writesmppostamble{%{{
  \string}\string}}   

% This is the command that gets executed at each bibitem, to write
% an appropriate entry into the sourcemap file. I daresay a
% person who understood expansion rather than simply banging it
% with a number of hammers until it seems to work could make
% this a lot more elegant
\newcommand{\writeautomap}[1]{%
  \def\@tempkey{#1}
  \edef\@tempcounter{\the\c@citecounter}
  \edef\@tempa{%
    \noexpand\map[overwrite=true]\string{
      \noexpand\step [fieldsource=entrykey, match=\string{\@tempkey\string}, final]%
      \noexpand\step [fieldset=usera,fieldvalue=\string{\@tempcounter\string}]\string}%
    }%
  \immediate\write\autosourcemaps{%
    \@tempa}}

% Because (I think) these commands are only defined by biblatex during the preamble
% but we use them in writeautomap, it is necessary to do this in order to prevent
% spurious errors.
\let\map\relax
\let\step\relax
\let\maps\relax

% We can automate the closing of the file, first writing the postamble
\AtEndDocument{%
  \immediate\write\autosourcemaps{\writesmppostamble}
  \immediate\closeout\autosourcemaps}

% This ensures that an appropriate mapping is written to the source file at each bibitem
\AtEveryBibitem{%
  \writeautomap{\thefield{entrykey}}
}

% And finally we need a sorting scheme which puts the number of citations first. We are
% storing them in the usera field
\DeclareSortingScheme{custom}{%
  \sort[
  direction=descending]{
    \field{usera}}
   \sort{
    \field{sortname}    
    \field{author}
    \field{editor}
    \field{translator}}
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{year}}
  }
\makeatother

\addbibresource{biblatex-examples.bib}
\preparecitationsfile
\begin{document}

\cite{worman}

\cite{worman}

\cite{worman}

\cite{worman}

\cite{worman}

\cite{cotton}

\cite{cotton}

\cite{cotton}

\cite{cotton}

\cite{aristotle:anima}

\newrefcontext[sorting=custom]
\printbibliography


\end{document}

.smp通过查看包含在参考书目生成期间写出的 sourcemapping 命令的文件,可能有助于形象化这一点:

\DeclareSourcemap{\maps[datatype=bibtex]{
\map [overwrite=true]{ \step [fieldsource=entrykey, match={aristotle:anima}, final]\step [fieldset=usera,fieldvalue={1}]}
\map [overwrite=true]{ \step [fieldsource=entrykey, match={cotton}, final]\step [fieldset=usera,fieldvalue={4}]}
\map [overwrite=true]{ \step [fieldsource=entrykey, match={worman}, final]\step [fieldset=usera,fieldvalue={5}]}
}}

答案2

目前,尽管几乎所有拼图碎片都已存在,但是还没有简单的方法可以按引用数进行排序。

biblatexcitecount提供计算每个引用出现次数并将其存储在计数器中的选项citecounter

但是,我们无法配置排序方案以按 (LaTeX) 计数器的值进行排序。这只是因为 Biber(或 BibTeX)进行排序,而后端无法访问 TeX 计数器。(请注意,这不是扩展问题,在您的情况下,计数器对不同的条目采用不同的值,因此必须由后端进行评估。)

我们必须通过辅助文件或类似工具在 LaTeX 和 Biber 之间传递可用信息。

后端本身也可以计算出现的次数,但目前没有接口可以按照排序方案访问该信息。

如果你确实需要这个,你可以向biblatex错误跟踪器提交功能请求(https://github.com/plk/biblatex/issues)。

相关内容