43

I'm trying to include a link to some code on OneDrive; the link includes an underscore, which is causing a lot of trouble.

I tried adding the entry like this:

@misc{code,
    title = {Code},
    url = {https://1drv.ms/link_link}
}

I use \usepackage{url} and \bibliographystyle{IEEEtran}. Everything compiles fine and the bibliography shows the correct entry. However, if you click the link, it opens a page at https://1drv.ms/link - so ignoring the underscore and everything after it, which leads to a wrong page. Even copy and paste from the PDF doesn't work since the underscore is copied as space, which again is a broken link.

How can I add the link such that it can be clicked and copied properly? I tried \_, \usepackage{underscore}, {{link}}, different bibtex entries but nothing worked. Also all the related questions/answers don't answer mine.

6
  • 6
    You need hyperref to get correct links. And if you are not using biblatex, then you will have to add the \url command to the bib. Commented Jul 28, 2017 at 11:42
  • 3
    \usepackage{hyperref} fixed it! However, I did not need \url even using bibtex; it seems like the url field in misc directly expects a URL. Commented Jul 28, 2017 at 12:11
  • 2
    It depends on the style. It could be that you have one that add the \url command. Commented Jul 28, 2017 at 12:17
  • 2
    Without a minimal example it is difficult to say, but I would guess that you are missing \usepackage[T1]{fontenc}. Commented Jul 28, 2017 at 12:26
  • For more details of the above discussions one may refer to the following site http://www.tex.ac.uk/FAQ-citeURL.html
    – venrey
    Commented Dec 20, 2017 at 5:45

5 Answers 5

37

In case of IEEE templates, when using the file bib, replacing the underscore with {\_} should solve the problem. In the case of the question:

@misc{code,
    title = {Code},
    url = {https://1drv.ms/link{\_}link}
}
1
  • 8
    Sometimes {\_} is converted by some latex preprocessing so it will not converted properly. In these cases, try just to replace _ by \_ in the bibtex.
    – Agile Bean
    Commented Nov 1, 2020 at 7:22
34

Just to formally answer and close this question: As Ulrike Fischer answered in the comments, the solution was to use \usepackage{hyperref} (or \usepackage[hidelinks]{hyperref} to avoid the ugly boxes) and \usepackage[T1]{fontenc}. This allows properly clicking and copy-&-pasting URLs with underscores.

However, it turned out, using package hyperref wasn't allowed when trying to submit the paper to IEEE. So I ended up simply changing the URL to something without underscore using goo.gl (or bitly since goo.gl is down).

MWE

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{hyperref}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.bib}
    @misc{code,
        title = {Code},
        url = {https://1drv.ms/link_link}
    }
\end{filecontents*}

\begin{document}

\cite{code}

\bibliographystyle{IEEEtran}
\bibliography{\jobname}

\end{document}

Result

enter image description here

2
  • 5
    This /has/ to be the hackiest solution ever, and I love it! Commented May 22, 2019 at 14:39
  • 1
    The bitly hint is amazing. Thanks!
    – Hannah H.
    Commented Nov 29, 2021 at 21:37
5

I do not know why, but the other solutions did not work for me, this did however:

  1. Delete auxiliary files (.bbl)

  2. In the bib file, replace the url by:

    url = {{https://doi.org.123345\_6789}}

Note, the double curly braces and non arround \_.

2
  • Deleting the .bbl file is always a good idea when you have a problem with the bibliography. But I'm sceptical about the second point. The double braces should normally not be any better than single braces. It will depend on your bibliography style, but usually styles that support a dedicated url field can also interface with the url or hyperref package so that they can deal with unescaped _s when you load \usepackage{hyperref} or \usepackage{url}. If at all possible I'd always prefer url = {https://doi.org.123345_6789} over escaping with backslashes.
    – moewe
    Commented Feb 22, 2021 at 17:05
  • Sane story, only this works for me.
    – Angus
    Commented Aug 3, 2022 at 9:01
3

I stumbled upon this solution from bibtex.com, which is not covered in other answers:

% if the DOI contains an "_" it must be escaped by "\{_}"
doi = "10.1057/9780230375048\{_}3"

It is for the doi field but maybe it is applicable to others as well.

2
  • In an another answer that is already written ...
    – Mensch
    Commented Jun 18, 2021 at 11:07
  • 2
    @Mensch I only saw {\_} in another answer and as a tex non-expert, I didn't know this would be transferable in this way and I would also appreciate the explicit mentioning of this slightly different solution, so I posted it.
    – xuiqzy
    Commented Jun 18, 2021 at 14:00
0

I'd recommend using sourcemaps in your Latex-File. That way you don't need to adapt the bib file or anything, For example, to escape an underscore (symbol 5F in unicode), one can use the following:

\DeclareSourcemap{
    \maps[datatype=bibtex, overwrite]{
        \map{
            \step[fieldsource=url, match=\regexp{\x{5F}}, replace=\regexp{\\\x{5F}}]
        }
    }
}

You must log in to answer this question.

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