更改引文中逗号的颜色

更改引文中逗号的颜色

我自定义了 biblatex 引文,使其看起来与我想要的完全一样,只是逗号的\citep颜色应该与链接相同。(逗号是否真的在超链接内或是否只是具有相同的颜色并不重要)

梅威瑟:

\documentclass{book}

\usepackage{xcolor}
\usepackage[
    style=authoryear,
    natbib=true,            
]{biblatex}

\addbibresource{Thesis.bib}

% modify \cite to add parenthesis around the year
\renewbibmacro*{cite}{%
    \iffieldundef{shorthand}{%
        \ifthenelse{%
            \ifnameundef{labelname}\OR\iffieldundef{labelyear}%
        }{%
            \usebibmacro{cite:label}%
            \setunit{\addspace}%
        }{%
            \printnames{labelname}%
            \setunit{\addspace}%
        }%
%       \ifentrytype{online}{\addspace \textendash\addspace\usebibmacro{title}}{%
            \printtext[parens]{\usebibmacro{cite:labeldate+extradate}}%
%       }%  
    }%
    {\usebibmacro{cite:shorthand}}%
}

\DeclareCiteCommand{\cite}
    {\usebibmacro{prenote}}
    {\usebibmacro{citeindex}%
        \printtext[bibhyperref]{\usebibmacro{cite}}}
    {   
%   \multicitedelim
    \textcitedelim
    }
    {\usebibmacro{postnote}}

\DeclareCiteCommand{\citep}[\mkbibparens]{%
    \usebibmacro{prenote}%
}{%
    \usebibmacro{citeindex}%
    \iffieldundef{shorthand}{%
        \ifthenelse{%
            \ifnameundef{labelname}\OR\iffieldundef{labelyear}%
        }{%
            \usebibmacro{cite:label}%
            \setunit{\addspace}%
        }{%
            \printtext[bibhyperref]{% Apply citation link to bibmacro output
            \DeclareFieldAlias{bibhyperref}{default}%
            \printnames[labelname_p]{labelname}%
            \setunit{\nameyeardelim}%
        }
        }%
        \usebibmacro{cite:labeldate+extradate}%
    }{%
        \usebibmacro{cite:shorthand}%
    }%
}{%
%   \multicitedelim%
  \ifnumequal{\value{citecount}}{\value{citetotal}}
    {\addspace\bibstring{and}\addspace}%
    {\multicitedelim}%
}{%
    \usebibmacro{postnote}%
}

\usepackage{hyperref}               
\hypersetup{
    colorlinks=true,            
    citecolor=red,
}

\begin{filecontents}{Thesis.bib}
@ARTICLE{2019arXiv190606091W,
       author = {{Wolf}, C. and {Langer}, C. and {Montes}, F. and {Pereira}, J. and
         {Ong}, W. -J. and {Poxon-Pearson}, T. and {Ahn}, S. and {Ayoub}, S. and
         {Baumann}, T. and {Bazin}, D.},
        title = "{Constraining the Neutron Star Compactness: Extraction of the $^{23}$Al($p,\gamma$) Reaction Rate for the $rp$-Process}",
      journal = {arXiv e-prints},
     keywords = {Nuclear Experiment, Astrophysics - Instrumentation and Methods for Astrophysics, Astrophysics - Solar and Stellar Astrophysics, Nuclear Theory},
         year = "2019",
        month = "Jun",
          eid = {arXiv:1906.06091},
        pages = {arXiv:1906.06091},
archivePrefix = {arXiv},
       eprint = {1906.06091},
 primaryClass = {nucl-ex},
       adsurl = {https://ui.adsabs.harvard.edu/abs/2019arXiv190606091W},
      adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}
\end{filecontents}

\begin{document}

\cite{2019arXiv190606091W}  

\citep{2019arXiv190606091W}

\citep[this text][and this text should remain black]{2019arXiv190606091W}

\printbibliography


\end{document}

在此处输入图片描述

答案1

以下代码将链接应用于由作者姓名、逗号和年份组成的整个引文标签。因此,逗号也是彩色的。我使用了biblatex-ext类似于以下的风格和代码括号内超链接引用宏以简化整个设置。

\DeclareInnerCiteDelims这个想法是通过复制原始的引用宏并将\letbibmacro其重新包装在链接中来在年份和链接周围添加括号。

请注意,\letbibmacro需要 的最新版本biblatex。(当前在 Overleaf 上运行的版本太旧,\letbibmacro并且不附带biblatex-ext。)

\documentclass{article}

\usepackage{xcolor}
\usepackage[
  style=ext-authoryear,
  natbib=true,
]{biblatex}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  citecolor=red,
}

\DeclareInnerCiteDelims{cite}{\bibopenparen}{\bibcloseparen}
\DeclareDelimFormat[cite]{nameyeardelim}{\addspace}

\DeclareFieldFormat{citehyperref}{%
  \DeclareFieldAlias{bibhyperref}{noformat}% Avoid nested links
  \bibhyperref{#1}}

\letbibmacro{saved:cite}{cite}
\renewbibmacro*{cite}{%
  \printtext[citehyperref]{%
    \usebibmacro{saved:cite}}}

\addbibresource{biblatex-examples.bib}

\begin{document}
\cite{sigfridsson}

\parencite{sigfridsson}

\parencite[this text][and this text should remain black]{sigfridsson}

\printbibliography
\end{document}

在此处输入图片描述


如果你真的只想给逗号上色,你通常会使用

\DeclareDelimFormat[cite,parencite]{nameyeardelim}{\textcolor{red}{\addcomma\space}}

但你重新定义了\citep用途\nameyeardelim而不是上下文相关的\printdelim{nameyeardelim},所以你不妨替换

\setunit{\nameyeardelim}%

\setunit{\textcolor{red}{\nameyeardelim}}%

请注意,您必须%在以下右花括号(}下一行中的)后添加一个忘记的,以避免出现不需要的空格(该空格会被 自动删除\addcomma,但不能被 删除\textcolor{red}{\addcomma})。

相关内容