# Linebreak in url

I'm using the apacite package and I'm trying in bibtex to supress the url if the doi is available. I found an answer in the posting apacite: how to suppress url if doi is avalable?

It is exacty what I was looking for, but for some reason the hyperref package is no longer of any help with the url's linebreaking as it used to (before I tried the answer). Can anyone help me fix this?

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[spanish]{babel}
\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%% The solution I'm using

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

\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}}
\togglefalse{bibdoi}\togglefalse{biburl}%
}{}{}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

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


And in tesis.bib I have

@Article{allali,
Title                    = {{Linear} {Algebra} and Image Processing.},
Author                   = {Allali, Mohamed},
Journal                  = {International Journal of Mathematical Education in Science and Technology},
Year                     = {2010},
}

@InProceedings{Alves,
Title                    = {Articulation problems between different systems of symbolic representations in {Linear} {Algebra}},
Author                   = {M. {Alves D{\'i}as} and M. Artigue},
Booktitle                = {Proceeding of the 19th {International} {Conference} for the {Psychology} of {Mathematics} {Education}},
Year                     = {1995},
Url                      = {http://files.eric.ed.gov/fulltext/ED411135.pdf}
}

@Article{Aydin2,
Title                    = {The factors effecting teaching {Linear} {Algebra} },
Author                   = {Sinan Ayd{\i}n},
Journal                  = {Procedia - Social and Behavioral Sciences },
Year                     = {2009},
Note                     = {World Conference on Educational Sciences: New Trends and Issues in Educational Sciences },
Number                   = {1},
Pages                    = {1549--1553},
Volume                   = {1},

Doi                      = {10.1016/j.sbspro.2009.01.272},
Url                      = {http://www.sciencedirect.com/science/article/pii/S1877042809002754}
}

• Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Aug 8 '14 at 18:44
• The reason the method you've linked to is not related directly to hyperref. Instead, the line breaking isn't done because the contents of the url and doi field are placed in an LR box (which does not allow linebreaks).
– Mico
Aug 8 '14 at 18:50
• @mico Is there a way to modify the solution in order to get the correct line breakings? Aug 8 '14 at 18:58
• @pablos - The answer is, fortunately, yes. :-) I've pinged the author of the earlier answer and asked that he provide an alternative solution that restores the line breaking capabilities of the url and hyperref packages.
– Mico
Aug 8 '14 at 19:09

The solution in apacite: how to suppress url if doi is avalable? can be modified as follows (it requires the environ package).

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

The difference is that we use the facilities provided by environ (i.e., \Collect@Body) to store the content on the two environments used by apacite to wrap the content of the doi and url fields.
• 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. Dec 20 '18 at 14:36