0

I'm using apacite package, and I've been trying to suppress the url when the doi is available, following the solution provided by Linebreak in url:

\documentclass[11pt,a4paper]{article}

\usepackage{hyperref} 
\usepackage{apacite}
\usepackage{etoolbox}
\usepackage{environ}

\newtoggle{bibdoi}
\newtoggle{biburl}
\makeatletter

\undef{\APACrefURL}
\undef{\endAPACrefURL}
\undef{\APACrefDOI}
\undef{\endAPACrefDOI}

\long\def\collect@url#1{\global\def\bib@url{#1}}
\long\def\collect@doi#1{\global\def\bib@doi{#1}}
\newenvironment{APACrefURL}{\global\toggletrue{biburl}\Collect@Body\collect@url}{\unskip\unskip}
\newenvironment{APACrefDOI}{\global\toggletrue{bibdoi}\Collect@Body\collect@doi}{}

\AtBeginEnvironment{thebibliography}{
 \pretocmd{\PrintBackRefs}{%
  \iftoggle{bibdoi}
    {\iftoggle{biburl}{\unskip\unskip doi:\bib@doi}{}}
    {\iftoggle{biburl}{Retrieved from\bib@url}{}}
  \togglefalse{bibdoi}\togglefalse{biburl}%
  }{}{}
}

\begin{document}

\nocite{*}
\bibliographystyle{apacite}
\bibliography{references}
\end{document}

In the references.bib have the following citation:

@misc{american_cancer_society_melanoma_2018,
    title = {Melanoma {skin} {cancer}},
    url = {https://www.cancer.org/cancer/melanoma-skin-cancer},
    urldate = {2018-02-20},
    author = {{American Cancer Society}},
    year = {2018}
}

I'm facing the same problem pointed out at the end of the thread, which was not answered:

The solution does not seem to work well if the entry urldate (or lastchecked) is defined (and the DOI is not). The reference would show up as Retrieved from [2018-12-20]http://www.example.com

So after using the code provided, I would come to this:

reference

Is there a way to suppress or reformat the urldate?

0

1 Answer 1

0

Yes, @pablos' and @Guido's solutions don't account for the optional urldate. To make it work, insert this (the original definition of the APACrefURL environment in apacite.sty):

\newcommand{\APAurl}[1][]{%
  \ifx\@empty#1\@empty
    \BRetrievedFrom%  Retrieved from
  \else
    \BRetrieved{#1}%  Retrieved <date>, from
  \fi}%

and replace this line:

{\iftoggle{biburl}{Retrieved from\bib@url}{}}

with this:

{\iftoggle{biburl}{\expandafter\APAurl\bib@url}{}}

I also spotted a bug in the previous solutions, which resulted in not printing the DOI if no URL is available (weird, right?). For this, you simply need to replace this line:

{\iftoggle{biburl}{\unskip\unskip doi:\bib@doi}{}}

with this:

{\unskip\unskip doi:\bib@doi}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .