在 biblatex 中格式化页码

在 biblatex 中格式化页码

考虑以下biblatex/biber最小示例:

在此处输入图片描述

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{references.bib}
@article{abc,
  author  = {A Author},
  title   = {The title},
  journal = {Journal of Journals},
  volume  = {12},
  number  = {34},
  year    = {1234},
  pages   = {12345-23456}
}
\end{filecontents*}

\usepackage{biblatex,siunitx}

\addbibresource{references.bib}

\begin{document}

\nocite{*}

\printbibliography

Current page number format: 12345--23456

Ideal page number format: \num{12345}--\num{23456}

\end{document}

我想使用以下方式格式化页码siunitx命令\num,产生如下范围:

在此处输入图片描述

我怎样才能做到这一点?

答案1

我们可以修改命令\mknormrange以将所有页码包装在siunitx命令中\num。然后我们只需确保\mknormrange也应用于pages字段格式(它通常不应用于该字段,因为 Biber 已经进行了足够的预处理)。

biblatex对于页码的定义相当宽松,允许除整数之外的字符串作为页码传递(例如122aiv),但此解决方案仅适用\num于通过更严格测试的字符串\ifinteger。此处修改的宏的原始定义可以在中找到biblatex.sty。本质上,我们只是\ifinteger在每次调用时添加测试\blx@range@out@value,并在其为真时将页码包装在其中\num。例如,的原始定义\blx@normrange@process

\def\blx@normrange@process#1#2{%
  \blx@range@out@value{#1\bibrangedash#2}}

我们把它读成

\def\blx@normrange@process#1#2{%
  \ifinteger{#1}
    {\ifinteger{#2}}
    {\@secondoftwo}
  {\blx@range@out@value{\num{#1}\bibrangedash\num{#2}}}
  {\blx@range@out@value{#1\bibrangedash#2}}}

总共

\documentclass{article}

\usepackage{biblatex,siunitx}

\DeclareFieldFormat{pages}{\mkpageprefix[pagination][\mknormrange]{#1}}

\makeatletter
\def\blx@normrange@split#1{%
  \def\blx@normrange@abort{%
    \ifinteger{#1}
      {\blx@range@out@value{\num{#1}}}
      {\blx@range@out@value{#1}}}%
  \blx@imc@ifpages{#1}
    {\blx@normrange@range#1\bibrangedash\bibrangedash&}
    {\blx@normrange@abort}}

\def\blx@normrange@range#1\bibrangedash#2\bibrangedash#3&{%
  \ifblank{#3}
    {\blx@normrange@hyphen#1--&}
    {\ifblank{#2}
       {\ifinteger{#1}
          {\blx@range@out@value{\num{#1}\bibrangedash}}
          {\blx@range@out@value{#1\bibrangedash}}}
       {\ifblank{#1}
          {\ifinteger{#2}
             {\blx@range@out@value{\bibrangedash\num{#2}}}
             {\blx@range@out@value{\bibrangedash#2}}}
          {\blx@range@process{#1}{#2}}}}}

\def\blx@normrange@hyphen#1-#2-#3&{%
  \ifblank{#3}
    {\blx@normrange@abort}
    {\ifblank{#2}
       {\ifblank{#1}
          {\let\blx@tempb\@empty}
          {\def\blx@tempb{#1}}%
        \blx@normrange@hyphen@i#3&}
       {\ifblank{#1}
          {\ifinteger{#2}
             {\blx@range@out@value{\bibrangedash\num{#2}}}
             {\blx@range@out@value{\bibrangedash#2}}}
          {\blx@range@process{#1}{#2}}}}}

\def\blx@normrange@hyphen@i#1-#2&{%
  \def\blx@range@out@value@numdash{\blx@range@out@value{\num{#1}\bibrangedash}}%
  \ifblank{#1#2}
    {\expandafter\ifinteger\expandafter{\blx@tempb}
       {\expandafter\blx@range@out@value@numdash
        \expandafter{\blx@tempb}}
       {\expandafter\blx@range@out@value
        \expandafter{\blx@tempb\bibrangedash}}}
    {\notblank{#1}
       {\ifdefempty\blx@tempb
          {\ifinteger{#1}
             {\blx@range@out@value{\bibrangedash\num{#1}}}
             {\blx@range@out@value{\bibrangedash#1}}}
          {\expandafter\blx@range@process
           \expandafter{\blx@tempb}{#1}}}
       {\blx@normrange@hyphen@i#2&}}}

\def\blx@normrange@process#1#2{%
  \ifinteger{#1}
    {\ifinteger{#2}}
    {\@secondoftwo}
  {\blx@range@out@value{\num{#1}\bibrangedash\num{#2}}}
  {\blx@range@out@value{#1\bibrangedash#2}}}
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@article{abc,
  author  = {A Author},
  title   = {The title},
  journal = {Journal of Journals},
  volume  = {12},
  number  = {34},
  year    = {1234},
  pages   = {12345-23456},
}
\end{filecontents*}
\addbibresource{\jobname.bib}

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

Current page number format: 12345--23456

Ideal page number format: \num{12345}--\num{23456}
\end{document}

参考书目中的页码显示为 <code>\num</code> 格式:百位和千位之间有一个小空格。

相关内容