具有超链接功能的自定义作者(年份、页数)引文

具有超链接功能的自定义作者(年份、页数)引文

我正在尝试创建一个自定义引文命令,如果定义了 URL 字段,则在超链接时显示为作者(年份,页数)。但是,我遇到了一个问题,即引文内的任何条件逻辑都将打印在引文之后,即页码将始终放在年份括号之后,尽管在其中调用了页码。

这是我当前的最小示例:

\documentclass{article}
\usepackage[backref=true, backend=biber, style=authoryear, giveninits=true, uniquename=false]{biblatex}
\usepackage{hyperref}
\usepackage{xstring} % For string comparison/checks

% prints page numbers
\newcommand{\printpages}[1]{%
  \ifblank{#1}
    {} % Return blank if input is empty
    {\StrCount{#1}{-}[\rangecount]% Count dashes to infer if it's a range
     \ifthenelse{\rangecount > 0}
       {pp.~#1} % Multiple pages
       {p.~#1}% Single page
    }%
}

% cite command
\DeclareCiteCommand{\citeurlya}
  {\usebibmacro{prenote}}
  {%
    \printnames{labelname}%
    \setunit{\addspace}%
    \iffieldundef{url}
      {\printtext[parens]{%
         \printfield{year}%
         \printpages{#1}%
       }}
      {\href{\thefield{url}}{%
         \printtext[parens]{%
           \printfield{year}%
           \printpages{#1}%
         }}%
      }%
  }
  {\multicitedelim}
  {\usebibmacro{postnote}}

\begin{filecontents}[overwrite]{\jobname.bib}
@article{example2021,
  author    = {Author, A.},
  title     = {An Example Article},
  journal   = {Journal of Examples},
  year      = {2021},
  url       = {https://example.com/article},
}

@article{example2022,
  author    = {Buthor, B.},
  title     = {An Example Article},
  journal   = {Journal of Examples},
  year      = {2021},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
See \citeurlya[123]{example2021} for a specific detail and \citeurlya[124--125]{example2021} for an extended discussion. \citeurlya[3, 6]{example2022} is also good.
\printbibliography

\end{document} ```

the output is: See Author (2021), p. 123 for a specific detail and Author (2021), pp. 124–
125 for an extended discussion. Buthor (2021), pp. 3, 6 is also good

答案1

设法解决了。我需要在 postnote 宏后关闭括号。这些命令将打印 (作者、年份、页数) 和作者 (年份、页数)。如果提供,将作者超链接到文章,并将年份超链接到参考书目条目:

% Helper command to decide between "p." and "pp."
\newbibmacro{print:pages}[1]{%
  \ifblank{#1}
    {} % Return blank if input is empty
    {\StrCount{#1}{-}[\rangecount]% Count dashes to infer if it's a range
     \ifthenelse{\rangecount > 0}
       {, pp.~#1} % Multiple pages
       {, p.~#1}% Single page
    }%
}

% Command for (authors year) format
\DeclareCiteCommand{\citeurl}[\mkbibparens]
  {\usebibmacro{prenote}}
  {%
    \iffieldundef{url}
      {\printnames{labelname}} % Just print the names if no URL is available
      {\href{\thefield{url}}{\printnames{labelname}}} % Make the names a hyperlink
    \addcomma\addspace
      \usebibmacro{cite:labeldate+extradate}%
    \usebibmacro{print:pages}{#1}% Print pages if provided
  }
  {\multicitedelim}
  {\usebibmacro{postnote}}

% Command for Author (year) format
\DeclareCiteCommand{\citeurltext}
  {\usebibmacro{prenote}}
  {%
    \iffieldundef{url}
      {\printnames{labelname}} % Just print the names if no URL is available
      {\href{\thefield{url}}{\printnames{labelname}}} % Make the names a hyperlink
    \addspace
     \bibopenparen\usebibmacro{cite:labeldate+extradate}\usebibmacro{print:pages}{#1}%
  }
  {\multicitedelim}
  {\usebibmacro{postnote}\bibcloseparen}

相关内容