8

I am using apacite package for my BibTex file, but according to my writing style, it's not required for url to be listed in the bibliography if doi is available. Is there any way I can forced apacite to suppress url in bibliography and display only the doi?

1

1 Answer 1

5

We can use the specific format of \bibitem produced by the apacite.bst style.

\documentclass{article}

\usepackage{filecontents}
\usepackage{apacite}
\usepackage{etoolbox}

\begin{filecontents}{\jobname.bib}
@article{aa,
  title = "DOI and URL",
  author = "Author, Author",
  year = 2000,
  journal = "Apacite Examples",
  url = "http://www.example.com/1",
  doi = "123/123"
  }
@article{bb,
  title = "Only URL",
  author = "Author, Author",
  year = 2001,
  journal = "Apacite Examples",
  url = "http://www.example.com/2"
  }
@article{cc,
  title = "No DOI no URL",
  author = "Author, Author",
  year = 2002,
  journal = "Apacite Examples" 
  }
@article{dd,
  title = "Only DOI",
  author = "Author. Author",
  year = 2004,
  journal = "Apacite Examples",
  doi = "123/124"
  }
\end{filecontents}


\newtoggle{bibdoi}
\newtoggle{biburl}
\makeatletter
\newsavebox{\bib@url}
\newsavebox{\bib@doi}

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

\newenvironment{APACrefURL}
  {\global\toggletrue{biburl}\lrbox\bib@url}
  {\endlrbox}

\newenvironment{APACrefDOI}
  {\global\toggletrue{bibdoi}\lrbox\bib@doi}
  {\endlrbox}


\newcommand{\printinfo}{
  \iftoggle{bibdoi}{\usebox{\bib@doi}}{\usebox{\bib@url}}
  \togglefalse{bibdoi}
}

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

\begin{document}

\cite{aa}
\cite{bb}
\cite{cc}
\cite{dd}


\bibliographystyle{apacite}
\bibliography{\jobname}

\end{document}

apacite uses APACrefURL and APACrefDOI environments to wrap \url and doi. Thus we can use lrbox to store such pieces of text and set toggle to identify whether they are present or not. Then, apacite inserts \PrintBackRefs at the end of each bibitem thus we can use it to hook into it the commands to print the url and doi. In this hook we do the reasoning to determine which ones has to be printed (and undo spaces).

enter image description here

3
  • 1
    This works brilliantly. However, with this solution URLs are no longer broken up into multiple lines when they are too long. Is there any way to restore the line breaking?
    – RoyalTS
    Apr 23, 2014 at 16:47
  • There's a new posting -- see tex.stackexchange.com/q/195512/5001 -- that would like to make use of your method while allowing line breaks to be inserted in url and doi strings. Pleas consider posting an addendum to your answer to show how this might be done. (It would involve not using \lrbox, right?)
    – Mico
    Aug 8, 2014 at 19:08
  • See tex.stackexchange.com/questions/195512/linebreak-in-url for a modification to the solution that allows line breaks in url and doi fields.
    – Guido
    Aug 8, 2014 at 20:54

You must log in to answer this question.

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