[Tex/LaTex] Automatically replace “{\_}” with “_” in URL strings in bibliographic references

bibliographiesurls

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}

enter image description here

Best Answer

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

enter image description here

\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}
\AtBeginDocument{\luadirect{luatexbase.add_to_callback ( 
    "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:

enter image description here

Related Question