2

I want to streamline my bibliography by removing the protocol information (http:// and https://) from the printed version of the URL without breaking the hyperrefs. My solution at the moment looks like this

\documentclass{article}

\begin{filecontents}{bibliography.bib}
@misc{key1,
author="Google",
title="Google",
url="https://google.com",
}
@misc{key2,
author="Microsoft",
title="Bing",
url="http://bing.com",
}
@misc{key3,
author="DuckDuckGo",
title="DuckDuckGo",
url="https://duckduckgo.com",
}
\end{filecontents}

\usepackage{hyperref}
\usepackage{biblatex}

\DeclareSourcemap{%
  \maps[datatype=bibtex,overwrite=true]{%
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=userd, origfieldval,final=true]
      \step[fieldsource=userd, match=\regexp{\A(ht|f)tp(s)?:\/\/([^/]+)},replace=\regexp{$3}]
    }
  }%
}

\DeclareFieldFormat{url}{\mkbibacro{URL}\addcolon\space\href{#1}{\url{\thefield{userd}}}}

\bibliography{bibliography}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

But I am sure that there must be a simpler solution, especially the Sourcemap seems to me like overkill considering this problem. At the very least I have not come up with the simplest regular expression.

2

I think your approach is very sensible, but I would change three things

  1. Use a verbatim field (verba) and not a literal field (userd) for the URL path. This becomes relevant if the URL contains dangerous characters like # or %. Literal fields can not deal with those characters unless they are escaped.

  2. You can make the RegEx slightly shorter by simply replacing the matched scheme with nothing instead of capturing the remaining path.

  3. Use \nolinkurl inside \href: \href already provides the link.

MWE

\documentclass{article}

\usepackage{biblatex}
\usepackage{hyperref}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=verba, origfieldval, final=true]
      \step[fieldsource=verba, match=\regexp{\A(ht|f)tp(s)?:\/\/}, replace={}]
    }
  }
}

\DeclareFieldFormat{url}{%
  \mkbibacro{URL}\addcolon\space
  \href{#1}{\nolinkurl{\thefield{verba}}}}

\begin{filecontents}{\jobname.bib}
@misc{key1,
  author = {Google},
  title  = {Google},
  url    = {https://google.com},
}
@misc{key2,
  author = {Microsoft},
  title  = {Bing},
  url    = {http://bing.com},
}
@misc{key3,
  author = {DuckDuckGo},
  title  = {DuckDuckGo},
  url    = {https://duckduckgo.com/_^a#?\u&6%k},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

DuckDuckGo. DuckDuckGo. url: duckduckgo.com/_^a#?\u&6%k.//Google. Google. url: google.com.//Microsoft. Bing. url: bing.com.


I think it is best to leave string manipulation to Biber (some string manipulation is certainly possible in TeX, but it is usually a bit more painful than with Biber), so if you want to retain the information about the protocol (http or https) and don't just want to link all URLs with a hard-coded http or https, you need to pass on two separate fields in the .bbl. The other option would be to save the protocol scheme and path separately, but then you would have to piece things together to get a working link, which seems like more work. So I think your approach is quite straightforward.


It is probably overkill in this situation, but usually I find it nicer to use a newly declared field instead of the generic placeholder verba. So here is the same solution with a dedicated protocollessurl field.

\documentclass{article}

\begin{filecontents}{protocollessurl.dbx}
\DeclareDatamodelFields[type=field, datatype=uri]{protocollessurl}

\DeclareDatamodelEntryfields{protocollessurl}
\end{filecontents}

\usepackage[datamodel=protocollessurl]{biblatex}
\usepackage{hyperref}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url, final=true]
      \step[fieldset=protocollessurl, origfieldval, final=true]
      \step[fieldsource=protocollessurl, match=\regexp{\A(ht|f)tp(s)?:\/\/}, replace={}]
    }
  }
}

\DeclareFieldFormat{url}{%
  \mkbibacro{URL}\addcolon\space
  \href{#1}{\nolinkurl{\thefield{protocollessurl}}}}

\begin{filecontents}{\jobname.bib}
@misc{key1,
  author = {Google},
  title  = {Google},
  url    = {https://google.com},
}
@misc{key2,
  author = {Microsoft},
  title  = {Bing},
  url    = {http://bing.com},
}
@misc{key3,
  author = {DuckDuckGo},
  title  = {DuckDuckGo},
  url    = {https://duckduckgo.com/_^a#?\u&6%k},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\nocite{*}

\begin{document}
\printbibliography
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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