考虑以下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
对于页码的定义相当宽松,允许除整数之外的字符串作为页码传递(例如122a
,iv
),但此解决方案仅适用\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}