2

Suppose I have the following code:

\documentclass{article}

\usepackage[backend=biber,bibencoding=utf8]{biblatex}
\usepackage{filecontents}

\begin{filecontents}{test.bib}
    @online {page1,
        crossref = "mysite",
        title = "Page 1",
        url = "http://www.example.com/page1.html"
    }

    @online {page2,
        crossref = "mysite",
        title = "Page 2",
        url = "http://www.example.com/page2.html"
    }

    @online {mysite,
        title = "My Website Home",
        author = "John Doe",
        date = "2016-01-01",
        url = "http://www.example.com/",
        organization = "My Website"
    }
\end{filecontents}

\addbibresource{test.bib}

\begin{document}

\cite{mysite}, \cite{page1}, \cite{page2}.

\printbibliography

\end{document}

As you can see, I have two webpages, page1 and page2 which are crossref'd from the entry mysite. This is nice, but unfortunately it requires me to copy the URL each time I add a new page. Now, for such a trivial example, it isn't too much of a hassle, but it's a pain for large bibliographies, and can be error prone. I set about trying to figure out a way to fix this. First, I added the following mapping code to the header:

...
\DeclareDriverSourcemap{
    \map[overwrite]{
      \pertype{online}
      \step[fieldsource=webpage]
      \step[fieldset=url, origfieldval, append]
    }
}
...

Then, I modified the two entries which reference mysite like so:

...
@online {page1,
    crossref = "mysite",
    title = "Page 1",
    webpage = "page1.html"
}

@online {page2,
    crossref = "mysite",
    title = "Page 2",
    webpage = "page2.html"
}
...

Unfortunately, this doesn't work. While it does put the contents of the webpage field into the url field, it does not append the content of the former to the inherited URL. I'm at a loss as to what the matter is. Is there some way to make BibLaTeX use inherited values when doing maps?

junius
  • 2,058
  • @Mico well, even if that's true, it doesn't appear to make a difference, it still doesn't work. But I'll update the question in any case – junius Sep 20 '16 at 05:24
  • 2
    I guess that is because sourcemapping is one of the first steps Biber does when processing the .bib file. While inheritance comes later. So when your sourcemap is executed inheritance has not taken place. – moewe Sep 20 '16 at 07:32
  • 2
    Imho url should contain an url. Why don't you change the macros so that the url is printed as url+ webpage? – Ulrike Fischer Sep 20 '16 at 08:32
  • 2
    @moewe is correct - sourcemapping is the very first thing that happens, otherwise you wouldn't know what exactly you were changing. – PLK Sep 20 '16 at 12:39
  • (I agree with Ulrike here, and can't disagree with PLK or moewe; but just trying to offer another workaround.) If you want to save yourself some typing, you could do @string{mywww = "http://www.example.com"} and then url = mywww in the entries. Your sourcemapping would work fine then. – jon Sep 20 '16 at 13:39
  • @UlrikeFischer: oh, I see: so just change the \DeclareFieldFormat command? – junius Sep 20 '16 at 14:07

1 Answers1

1

Thanks to suggestions from the comments, I found a workable solution to the problem:

\documentclass{article}
\usepackage{hyperref}
\usepackage[backend=biber,bibencoding=utf8]{biblatex}
\usepackage{filecontents}

\DeclareDriverSourcemap{
    \map[overwrite]{
      \pertype{online}
      \step[fieldsource=webpage]
      \step[fieldset=verba, origfieldval]
    }
}

\DeclareFieldFormat{url}{\mkbibacro{URL}\addcolon\space\nolinkurl{#1\thefield{verba}}}


\begin{filecontents}{plainurl.bib}
    @online {page1,
        crossref = "mysite",
        title = "Page 1",
        webpage = "page1.html"
    }

    @online {page2,
        crossref = "mysite",
        title = "Page 2",
        webpage = "page2.html"
    }

    @online {mysite,
        title = "My Website Home",
        author = "John Doe",
        date = "2016-01-01",
        url = "http://www.example.com/",
        organization = "My Website"
    }
\end{filecontents}

\addbibresource{plainurl.bib}

\begin{document}

\cite{mysite}, \cite{page1}, \cite{page2}.

\printbibliography

\end{document}

Instead of trying to dynamically modify the URL field, what I've instead done is changed the DeclareFieldFormat command for the url field. Also, I couldn't get it to work with \thefield{webpage} so I first mapped the field webpage to verba and then used that. Also, it's important to note that hyperref is required for this to work, otherwise the input to the \url command will be treated like verbatim text.

junius
  • 2,058