0

I have altered the formatting of the @online entry for BibLaTeX.

The online entry is displaying how I would like, but it's causing the formatting of the @book entries to go wrong.

I would like for the @book entries to be formatted as

Surname, B. (2005) Book title in italics followed by full stop.
    Place of publication: Name of the publisher

Where the name format here is Surname, Initials. So Jeff Buckley would be expressed as Buckley, J. and so on.

I'm not sure why [online] url is forced onto the end of the book references, or how to remove this without messing with the formatting created for the online entries.

Following is a MWE.


Here's the main.tex file

\documentclass{book}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PACKAGES 

\usepackage[style=authoryear]{biblatex}
\usepackage[T1]{fontenc} % font encoding, copied 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BIBLIOGRAPHY INFORMATION

% \addbibresource{main.bib}
\addbibresource{refs.bib}

\NewBibliographyString{onlinesource}

\DefineBibliographyStrings{english}{%
  urlseen = {accessed on},
  urlfrom = {available from},
  onlinesource = {online},
}

\DeclareFieldFormat{urldate}{%
\mkbibbrackets{%
  \thefield{urlday}\adddot\addspace%
  \mkbibmonth{\thefield{urlmonth}}\adddot\addspace%
  \thefield{urlyear}\isdot}}

% This puts the <> braces about the url field
\DeclareFieldFormat{url}{\space\bibsentence\bibstring{urlfrom}\addcolon\space<\url{#1}>}

\newbibmacro*{url+urldate}{%
  \printtext{\bibstring[\mkbibbrackets]{onlinesource}}%
  \setunit{\addspace}%
  \iffieldundef{urlyear}
    {}
  \usebibmacro{url}%
    {\setunit*{\addspace}%
     \usebibmacro{urldate}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN DOCUMENT

\begin{document}

This citation is working correctly \autocite{ford} 

But others don't seem to work as a result of the adjustments for web
\autocite{Chomsky1957}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BIBLIOGRAPHY INCLUDE

\printbibliography
\end{document}

Here's the refs.bib file

@book{Chomsky1957,
    Address   = {The Hague},
    Author    = {A Book},
    Publisher = {Mouton},
    Title     = {Book Book Book},
    Year      = {1957}
}

@online{ford,
    author  = {Fordy, Rebecca},
    title   = {Earthquake: Twitter Users Learned},
    date    = {2011-08},
    url     = {http://www.hollywoodreporter.com/news/earthquake-twitter-users-learned-tremors-226481},
    urldate = {2012-05-16}
}

How the formatting currently looks :

enter image description here

  • 1
    Do you write your actual document in English (American, Australian or British)? Do you load babel? – moewe Sep 26 '18 at 6:49
  • Yes the document is all in English ( British ), I've not loaded babel in this example. – baxx Sep 26 '18 at 12:53
  • 1
    I can see that you don't load babel in the example. But I'd like to know if you are going to use it in your actual document. Because things might be easier for the URL date in that case. – moewe Sep 26 '18 at 12:55
  • Oh, sorry. I'm absolutely happy to use babel if it makes anything easier here. – baxx Sep 26 '18 at 13:01
1

The macro url+urldate is normally called by all entry types. So if it typesets text like "[Online]", that text is going to be shown for all entry types. If you want to suppress URLs for all entry types except @online, you could use the option url=false which would then avoid calling url+urldate for all other types and hence not typeset "[Online]". But if you still want to be able to have a URL for other entry types it is safer to redefine the url+urldate macro to add a check whether or not to print the "[Online]" string.

Additionally, there is a small syntax error in your definition of url+urldate:

  \iffieldundef{urlyear}
    {}
  \usebibmacro{url}%
    {\setunit*{\addspace}%
     \usebibmacro{urldate}}

is not a valid conditional. This causes the unwanted string "url" to appear in the @book entry. I can only assume that the \usebibmacro{url} slid between the two branches of the conditional by accident. It should either come before the block or after.

Usually there should be no space or punctuation command at the beginning or end of \DeclareFieldFormats, the \space at the beginning of \DeclareFieldFormat{url} actually causes unwanted double spaces. Punctuation between fields should be handled with \setunit and friends and not with \DeclareFieldFormat.

I strongly advise against redefining the date format with \DeclareFieldFormat{urldate}. Since you mention in the comments that your document is in British English the best solution is to use the option urldate=long and load babel with the british option.

Putting this together we end up with

\documentclass[british]{article}
\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[style=authoryear, urldate=long]{biblatex}

\NewBibliographyString{onlinesource}

\DefineBibliographyStrings{english}{%
  urlseen = {accessed on},
  urlfrom = {available from},
  onlinesource = {online},
}

\DeclareFieldFormat{urldate}{\mkbibbrackets{#1}}

\DeclareFieldFormat{url}{\bibsentence\bibstring{urlfrom}\addcolon\space<\url{#1}>}

\renewbibmacro*{url+urldate}{%
  \ifentrytype{online}
    {\bibstring[\mkbibbrackets]{onlinesource}}
    {}%
  \setunit{\addspace}%
  \usebibmacro{url}%
  \iffieldundef{urlyear}
    {}
    {\setunit*{\addspace}%
     \usebibmacro{urldate}}}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{Chomsky1957,
  address   = {The Hague},
  author    = {A Book},
  publisher = {Mouton},
  title     = {Book Book Book},
  year      = {1957}
}

@online{ford,
  author  = {Fordy, Rebecca},
  title   = {Earthquake: Twitter Users Learned},
  date    = {2011-08},
  url     = {http://www.hollywoodreporter.com/news/earthquake-twitter-users-learned-tremors-226481},
  urldate = {2012-05-16}
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
This citation is working correctly \autocite{ford} 

But others don't seem to work as a result of the adjustments for web
\autocite{Chomsky1957}

\printbibliography
\end{document}

Book, A (1957). Book Book Book. The Hague: Mouton.//Fordy, Rebecca (Aug. 2011). Earthquake: Twitter Users Learned. [Online] Available from: <http://www.hollywoodreporter.com/news/earthquake-twitter-users-learned-tremors-226481> [16th May 2012].

  • Part of the original code seems to have come from tex.stackexchange.com/q/238926/35864 and tex.stackexchange.com/q/194575/35864 – moewe Sep 26 '18 at 13:19
  • Thanks I'm reading this now - you're right that the code has ( mainly ) been lifted from various posts on this site ( and some previous answers of yours I think! ). I'm not sure whether I should have kept track and noted them in the original post or not – baxx Sep 26 '18 at 13:22
  • 1
    @baxx I mainly linked them because I found it convenient to go back to the other two posts and check up on a few things. Linking the other questions from here also makes this question visible from the older question, so maybe it can help other people with a similar problem if they see the links in the "linked" column. If your code is based on earlier answers, it might help to mention that in the question, but in this case that was not really important (but there are cases where it make much more sense, tex.meta.stackexchange.com/q/7724/35864). – moewe Sep 26 '18 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.