# biblatex edit the way pages are printed dynamically

I'm tryng to get my bibliography pages print dynamically adapted to the content of the pages field. What I'm tying to achieve is checking if there's a range of pages or just a number of pages (searching the - maybe..?) and then changing between: page 2-5 <-> 4 pages but I can't find any boolean test to check for the dash... Any help owuld be greatly appreciated!

• What exactly do you want to do? biblatex can already check if you have one page or a range of pages, and certain fields that pass through Biber can already tell you the range length. Whether or not what you want is available already depends on what exactly you are planning to do. Please show us an MWE for the use case you have in mind. – moewe Jul 12 '18 at 7:00
• The standard styles already respond dynamically to page fields and decide whether or not to write "p./page" or "pp./pages" is that what you are after? – moewe Jul 12 '18 at 7:01
• 1) Yes, thanks, requested the merge. 2) My Literature CMS only supports the pages field. No pagestotal or anything. Stupid, but I can't change that now that I got it organized there. That said, authoryear-comp style always compiles to "S. pages" (german locale). So I need to check whats in the field, either a--b or just a number of pages n. 3) Ill produce a MWE asap, but I will need some time to do so. – Max Jul 12 '18 at 8:02
• Any news here? At the moment it is not really clear to me what you want to do. The MWE (link!)/MWEB (link!) would really help me figure out what you want to do. – moewe Jul 14 '18 at 10:42
• Sorry I'm swamped at work right now. I postponed the issue since it turns out to be more of an issue of my BibManagementTool. Biblatex, as you said, dynamically adapts to the field defining pages. Also there are different fields: pages, pagestotal, pagination. However my BibManagementTool dumps everything in pages and completely ignores any format. SO right now, Im trying to fix that before I tinker areound with biblatex – Max Jul 18 '18 at 14:33

For fields marked as range fields biblatex has the special macro \rangelen{<field>} to obtain the span/length of the range. A single page returns 1, real ranges give higher numbers.

So you could check for a single page vs. a range with

\DeclareFieldFormat{pages}{%
\ifnumless{\rangelen{pages}}{2}
{single page: #1}
{page range: #1}}


which gives

For more on \rangelen see p. 239 of the biblatex documentation.

In the comments it emerged that you are looking for a way to display the total number of pages of a @book entry. The correct field for that would be pagetotal.

Apparently your literature reference software does not support that field. Step one is to complain about this deficit to the maintainers of said software. If they claim their software supports biblatex, it should be able to write pagetotal fields to the .bib file.

You should try to get that fixed in the reference manager and its .bib export and not try to implement a hack in biblatex. If you can only export to pages you can try

\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=pages, match=\regexp{\A\d+\Z}, final]
\step[fieldset=pagetotal, origfieldval]
\step[fieldset=pages, null]
}
}
}


which transfers all single-value pages fields to pagetotal as a workaround.

You could also set something up that depends on the entry type. Normally @article, @incollection, @inbook, ... will have a pages field with a page range, while @book, @collection, ... will have pagetotal.

MWE with the two suggestions commented out

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}

\iffalse
\DeclareFieldFormat{pages}{%
\ifnumless{\rangelen{pages}}{2}
{single page: #1}
{page range: #1}}
\fi

\iffalse
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=pages, match=\regexp{\A\d+\Z}, final]
\step[fieldset=pagetotal, origfieldval]
\step[fieldset=pages, null]
}
}
}
\fi

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{appleby,
author  = {Humphrey Appleby},
title   = {On the Importance of the Civil Service},
date    = {1980},
pages   = {345},
}
@article{elk,
author  = {Anne Elk},
title   = {A Theory on Brontosauruses},
journal = {Journal of Dinosaurs},
volume  = {14},
date    = {1972},
pages   = {345-346},
}
\end{filecontents}