# Automatically replace “{\_}” with “_” in URL strings in bibliographic references?

When I export from Mendeley to Sharelatex, some references contain url fields like this:

@misc{15MPEDIA2014,
title = {{Lista de contactos de los nodos de la Plataforma DRY - 15Mpedia}},
year = {2014},
author = {{15MPEDIA}},
url = {https://15mpedia.org/wiki/Lista{\_}de{\_}contactos{\_}de{\_}los{\_}nodos{\_}de{\_}la{\_}Plataforma{\_}DRY}
}


If you see the url, instead of having _ it has {\_} which obviously breaks the link. Maybe it is a problem of Sharelatex. They told me they don't know what's happening... :-(

So, in this case, with this kind of 'urls', is there any way to correct these incorrect bib entries in order to have links working properly? For example, coding to change always {\_} to _ inside url = ... strings.

MWE:

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass{DissertateB5}
\captionsetup{labelfont=\rmdefault, textfont=\rmdefault }
\usepackage[natbibapa]{apacite}
\bibliographystyle{apacite}

\begin{document}

This is the reference \cite{15MPEDIA2014}

\end{document}


• Try simply _, without braces and backslash. – egreg Dec 9 '16 at 20:30
• But the question is: how to make this change automatically for a lot of entries, which I am constantly updating from Mendeley, not one to one? – Juan Linares Dec 9 '16 at 20:50
• It's a Mendeley problem, so off topic for the site, I'm afraid. – egreg Dec 9 '16 at 20:51
• I take it you're using XeLaTeX; is switching to LuaLaTeX an option for you? – Mico Dec 9 '16 at 20:56
• With LuaLatex the compiler is having troubles and no 'pdf' is done. A lot of errors – Juan Linares Dec 9 '16 at 21:02

Here's a LuaLaTeX-based solution. It defines a Lua function that scans all input lines -- including those in the file \jobname.bbl, which is created by BibTeX -- and looks for \url{...} strings. For every such string, all instances of {\_} within the string are replaced with _. This happens at a very early stage of processing, i.e., before LaTeX starts its usual operations.

The only input requirement is that the string containing a URL, along with its encasing macro "\url{...}, be on one line. No linebreaks allowed.

A separate comment: You should really try to figure out how to set all Mendeley parameters. It shouldn't be that difficult to instruct Mendeley not to encase all instances of _ in a URL with {\_}.

\RequirePackage{filecontents}
\begin{filecontents}{mybib.bib}
@misc{15MPEDIA2014,
title = {{Lista de contactos de los nodos de la Plataforma DRY--\mbox{15Mpedia}}},
year = {2014},
author = {{15MPEDIA}},
url = {https://15mpedia.org/wiki/Lista{\_}de{\_}contactos{\_}de{\_}los{\_}nodos{\_}de{\_}la{\_}Plataforma{\_}DRY}
}
\end{filecontents}

\documentclass{article}
%%% Lua-related code begins here
\usepackage{luacode}
\begin{luacode}
function clean_underscore ( s )
return ( string.gsub ( s , "{\\_}" , "_" ) )
end
function clean_url ( s )
return ( string.gsub ( s , "\\url%b{}" , clean_underscore ) )
end
\end{luacode}
"process_input_buffer", clean_url, "clean_url" )}}
%%% Lua-related code ends here

\usepackage{natbib}
\bibliographystyle{plainnat}

\usepackage{url}
\usepackage[colorlinks,urlcolor=blue]{hyperref} % just for this example

\begin{document}
\nocite{*}
\bibliography{mybib}
\end{document}


Addendum: I just noticed that you load the apacite citation management package (with the option natbibapa) along with the apacite bibliography style. Using these pieces of information, the formatted bib entry looks like this -- note that the Lua function continues to get the job done:

Just disable Escape LaTeX special characters in your Mendeley preferences.

Disadvantage: will fail if e.g. the title contains special characters like &.

My personal opinion: Don't use such automatically generated bib files, they are far from perfect. Instead take the entries and copy them to your own bib file. There you can clean up.

Some flaws from your automatic bib file:

• Alexa is repeated two times in its entry
• in dataanlysis15m: two times 2014
• should DENZIN be in caps?
• +1. I knew it shouldn't be that difficult to instruct Mendeley to do the right thing... – Mico Dec 9 '16 at 22:04
• About DENZIN`, this is other issue, I don't know wich one is better: all references names in caps or not..and how to change all of them – Juan Linares Dec 10 '16 at 14:48
• @JuanLinares A matter of taste, but I would not do it as I find it uncomfortable to read caps. – samcarter_is_at_topanswers.xyz Dec 10 '16 at 15:47