使用 Hyperref 引用时使用不同颜色

使用 Hyperref 引用时使用不同颜色

我想用蓝色突出显示一些引文,而用黑色突出显示其他引文。我通过定义以下命令实现了此目的:

\newcommand\mycite[1]{\hypersetup{citecolor=blue}\cite{#1}\hypersetup{citecolor=black}

它按预期工作,但如果我有多个引用,其中一个应该标记而其他的不应该,这没有帮助。

这是一个最小的例子

\documentclass{article}

\usepackage[colorlinks,citecolor=black]{hyperref}
\newcommand\mycite[1]{\hypersetup{citecolor=blue}\cite{#1}\hypersetup{citecolor=black}}

\begin{document}
This is OK: \mycite{one} \cite{two}

But I have to chose between \mycite{one,two} or \cite{one,two}

\bibliographystyle{apalike}
\bibliography{biblio}
\end{document}

的内容biblio.bib为:

@misc{one,
  author = "Author one",
  title = "The reference to this should be in blue",
  howpublished = "Not at all",
  year = "2022"
}

@misc{two,
  author = "Author two",
  title = "The reference to this should be in black",
  howpublished = "Not at all",
  year = "2022"
}

这是我得到的。在第二种情况下,我希望有一个 2022 为蓝色,两个 2022 为黑色。

在此处输入图片描述

答案1

\documentclass{article}
\usepackage[colorlinks,citecolor=black]{hyperref}
\makeatletter
\def\bibcite#1#2{%
    \@newl@bel{b}{#1\@extra@binfo}{%
      \hyper@@link[cite\ifcsname citecontext-#1\endcsname \csname citecontext-#1\endcsname\fi]{}{cite.#1\@extra@b@citeb}{#2}%
    }%
  }%

\newcommand\@citespecialcolor{blue}
\makeatother  
  
 
\begin{document}
Give one a specialcolor

\makeatletter
\@namedef{citecontext-one}{special}
\makeatother
\cite{one,two}

Revert again
\makeatletter
\@namedef{citecontext-one}{}
\makeatother
\cite{one,two}

\bibliographystyle{apalike}
\bibliography{biblio}
\end{document}

在此处输入图片描述

答案2

您可以加载纳特比布引文管理包以及标准和宏的定义\bluecitep和变体。\bluecitealp\citep\citealp

在此处输入图片描述

\documentclass{article}

\begin{filecontents}[overwrite]{biblio.bib}
@misc{one,
  author = "Anna One",
  title = "Thoughts",
  howpublished = "Not at all",
  year = "2022"
}
@misc{two,
  author = "Albert Two",
  title = "Further thoughts",
  howpublished = "Not at all",
  year = "2022"
}
\end{filecontents}

\usepackage[colorlinks,citecolor=black]{hyperref}
\usepackage[round]{natbib}
\bibliographystyle{apalike}
\newcommand\bluecitep[1]{\hypersetup{citecolor=blue}\citep{#1}\hypersetup{citecolor=black}}
\newcommand\bluecitealp[1]{\hypersetup{citecolor=blue}\citealp{#1}\hypersetup{citecolor=black}}

\begin{document}
\setlength\parindent{0pt} % just for this example

This is OK: \bluecitep{one} \citep{two}

Either \bluecitep{one,two} or \citep{one,two}

You \emph{can} have both: (\bluecitealp{one}; \citealp{two})

\bibliography{biblio}
\end{document}

相关内容