I am trying to make a document to accept both bibtex and biblatex (during different compilations of course) and I have a problem because:

  1. In bibtex I have to add \url command inside every url (really many entries)
  2. But biblatex doesn't accept this (already there and so, it supposes it is a part of my url) and giving unwanted results.

Here is the code:

\documentclass{article}
\usepackage{filecontents}
% \UseBibLaTex is a variable that if is less than 1 the document 
% will have to compiled with `bibTeX`... else with biber
\def\UseBibLaTeX{0}
% ``url'' package is used in order to make LaTeX to break the urls 
% in places like the bibliography combined with the next (upcoming) 
% command
%\usepackage{breakurl}
\usepackage[hyphens]{url}
\Urlmuskip=0mu plus 1mu

\ifnum\UseBibLaTeX>0
\usepackage[backend=biber,style=authoryear]{biblatex}
\newcommand\myurl[1]{#1}
\DeclareFieldFormat{url}{{\footnotesize URL:}\space%\expandafter%\noexpand
\myurl{#1}}
\addbibresource{myBib.bib}
\else
% ``natbib'' package offers the bigger ammount possibilities in bibliography
% formating
\let\myurl\url
\usepackage{natbib}
\fi


\begin{filecontents*}{myBib.bib}
@Article{cite1,
title={One Article with a url that has to break both in bibtex and biblatex},
author={Someone NotMe and Someother Me},
year={2018},
month={10},
url={\url{http://A-long-URL-that-has-to-break-through-lines-and-biber-has-to-ignore-the-\textbackslash url-command}},
doi={http://dx.doi.org/10.1038/nphys1170}
}
\end{filecontents*}

\title{Double Functionality about Bibliography}
\author{Konstantinos Leledakis}
\date{October 2018}

\begin{document}

\maketitle

\nocite{*}
\ifnum\UseBibLaTeX>0
\printbibliography
\else
\bibliographystyle{agsm}
\begin{sloppypar}
  \bibliography{myBib}
\end{sloppypar}
\fi



\end{document}

The output of the above code, is as expected... but if I change the command \def\UseBibLaTeX{0} to \def\UseBibLaTeX{1} I get (an expected as a noob about biber but...) unwanted result:

enter image description here

Already tried some things like \noexpand or \expandafter that are commented in my code but could not make it work as I wanted.

  • 2
    As far as biblatex is concerned the url field must not contain \url or anything else other than the raw URL. I thought most .bst files (that support URLs) would handle the situation similarly. – moewe Oct 18 at 14:40
  • @moewe... I understood that, but tried to change the way that handles this field (possibly only its format and thus failed). I suppose there is a way, at least change the bibtex way (and adding this command to every url field autamaticaly) if biblatex way can not adjusted. I tried with changes in biblatex first because I thought is more powerful to handle such situations – koleygr Oct 18 at 14:46
  • I could accept a new kind of entry too that will be added there like note, but trying to avoid it. The final result I am after is just to not need to change the bibliography file from compilation to compilation... And just preffer to be able to use the already existing fields in bibliography files (with or without addition of \url command) without changes... But if not posible, I am going to add them as a note – koleygr Oct 18 at 14:55
up vote 6 down vote accepted

biblatex expects the url field to contain the URL and the URL only. No additional markup like \url{...}, no escaping of special characters, nothing, just the verbose URL.

The problem you are seeing is that Biber applies percent encoding to the URL and this messes up your \url{...} wrapper. (There are ways to get rid of the percent encoding, see biblatex, using space in url doesnt work, How to disable percent-encoding in URLs?, Scandinavian letters in Biblatex URL, but that would not really help here.)

Luckily most .bst files that support a dedicated URL field also handle URLs in a way that does not require you to add \url{...} or additional commands; like biblatex they expect the raw URL.

The exact handling of the URL with BibTeX depends on the .bst style you use. agsm uses the command \harvardurl which you can redefine. Unfortunately, there is no general consensus amongst all .bst as to how URLs are handled. Many .bst files just use \url{...} directly (notably the standard natbib styles and urlbst), a few like agsm have a dedicated command.

edit: I originally understood the use of \myurl in the MWE as an attempt to uniformly change the URL format for both biblatex and BibTeX styles (possibly independent of other \urls displayed in the document). If that was indeed the aim of \myurl, you need to know how your .bst files handles URLs and redefine the commands involved accordingly. It would be nigh-impossible to pull this off in a style-agnostic fashion. If, on the other hand, you only intended to fix the undesired output of the biblatex URLs in the MWE and you generally want the URLs to be displayed "normally", then there is no need for \myurl at all and you can probably just use the styles as they are. See the edit history for the first version of the answer.

Either way, natbib's definition of agsm's \harvardurl should probably be tweaked a bit to allow for all kinds of URLs.

\documentclass{article}
\usepackage{filecontents}
\def\UseBibLaTeX{0}
\usepackage[hyphens]{url}


\ifnum\UseBibLaTeX>0
\usepackage[backend=biber,style=authoryear]{biblatex}
  \addbibresource{\jobname.bib}
\else
  \usepackage{natbib}
  \renewcommand\harvardurl[1]{\textbf{URL:} \url{#1}}
\fi

\begin{filecontents*}{\jobname.bib}
@misc{cite1,
  title  = {One Article with a url that has to break both in bibtex and biblatex},
  author = {Someone NotMe and Someother Me},
  year   = {2018},
  url    = {http://A-long-URL-that-has-to-break-through-lines-and-biber-has-to-ignore-the-\textbackslash url-command},
}
\end{filecontents*}

\begin{document}
\nocite{*}
\ifnum\UseBibLaTeX>0
  \printbibliography
\else
  \bibliographystyle{agsm}
  \bibliography{\jobname}
\fi
\end{document}
  • Thanks, will it will work for more styles in bibtex? (I am going to accept it anyway... but would like to be a little more flexible between styles if possible) – koleygr Oct 18 at 15:02
  • @koleygr It depends on the .bst style. For starters, it won't work if the .bst file does not support a url field. The code with \harvardurl only works for .bst files that use \harvardurl. Depending on the exact code for URLs that your style may use, this would have to be tweaked slightly. Given that there is no universal standard for URLs in .bst files, that is about as general as it gets, sorry. – moewe Oct 18 at 15:06
  • Thanks... I am somehow disappointed for this... because the style was just an example and I meant to use this way to have more ways to print the bibliography and not less chances to work... Of course if url doesn't supported it will be ignored and this is not a problem since I will have decided to use the style... I have to change the code (your \let) to something supported by my specific bibtex style if any. (Remove the \DeclareFieldFormat{url} command since doesn't do something useful now . Also \myurl doensn's needed since \url would be enough... but your point is clear). Thanks again – koleygr Oct 18 at 15:27
  • @koleygr Well if you don't want to mess with the output the styles give you by default, you don't need \myurl and all the redefinitions. In that case most .bst files with URL support will just work. I thought the whole point of \myurl was to be able to manipulate the URL output uniformly for both biblatex and BibTeX? – moewe Oct 18 at 15:30
  • @koleygr I think I misunderstood your \myurl, see the updated answer, please. – moewe Oct 18 at 15:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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