编辑

编辑

我正在尝试使用以下语法定义一个新的宏:

\citeimg*{<bibkey>}

无星号的表格应该只显示

\citeauthor{<bibkey>}, \citetitle{<bibkey>}, \citeyear{<bibkey>}

这很简单(当我不是个白痴的时候)。

带星号的表格应该显示

\citeauthor{<bibkey>}, \citetitle{<bibkey>}, \citeyear{<bibkey>}

并将此文本链接到 ie 的 URL<bibkey>示意图

\href{\citefield{<bibkey>}{url}}{\citeauthor{<bibkey>}, \citetitle{<bibkey>}, \citeyear{<bibkey>}}

这显然行不通,因为生活从来就不是那么容易。

我有一个问题:

  1. 使带星号的表单链接到相关的 URL(而不是像下面示例中那样的虚拟 URL)。

梅威瑟:

\documentclass{beamer}
\usepackage{hyperref}
\usepackage{xparse}
\makeatletter
\AtBeginDocument{%
  \@ifpackageloaded{biblatex}{%
    \NewDocumentCommand\citeimg{ s m }{%
      \IfBooleanTF{#1}{%
        Starred: \href{www.example.com}{\citeauthor{#2}, \citetitle{#2}, \citeyear{#2}}%
      }{%
        Unstarred: \citeauthor{#2}, \citetitle{#2}, \citeyear{#2}%
      }%
    }%
  }{}%
}
\makeatother
\usepackage[backend=biber]{biblatex}
\bibliography{biblatex-examples}
\begin{document}
\begin{frame}
  Desired output from unstarred version:

  \citeimg{markey}

  I want the starred version to output the same text linked to URL for the relevant .bib entry.

  \citeimg*{markey}

  I understand why this links to the wrong place, but I'm not sure how best to fix it.

  I would like the starred form to be equivalent to the following:

  \href{http://tug.ctan.org/tex-archive/info/bibtex/tamethebeast/ttb_en.pdf}{\citeauthor{markey}, \citetitle{markey}, \citeyear{markey}}
\end{frame}
\end{document}

可能解释性的幻灯片

我想知道下面这样的内容或新的 bib 宏是否会有所帮助。但这似乎过于复杂,而在我看来(也许天真地)这是一个相对简单的要求。

\DeclareFieldFormat{linked}{% simplified from Audrey's answer at http://tex.stackexchange.com/a/49578/
  \iffieldundef{url}
  {#1}
  {\href{\thefield{url}}{#1}}}

答案1

使用 的一些biblatex内置技术,我们可以使代码更短一些。(我真的不喜欢 这样的构造\citeauthor{#2}, \citetitle{#2}, \citeyear{#2},当引用多部作品时,它们会出错,而且它们不能很好地处理前后注。当然,这可能不是问题,但仍然......)

首先请注意,带星号的版本\DeclareCiteCommand会产生带星号的宏。因此\DeclareCiteCommand*{\citeimg}我们定义\citeimg*

我们还定义了一个提供链接的字段格式,但这次它接受任何文本并将其链接到条目的 URL

\DeclareFieldFormat{imglink}{\href{\thefield{url}}{#1}}

稍后还会用到\printtext[imglink]{foo}

更花哨一点的是

\makeatletter
\DeclareFieldFormat{imglink}{%
  \iffieldundef{url}
    {{#1}%
     \blx@warning@noline{you want to citeimg a work without URL,
     please check the entry '\thefield{entrykey}'}}
    {\ifhyperref
      {\href{\thefield{url}}{#1}}
      {#1}}}
\makeatother

首先检查 URL,\href如果hyperref未加载则不使用。

然后打印数据的宏是

\newbibmacro*{cfr:imgcite}{%  
  \printnames{labelname}%
  \setunit{\addcomma\space}%
  \printfield[citetitle]{labeltitle}%
  \setunit{\addcomma\space}%
  \printfield{year}%
}

最后,我们可以定义最终用户引用命令

\DeclareCiteCommand*{\citeimg}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\printtext[imglink]{\usebibmacro{cfr:imgcite}}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\DeclareCiteCommand{\citeimg}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\usebibmacro{cfr:imgcite}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\makeatletter
\newrobustcmd*{\Citeimg}{%
  \@ifstar{\bibsentence\citeimg*}{\bibsentence\citeimg}}
\makeatother

平均能量损失

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{hyperref}
\addbibresource{biblatex-examples.bib}


\makeatletter
\DeclareFieldFormat{imglink}{%
  \iffieldundef{url}
    {{#1}%
     \blx@warning@noline{you want to citeimg a work without URL,
     please check the entry '\thefield{entrykey}'}}
    {\ifhyperref
      {\href{\thefield{url}}{#1}}
      {#1}}}

\newbibmacro*{cfr:imgcite}{%  
  \printnames{labelname}%
  \setunit{\addcomma\space}%
  \printfield[citetitle]{labeltitle}%
  \setunit{\addcomma\space}%
  \printfield{year}%
}

\DeclareCiteCommand*{\citeimg}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\printtext[imglink]{\usebibmacro{cfr:imgcite}}}
  {\multicitedelim}
  {\usebibmacro{postnote}}

\DeclareCiteCommand{\citeimg}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\usebibmacro{cfr:imgcite}}
  {\multicitedelim}
  {\usebibmacro{postnote}}


\newrobustcmd*{\Citeimg}{%
  \@ifstar{\bibsentence\citeimg*}{\bibsentence\citeimg}}
\makeatother

\begin{document}
  \citeimg{markey}

  \citeimg*{brandt}

  \citeimg*{markey}

  \Citeimg{markey}

  \Citeimg*{markey}
\end{document}

示例输出

答案2

好的。我想到了一个似乎可行的办法 - 也就是说,它经受住了绝对最低限度的测试,但毫无疑问,当我尝试在真实文档中使用它时,它会崩溃。

但我确信这不可能是最简单的解决方案。一定有更直接的方法来提取 URL 以用作超链接目标,不是吗?

我声明了一个新的“字段格式”,它由来自的一大堆乱七八糟的东西组成biblatex.def。这是imglink,它应该创建一个超链接,链接的文本由来自当前正在处理的 Biblatex 条目的名称-标题-年份字段组成。

    \DeclareFieldFormat{imglink}{%
      \href{#1}{%
        \printnames{labelname}, \printfield[citetitle]{labeltitle}, \thefield{year}%
      }%
    }%

然后,我又从中拼凑了一些内容来biblatex.def声明一个新的引用命令\citeurlimg

    \DeclareCiteCommand{\citeurlimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \printfield[imglink]{url}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%

我其实没有必要在这里使用所有这些花招,但我认为暂时保留前后注释的可能性不会有什么坏处。

对 进行更多的 hodging (或 podging) 和 mangling 以及 tangling 和 tangling 的东西进行biblatex.def这种定义的方式\Citeurlimg。(我没有在问题中问这个,因为我认为我只需要改为\citeauthor\Citeauthor但由于我没有在这里直接使用它,我需要一种不同的方法来处理大写版本。)

    \newrobustcmd*{\Citeurlimg}{%
      \bibsentence\citeurlimg
    }%

这意味着我可以将\citeimg*{<bibkey>}和定义为带星号的案例周围的和中的\Citeimg*{<bibkey>}简单包装器,就像在我原始的未加星号的案例代码中一样。\citeurlimg\Citeurlimg

    \NewDocumentCommand\citeimg{ s m }{%
      \IfBooleanTF{#1}{%
        \citeurlimg{#2}%
      }{%
        \citeauthor{#2}, \citetitle{#2}, \citeyear{#2}%
      }%
    }%
    \NewDocumentCommand\Citeimg{ s m }{%
      \IfBooleanTF{#1}{%
        \Citeurlimg{#2}%
      }{%
        \Citeauthor{#2}, \citetitle{#2}, \citeyear{#2}%
      }%
    }%

完整示例:

\documentclass{beamer}
\usepackage{xparse}
\makeatletter
\AtBeginDocument{%
  \@ifpackageloaded{biblatex}{%
    \DeclareFieldFormat{imglink}{%
      \href{#1}{%
        \printnames{labelname}, \printfield[citetitle]{labeltitle}, \thefield{year}%
      }%
    }%
    \DeclareCiteCommand{\citeurlimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \printfield[imglink]{url}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%
    \newrobustcmd*{\Citeurlimg}{%
      \bibsentence\citeurlimg
    }%
    \NewDocumentCommand\citeimg{ s m }{%
      \IfBooleanTF{#1}{%
        \citeurlimg{#2}%
      }{%
        \citeauthor{#2}, \citetitle{#2}, \citeyear{#2}%
      }%
    }%
    \NewDocumentCommand\Citeimg{ s m }{%
      \IfBooleanTF{#1}{%
        \Citeurlimg{#2}%
      }{%
        \Citeauthor{#2}, \citetitle{#2}, \citeyear{#2}%
      }%
    }%
  }{}%
}
\makeatother
\usepackage[backend=biber]{biblatex}
\bibliography{biblatex-examples}
\begin{document}
\begin{frame}

  \citeimg{markey}

  \citeimg*{markey}

  \Citeimg{markey}

  \Citeimg*{markey}

\end{frame}
\end{document}

编辑

为了完整起见,以下是我现在得到的信息。moewe 的回答但试图避免\def

\AtBeginDocument{%
  \@ifpackageloaded{biblatex}{%
    % moewe's answer at https://tex.stackexchange.com/a/290681/
    \@ifpackageloaded{hyperref}{%
      \DeclareFieldFormat{imglink}{%
        \iffieldundef{url}
        {%
          {#1}%
          \blx@warning@noline{citeimg cannot hyperlink entry '\thefield{entrykey}' as it lacks field url}%
        }{%
          \href{\thefield{url}}{#1}%
        }%
      }%
    }{%
      \DeclareFieldFormat{imglink}{#1}%
    }%
    \newbibmacro*{cfr:imgcite}{%
      \printnames{labelname}%
      \setunit{\addcomma\space}%
      \printfield[citetitle]{labeltitle}%
      \setunit{\addcomma\space}%
      \printfield{year}%
    }%
    \DeclareCiteCommand*{\citeimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \printtext[imglink]{\usebibmacro{cfr:imgcite}}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%
    \DeclareCiteCommand{\citeimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \usebibmacro{cfr:imgcite}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%
    \newrobustcmd*{\Citeimg}{%
      \bibsentence\@ifstar{\citeimg*}{\citeimg}%
    }%
  }{}%
}

我可能会做的是去除对 Biblatex 负载的条件依赖,并将以下内容添加到我的biblatex.cfg,我认为这会起作用:

    \@ifpackageloaded{hyperref}{%
      \DeclareFieldFormat{imglink}{%
        \iffieldundef{url}
        {%
          {#1}%
          \blx@warning@noline{citeimg cannot hyperlink entry '\thefield{entrykey}' as it lacks field url}%
        }{%
          \href{\thefield{url}}{#1}%
        }%
      }%
    }{%
      \DeclareFieldFormat{imglink}{#1}%
    }%
    \newbibmacro*{cfr:imgcite}{%
      \printnames{labelname}%
      \setunit{\addcomma\space}%
      \printfield[citetitle]{labeltitle}%
      \setunit{\addcomma\space}%
      \printfield{year}%
    }%
    \DeclareCiteCommand*{\citeimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \printtext[imglink]{\usebibmacro{cfr:imgcite}}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%
    \DeclareCiteCommand{\citeimg}{%
      \boolfalse{citetracker}%
      \boolfalse{pagetracker}%
      \usebibmacro{prenote}%
    }{%
      \usebibmacro{cfr:imgcite}%
    }{%
      \multicitedelim
    }{%
      \usebibmacro{postnote}%
    }%
    \newrobustcmd*{\Citeimg}{%
      \bibsentence\@ifstar{\citeimg*}{\citeimg}%
    }%

这实际上是存在条件依赖的唯一原因超链接正在加载。目前,它只用于 Beamer,无论如何它都会加载包。但如果我把它放在我的 中.cfg,这可能会很方便,我也需要它在通常情况下工作,当超链接未加载。

[我尝试使用 Biblatex 提供的条件,\ifhyperref但序列似乎无法被识别,所以也许我误解了它的预期用途。]

相关内容