# Alter one BibLaTeX citation style without messing up the rest

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

\NewBibliographyString{onlinesource}

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

\DeclareFieldFormat{urldate}{%
\mkbibbrackets{%
\thefield{urlyear}\isdot}}

% This puts the <> braces about the url field

\newbibmacro*{url+urldate}{%
\printtext{\bibstring[\mkbibbrackets]{onlinesource}}%
\iffieldundef{urlyear}
{}
\usebibmacro{url}%
\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,
Author    = {A Book},
Publisher = {Mouton},
Title     = {Book Book Book},
Year      = {1957}
}

@online{ford,
author  = {Fordy, Rebecca},
title   = {Earthquake: Twitter Users Learned},
date    = {2011-08},
urldate = {2012-05-16}
}


How the formatting currently looks :

• 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
• 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

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}%
\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}}

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

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

@online{ford,
author  = {Fordy, Rebecca},
title   = {Earthquake: Twitter Users Learned},
date    = {2011-08},
urldate = {2012-05-16}
}
\end{filecontents}

\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}


• 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
• @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