2

I want to do Multiple references in one superscript citation but with the options of pdf or paper layout.

When pdf is selected I want an internal link in the citation number to the reference and a link in the name of the articles to the doi or url.

When the paper option is selected the url or the doi should appear at the end of each article, but not both of them.

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\addtokomafont{disposition}{\rmfamily}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{csquotes,siunitx,fancyhdr,etoolbox}
\sisetup{per-mode = symbol}
\DeclareSIUnit\cal{cal}
\newtoggle{pdf}
\togglefalse{pdf}
\usepackage[
  refsection=section,
  style=authortitle,
  backend=biber,
  hyperref=auto
  ]{biblatex}
\addbibresource{biblatex-examples.bib}
\defbibheading{refbib}[Referencia~\therefsegment]{\subsection*{#1}}
\DeclareFieldFormat{linktitle}{%
  \ifhyperref
    {\iffieldundef{doi}
       {\iffieldundef{eprint}
           {\iffieldundef{url}
             {#1}
             {\href{\thefield{url}}{#1}}}
           {\mkhrefeprint{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {#1}}

\makeatletter
\newrobustcmd*{\mkhrefeprint}[1]{%
  \iffieldequalstr{eprinttype}{arxiv}
    {\href{http://arxiv.org/\abx@arxivpath/\thefield{eprint}}{#1}}
    {\iffieldequalstr{eprinttype}{hdl}
       {\href{http://hdl.handle.net/\thefield{eprint}}{#1}}
       {\iffieldequalstr{eprinttype}{jstor}
          {\href{http://www.jstor.org/stable/\thefield{eprint}}{#1}}
          {\iffieldequalstr{eprinttype}{pubmed}
             {\href{http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}{#1}}
             {\iffieldequalstr{eprinttype}{googlebooks}
                {\href{http://books.google.com/books?id=\thefield{eprint}}{#1}}
                {#1}}}}}}
\makeatother
\iftoggle{pdf}{
    \usepackage[unicode,colorlinks,urlcolor=blue]{hyperref}
    \newcommand{\myhref}[2]{\href{#1}{#2}} % enlace externo
    \newcommand{\mylref}[2]{\hyperref[#1]{#2}} % enlace interno
    \newcommand{\myplainurl}[1]{\ttfamily \url{#1}}
    \newcommand{\myfootnote}[1]{\footnote{#1}}
    \newcommand{\myfnhref}[2]{\href[#1]{#2}}
    \def\mycite#1{\begin{refsegment}\nocite{#1}\footnotemark[\therefsegment]\end{refsegment}}
    \renewbibmacro*{title}{%
  \ifboolexpr{
    test {\iffieldundef{title}}
    and
    test {\iffieldundef{subtitle}}
  }
    {}
    {\printtext[linktitle]{\printtext[title]{%
       \printfield[titlecase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{subtitle}}}%
     \newunit}%
  \printfield{titleaddon}}
    \renewbibmacro*{doi+eprint+url}{}
  \renewbibmacro*{eprint}{}
  \renewbibmacro*{url+urldate}{}
}{
    \usepackage[unicode,hidelinks]{hyperref}
    \newcommand{\myhref}[2]{{#2}\footnote{\url{#1}}} % enlace externo
    \newcommand{\mylref}[2]{{#2}\footnote{\ref{#1} en la página \pageref{#1}}} % enlace interno
    \newcommand{\myplainurl}[1]{\url{#1}}
    \newcommand{\myfootnote}[1]{\footnote{#1}}
    \newcommand{\myfnhref}[2]{{#2} \^{}{\{\ttfamily  \url{#1}\}}} % enlace que sólo aparece en el pie de página
    \def\mycite#1{\begin{refsegment}\nocite{#1}\textsuperscript{[\therefsegment]}\end{refsegment}}
    \renewbibmacro*{doi+eprint+url}{%
    \iftoggle{bbx:doi}
      {\printfield{doi}}
      {\newunit\newblock
       \iftoggle{bbx:eprint}
        {\usebibmacro{eprint}}
        {\newunit\newblock
         \iftoggle{bbx:url}
            {\usebibmacro{url+urldate}}
            {}}}}
}

\begin{document}
  \section*{Referencias}
  This should reference the first reference \mycite{itzhaki,markey,sigfridsson,geer}
\bibbysegment[heading=refbib]
\end{document}
  • So, are we talking two different bibliography styles -- one for the electronic version of the document (showing doi or url), the other for the print version (url only)? – Mico Mar 07 '15 at 20:39
  • In the pdf the title of the reference is hyperlinked to the doi or url and the print version just prints the url after the reference's title, page... –  Mar 07 '15 at 20:41
  • 2
    Please provide a minimal working example (MWE). Nobody wants to spend time on a solution only to find out that it doesn't work with the citation or bibliography style you're using, or conflicts with your chosen class. – cfr Mar 08 '15 at 00:49
  • Could you be a bit more precise about what you mean by "[m]ultiple references in one citation" in the title? Would biblatex's (dynamic or static) set help you there (see pp. 111-113 of the biblatex documentation)? A MWE and a more precise description of what you seek to achieve would be greatly appreciated. – moewe Mar 08 '15 at 07:22
  • http://tex.stackexchange.com/questions/181450/multiple-references-in-one-superscript-citation –  Mar 08 '15 at 12:40
  • So the "multiple" part refers to the link you just added? Will you have as many references in each "part" as in the MWE or will it be around three or four tops. In any case I presume you would be OK with the solution suggested in the question, right? What I really don't understand is what you want additionally; to me it seems to be about a PDF vs print version: Is it that you want the title to link to DOI/URL if in PDF mode and to add the ULR/DOI if in print mode? – moewe Mar 08 '15 at 12:56
  • What I can imagine is using something like this if in PDF mode (and suppressing DOI, URL etc. output maybe via \renewbibmacro*{doi+eprint+url}{} and \newbibmacro*{url+urldate}{}) and just the standard in print mode. (Come to think of it one would only need to enable printing of the URL in print mode and can leave the hyperlink active since it does not matter for printing.) – moewe Mar 08 '15 at 12:59
  • How would that be without the eprint option? –  Mar 08 '15 at 17:15
  • I'm sorry, I don't quite understand your last comment. Have you tried any of the suggestions? – moewe Mar 08 '15 at 17:58
  • The "multiple" part refers to the link I added. The number of references can vary a lot, it can be one or ten but it doesn't have to be around three or four. "I presume you would be OK with the solution suggested in the question" What question? I want the title to link to URL or DOI in that order if in PDF mode and to add the URL or DOI but not both if in print mode. I don't understand what the solution of the linked question changes from default. But in that solution the url isn't in the title. –  Mar 08 '15 at 18:08
  • When I said "I presume you would be OK with the solution suggested in the question", I was referring to the answer in Multiple references in one superscript citation, originally I had thought your question was also about how to achieve something like this and that you were not satisfied with the solutions there in principle. But from what I gather now you are only dissatisfied with the linking/URL/DOI etc. issue, right? Please have a look at my answer below and let me know how it works for you (and if not, why not). – moewe Mar 09 '15 at 08:19

1 Answers1

3

I have not copied the solution to Multiple references in one superscript citation into this answer to keep the code shorter and show that this solution is universally applicable (I have made sure, however, that this answer works smoothly together with the answer given there).

Major parts of the code in this answer is inspired by my answer to Custom url for \cite links.

In order to be able to link the titles we need a new link format

\DeclareFieldFormat{linktitle}{%
  \ifhyperref
    {\iffieldundef{doi}
       {\iffieldundef{eprint}
           {\iffieldundef{url}
             {#1}
             {\href{\thefield{url}}{#1}}}
           {\mkhrefeprint{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {#1}}

Which requires a helper function for eprint

\makeatletter
\newrobustcmd*{\mkhrefeprint}[1]{%
  \iffieldequalstr{eprinttype}{arxiv}
    {\href{http://arxiv.org/\abx@arxivpath/\thefield{eprint}}{#1}}
    {\iffieldequalstr{eprinttype}{hdl}
       {\href{http://hdl.handle.net/\thefield{eprint}}{#1}}
       {\iffieldequalstr{eprinttype}{jstor}
          {\href{http://www.jstor.org/stable/\thefield{eprint}}{#1}}
          {\iffieldequalstr{eprinttype}{pubmed}
             {\href{http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}{#1}}
             {\iffieldequalstr{eprinttype}{googlebooks}
                {\href{http://books.google.com/books?id=\thefield{eprint}}{#1}}
                {#1}}}}}}
\makeatother

Then we need to link the title field via this redefinition of biblatex.def's title macro

\renewbibmacro*{title}{%
  \ifboolexpr{
    test {\iffieldundef{title}}
    and
    test {\iffieldundef{subtitle}}
  }
    {}
    {\printtext[linktitle]{\printtext[title]{%
       \printfield[titlecase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{subtitle}}}%
     \newunit}%
  \printfield{titleaddon}}

One could also change the definition of the title field format.

To suppress all other linked output (URL, DOI etc.) in "PDF mode", but retain one of doi, eprint, url (in that order) in "print" mode we use the following logic

\iftoggle{pdf}{%
  \renewbibmacro*{doi+eprint+url}{}
  \renewbibmacro*{eprint}{}
  \renewbibmacro*{url+urldate}{}
}{
  \renewbibmacro*{doi+eprint+url}{%
    \iftoggle{bbx:doi}
      {\printfield{doi}}
      {\newunit\newblock
       \iftoggle{bbx:eprint}
        {\usebibmacro{eprint}}
        {\newunit\newblock
         \iftoggle{bbx:url}
            {\usebibmacro{url+urldate}}
            {}}}}
}

MWE

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage{filecontents}
\usepackage[style=authortitle, backend=biber]{biblatex}
\usepackage{hyperref}

\begin{filecontents*}{\jobname.bib}
@article{KingZengCounterFacts,
  author        = {Gary King and Langche Zeng},
  title         = {The Dangers of Extreme Counterfactuals},
  journaltitle  = {Political Analysis},
  volume        = {14},
  number        = {2},
  date          = {2006},
  issue         = {Spring},
  pages         = {131–159},
  doi           = {10.1093/pan/mpj004},
  url           = {http://gking.harvard.edu/files/abs/counterft-abs.shtml},
  urldate       = {2012-06-05},
}
\end{filecontents*}

\newtoggle{pdf}
\toggletrue{pdf}

\addbibresource{biblatex-examples.bib}
\addbibresource{\jobname.bib}

\DeclareFieldFormat{linktitle}{%
  \ifhyperref
    {\iffieldundef{doi}
       {\iffieldundef{eprint}
           {\iffieldundef{url}
             {#1}
             {\href{\thefield{url}}{#1}}}
           {\mkhrefeprint{#1}}}
       {\href{http://dx.doi.org/\thefield{doi}}{#1}}}
    {#1}}

\makeatletter
\newrobustcmd*{\mkhrefeprint}[1]{%
  \iffieldequalstr{eprinttype}{arxiv}
    {\href{http://arxiv.org/\abx@arxivpath/\thefield{eprint}}{#1}}
    {\iffieldequalstr{eprinttype}{hdl}
       {\href{http://hdl.handle.net/\thefield{eprint}}{#1}}
       {\iffieldequalstr{eprinttype}{jstor}
          {\href{http://www.jstor.org/stable/\thefield{eprint}}{#1}}
          {\iffieldequalstr{eprinttype}{pubmed}
             {\href{http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}{#1}}
             {\iffieldequalstr{eprinttype}{googlebooks}
                {\href{http://books.google.com/books?id=\thefield{eprint}}{#1}}
                {#1}}}}}}
\makeatother

\renewbibmacro*{title}{%
  \ifboolexpr{
    test {\iffieldundef{title}}
    and
    test {\iffieldundef{subtitle}}
  }
    {}
    {\printtext[linktitle]{\printtext[title]{%
       \printfield[titlecase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[titlecase]{subtitle}}}%
     \newunit}%
  \printfield{titleaddon}}

\iftoggle{pdf}{%
  \renewbibmacro*{doi+eprint+url}{}
  \renewbibmacro*{eprint}{}
  \renewbibmacro*{url+urldate}{}
}{
  \renewbibmacro*{doi+eprint+url}{%
    \iftoggle{bbx:doi}
      {\printfield{doi}}
      {\newunit\newblock
       \iftoggle{bbx:eprint}
        {\usebibmacro{eprint}}
        {\newunit\newblock
         \iftoggle{bbx:url}
            {\usebibmacro{url+urldate}}
            {}}}}
}

\begin{document}
  \nocite{itzhaki,markey,kastenholz,sigfridsson,geer,KingZengCounterFacts}
  \printbibliography
\end{document}

In "PDF mode"

enter image description here

In "print mode"

enter image description here

You will not that we have not suppressed the links in the titles in "print mode" you can achieve this by moving the redefinition of the title macro (the \renewbibmacro*{title}{...} bit) inside the \iftoggle{pdf}{%.

moewe
  • 175,683