如何使用 biblatex 为参考书目中的特定条目和文本中的相应参考文献着色?

如何使用 biblatex 为参考书目中的特定条目和文本中的相应参考文献着色?

这个问题是之前问题的后续这里(与biblatex)和那里

我使用biblatex样式alphabetic并实现了对参考书目中整个参考文献进行着色(上面第一个链接中使用了数字样式)。我可以使用包中的某些选项对文本中的引用键进行着色hyperref

我想要得到的是文本中的彩色引用键和参考书目中的相同引用键,而不对整个参考文献进行着色,如上面第二个链接所示。

有没有办法让上面的第一个问题适应这种alphabetic风格?我试过了,但我对这种风格还不太熟悉biblatex

这是我的 MWE。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{filecontents}
\PassOptionsToPackage{
            natbib=true,
            backend=bibtex,      
            style=alphabetic,
            doi=false,
            maxcitenames=1, % 1 auteur max dans la citation dans le texte
            maxbibnames=1 % 1 auteur max dan la biblio
        }{biblatex}         
        \usepackage{biblatex}

\begin{filecontents}{mybib.bib}
@misc{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha}
}
@misc{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo}
}
@misc{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie}
}
@misc{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta}
}
\end{filecontents}    

\addbibresource{mybib.bib}
\renewbibmacro{in:}{}

\usepackage[usenames,dvipsnames]{xcolor}

\DeclareBibliographyCategory{important}
\addtocategory{important}{A01}
\addtocategory{important}{B02}

\DeclareFieldFormat{labelprefix}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumber}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\DeclareFieldFormat{labelnumberwidth}{%
  \ifcategory{important}
    {\textcolor{orange}{\mkbibbrackets{#1}}}
    {\mkbibbrackets{#1}}}

\AtEveryBibitem{%
  \ifcategory{important}
    {\color{orange}}
    {}}

\begin{document}
  \cite{A01}
  \cite{B02}
  \cite{C03}       
  \cite{D04}      

  \cite{A01,B02,C03,D04}

  \printbibliography
\end{document}

答案1

您几乎已经完成了。只需移除\AtEveryBibitem块并将所有出现的 更改为labelnumberlabelalpha因为您使用的不是numeric而是alphabetic样式)。您还需要为 着色extraalpha

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[usenames,dvipsnames]{xcolor}

\usepackage[backend=biber, style=alphabetic,
  maxcitenames=1, maxbibnames=1,
  doi=false,
  natbib=true,]{biblatex}


\renewbibmacro{in:}{}

\DeclareBibliographyCategory{important}
\addtocategory{important}{A01}
\addtocategory{important}{B02}

\DeclareFieldFormat{labelprefix}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\DeclareFieldFormat{labelalpha}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\DeclareFieldFormat{extraalpha}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\DeclareFieldFormat{labelalphawidth}{%
  \ifcategory{important}
    {\textcolor{orange}{\mkbibbrackets{#1}}}
    {\mkbibbrackets{#1}}}

\begin{filecontents}{\jobname.bib}
@misc{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha}
}
@misc{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo}
}
@misc{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie}
}
@misc{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta}
}
\end{filecontents}

\addbibresource{\jobname.bib}

\begin{document}
  \cite{A01}
  \cite{B02}
  \cite{C03}
  \cite{D04}

  \cite{A01,B02,C03,D04}

  \printbibliography
\end{document}

彩色引文和参考书目标签。

您应该认真考虑backend=biber,从切换到backend=bibtex,。您只能biblatex在 Biebr 后端使用 的所有功能。切换通常很简单,只需用 替换backend=bibtex,backend=biber,运行 Biber 而不是 BibTeX (Biblatex 与 Biber:配置我的编辑器以避免未定义的引用帮助您进行编辑器配置)。


cite直接修改 bibmacro 可能比更改几种不同的字段格式稍微容易一些。

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[usenames,dvipsnames]{xcolor}

\usepackage[backend=biber, style=alphabetic,
  maxcitenames=1, maxbibnames=1,
  doi=false,
  natbib=true,]{biblatex}


\renewbibmacro{in:}{}

\DeclareBibliographyCategory{important}
\addtocategory{important}{A01}
\addtocategory{important}{B02}

\DeclareFieldFormat{colourimportant}{%
  \ifcategory{important}
    {\textcolor{orange}{#1}}
    {#1}}

\renewbibmacro*{cite}{%
  \printtext[bibhyperref]{%
    \printtext[colourimportant]{%
      \printfield{labelprefix}%
      \printfield{labelalpha}%
      \printfield{extraalpha}%
      \ifbool{bbx:subentry}
        {\printfield{entrysetcount}}
        {}}}}

\DeclareFieldFormat{labelalphawidth}{%
  \ifcategory{important}
    {\textcolor{orange}{\mkbibbrackets{#1}}}
    {\mkbibbrackets{#1}}}

\begin{filecontents}{\jobname.bib}
@misc{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha}
}
@misc{B02,
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo}
}
@misc{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie}
}
@misc{D04,
  author = {Duthor, D.},
  year = {2004},
  title = {Delta}
}
\end{filecontents}

\addbibresource{\jobname.bib}

\begin{document}
  \cite{A01}
  \cite{B02}
  \cite{C03}
  \cite{D04}

  \cite{A01,B02,C03,D04}

  \printbibliography
\end{document}

相关内容