I want to cut the URL tags defined in a Bibtex file. For example I want to crop the prefix "mailto:" so that it won't be shown. But the hyperlink still has to contain it. The following command does work in the document environment but fails for bibliography entries. Do they need a special care?

MWE:

\documentclass{scrreprt}

\usepackage{hyperref}
\usepackage{xstring}

\let\oldUrl\url

\renewcommand{\url}[1]{
    \StrBehind{#1}{:}[\tempa]
    \StrBefore{#1}{:}[\tempb]
    \IfBeginWith{\tempb}{mailto}
        {\href{#1}{\nolinkurl{\tempa}}}
        {\oldUrl{#1}}
}

\usepackage[
    backend=biber,
    style=alphabetic
]{biblatex}

\addbibresource{test.bib}

\begin{document}
\url{mailto:john@doe.com}
\nocite{*}
\printbibliography
\end{document}

Bibtex file:

@MANUAL{ABC,
    title = {Test Title},
    author = {ABC},
    url = {mailto:john@doe.com}
}

Output:

MWE

It does work for the note tag but not for the url tag, because in the note tag the url macro is used (discovered by using bibtex engine instead of the biber engine, because bibtex does not provide a url tag):

@MANUAL{ABC,
    title = {Test Title},
    author = {ABC},
    url = {mailto:john@doe.com}
    note = {\url{mailto:john@doe.com}}
}

MWE2

My first conclusion: biber does not use the url macro to create hyperrefs but then why does the else branch of the \IfBeginWith macro in the redefine of the url macro work?

up vote 2 down vote accepted

See xstring manual, 3.4 Catcode and starred macros.

You have to use the starred version of the test macro, if you want to use macros therein.

\renewcommand{\url}[1]{
    \StrBehind{#1}{:}[\tempa]
    \StrBefore{#1}{:}[\tempb]
    \IfBeginWith*{\tempb}{mailto}
        {\href{#1}{\nolinkurl{\tempa}}}
        {\oldUrl{#1}}
}
  • Very nice and very easy! It solves my problem. Thank you very much! Btw: To avoid that ugly indentation betwenn "URL" and the mail address I cropped that macro to a one-liner. – masewo Sep 24 '14 at 8:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.