2

This question already has an answer here:

I have a special field in my bibliography, called adsurl, and I would like to display this url in my file as a link.

However, when I try this, href throws a very weird error. It enters some sort of infinite loop where it keeps saying

! Undefined control sequence.
 \filename@simple ...#2\\}\fi \edef \filename@base 
                                              {#1}

MWE:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.bib}
@online{elk,
 author       = {Anne Elk},
 title        = {On the Theory of Brontosauruses},
 adsurl       = {https://example.edu/~elk/bronto.html},
}
\end{filecontents*}


\usepackage[hyperref,backend=biber]{biblatex}
\usepackage{hyperref}
\addbibresource{\jobname.bib}

\begin{filecontents*}{biblatex-dm.cfg}
 \DeclareDatamodelFields[type=field, datatype=literal, skipout=false]{adsurl}
 \DeclareDatamodelEntryfields{adsurl}
\end{filecontents*}

\begin{document}
 \cite{elk} %this works
 \citefield{elk}{adsurl} %this also works
 \href{\citefield{elk}{adsurl}}{ads} %this does not work
\end{document}

How can I get href to accept the output of citefield as url?

marked as duplicate by gusbrs, Phelype Oleinik, Kurt, Troy, Stefan Pinnow May 7 '18 at 11:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Does tex.stackexchange.com/q/412924/35864 help? \citefield does a bit more than just 'returning' the value of the field, so it can't be used as argument to \href. – moewe May 4 '18 at 15:42
  • More relevant: tex.stackexchange.com/q/413365/35864 – moewe May 4 '18 at 15:45
  • @moewe, wouldn't a DeclareFieldFormat with a bibhyperref directive get the job done here? – gusbrs May 4 '18 at 15:46
  • 1
    @gusbrs You mean \DeclareFieldFormat{adsurl}{\href{#1}{ads}}? That would be a viable option if the link text is fixed. The \citefield gets a bit more awkward, though, since we have to specify the format (otherwise \citefield defaults to the citefield format): \citefield{elk}[adsurl]{adsurl}. If the 'ads' text ought to be variable, one would have to find a way to inject the correct text into the format (probably via a wrapper command). In that case a new \DeclareCiteCommand with postnote seems nicer to me. – moewe May 4 '18 at 15:50
  • 1
    The question remains searchable even if closed as a duplicate. It only just occurred to me: If the adsurl is similar to the arXiv you could also check out the eprint field. – moewe May 7 '18 at 11:44
0

Thanks to the links provided by moewe, I was able to fix this. So I decided to answer this so people won't have to click through to the other questions.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{filecontents}

\begin{filecontents*}{\jobname.bib}
@online{elk,
author       = {Anne Elk},
title        = {On the Theory of Brontosauruses},
adsurl       = {https://example.edu/~elk/bronto.html},
}
\end{filecontents*}


\usepackage[hyperref,backend=biber]{biblatex}
\usepackage{hyperref}
\addbibresource{\jobname.bib}

\begin{filecontents*}{biblatex-dm.cfg}
\DeclareDatamodelFields[type=field, datatype=literal, skipout=false]{adsurl}
\DeclareDatamodelEntryfields{adsurl}
\end{filecontents*}

%Here's the trick:
\DeclareCiteCommand{\ads}
 {\boolfalse{citetracker}%
 \boolfalse{pagetracker}%
 \usebibmacro{prenote}}
 {\href{\thefield{adsurl}}{ads}}
 {\multicitedelim}
 {\usebibmacro{postnote}}

\begin{document}
 \cite{elk}
 \citefield{elk}{adsurl}
 \ads{elk}
\end{document}

For more explanations see the linked questions.

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