我正在写论文,我的大学提供了一个简洁的模板,我目前正在使用。在模板的自定义选项中,还有一种特定的颜色,用于标题、说明等。
在起草过程中的某个时候,我觉得能够以该颜色而不是暗淡的黑色看到引文会很好,所以我开始摆弄这个hyperref
包:
\definecolor{bluepoli}{cmyk}{0.4,0.1,0,0.4} % University colour definition for context
\usepackage[colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=bluepoli,%
filecolor=black, menucolor=black, runcolor=black, urlcolor=black]{hyperref}
但结果却不能让我满意,事实上看起来更糟,因为只有方括号内的数字是有颜色的(我使用了biber
一种numeric-comp
样式)。
然后我查看了一下该biblatex
软件包,看看它是否提供了任何颜色选项,但我仍然不满意。
我可以放弃这个想法,但我无法理解这样一个事实:在我看来,即使是使用彩色链接的书籍和论文也存在同样的问题。我考虑过在 使用
之前和之后更改文本颜色,但我对 LaTeX 底层的机制不是很熟悉。经过快速搜索,我找到了这个答案:\cite{}
\renewcommand{}{}
renewcommand 引用一个或两个参数,
现在我的代码如下所示:
\newcommand{\blup}[1]{\textcolor{bluepoli}{#1}} % Quick colour-setting command
\makeatletter
\let\@citexOld\@citex
\def\@citex[#1]#2{\blup{\@citexOld[#1]{#2}}}
\makeatother
\let\citeOld\cite % renew cite
\renewcommand{\cite}[1]{{\blup{\citeOld{#1}}}}
\let\citetOld\citet % renew cite
\renewcommand{\citet}[1]{{\blup{\citetOld{#1}}}}
虽然这可行,但对于我可能需要的每个引用命令都这样做感觉很笨拙。我也没有意识到我定义的内容也会改变明确作者引用的颜色。 有没有更简洁的方法,只给方括号和引文上色?从排版角度来看,这真的是异端吗?
注意:是的,我知道这\citet{}
是来自的命令natbib
,它只使用bibtex
。最初模板使用了这个包,但在越来越沮丧之后,我改用biber
编辑(1):我在这里提供了源代码的最小可重现示例。
\documentclass[12pt, twoside]{book}
\usepackage{color}
\usepackage[autostyle,italian=guillemets]{csquotes}
\usepackage[natbib=true,style=numeric-comp,sorting=none,backend=biber]{biblatex}
\usepackage[colorlinks=true,citecolor=bluepoli]{hyperref}
\begin{filecontents}{test.bib}
@article{knuth74,
author = {Knuth, Donald E.},
title = {Computer Programming As an Art},
journal = {Commun. ACM},
year = {1974},
pages = {667--673},
}
@book{peterson93,
author = {Peter Peterson},
title = {The title of the work},
publisher = {The name of the publisher},
year = {1993},
}
\end{filecontents}
\addbibresource{test.bib}
\definecolor{bluepoli}{cmyk}{0.4,0.1,0,0.4} % Define colour of citations and
\newcommand{\blup}[1]{\textcolor{bluepoli}{#1}} % its shortcut
\makeatletter % Redefine the macro of citex
\let\@citexOld\@citex
\def\@citex[#1]#2{\blup{\@citexOld[#1]{#2}}}
\makeatother
\let\citeOld\cite % Renew cite
\renewcommand{\cite}[1]{{\blup{\citeOld{#1}}}}
\let\citetOld\citet % Renew citet
\renewcommand{\citet}[1]{{\blup{\citetOld{#1}}}}
\begin{document}
\section*{Section}
This is a test~\cite{knuth74}.\\
\citet{peterson93} also reports a test.
\printbibliography
\end{document}
编辑 (2):感谢 @Accácio 深入分析了解决方案参考文档。我决定稍微整理一下这个问题。
包扩展biblatex-ext
有一个专门用于更改分隔符样式(在本例中为颜色)的功能。
需要在第一个选项中更改引用样式biblatex
:
\usepackage[style=ext-numeric-comp,sorting=none,backend=biber]{biblatex}
% the style is changed to 'ext-numeric-comp'
\DeclareOuterCiteDelims{cite} % for the cite command
{\textcolor{bluepoli}{\bibopenbracket}} % left bracket
{\textcolor{bluepoli}{\bibclosebracket}} % right bracket
目前我还没有找到一种方法来改变所有引用命令的括号,部分原因是我寻求的结果在某些情况下涉及外部分隔符,在其他情况下涉及内部分隔符,例如作者引用
\DeclareInnerCiteDelims{textcite} % for the textcite command
{\textcolor{bluepoli}{\bibopenbracket}} % left INNER bracket
{\textcolor{bluepoli}{\bibclosebracket}} % right INNER bracket
答案1
我们可以使用类似的解决方案https://stackoverflow.com/a/69727169/9781176通过使用biblatex-ext和\DeclareOuterCiteDelims
函数:
\documentclass[12pt, twoside]{book}
\usepackage{color}
\usepackage[colorlinks=true,citecolor=bluepoli]{hyperref}
\usepackage[autostyle,italian=guillemets]{csquotes}
\usepackage[natbib=true,style=ext-numeric-comp,sorting=none,backend=biber]{biblatex}
\DeclareOuterCiteDelims{cite}{\textcolor{green}{\bibopenbracket}}{\textcolor{red}{\bibclosebracket}}
\begin{filecontents}{test.bib}
@article{knuth74,
author = {Knuth, Donald E.},
title = {Computer Programming As an Art},
journal = {Commun. ACM},
year = {1974},
pages = {667--673},
}
@book{peterson93,
author = {Peter Peterson},
title = {The title of the work},
publisher = {The name of the publisher},
year = {1993},
}
\end{filecontents}
\addbibresource{test.bib}
\definecolor{bluepoli}{cmyk}{0.4,0.1,0,0.4} % Define colour of citations and
\newcommand{\blup}[1]{\textcolor{bluepoli}{#1}} % its shortcut
\makeatletter % Redefine the macro of citex
\begin{document}
\section*{Section}
This is a test~\cite{knuth74}.\\
\citep{peterson93} also reports a test.
\printbibliography
\end{document}
导致
注意:引用格式改为 ext-numeric-comp
答案2
您可以使用的包装器参数\DeclareCiteCommand
插入另一个着色命令。我们从样式中复制了相关引用命令的定义(在本例中numeric-comp.cbx
,第 209-300 页)并添加\textcolor{bluepoli}
(可能通过辅助命令)到包装器参数。
请注意,这只会为引文添加更多颜色。它不会改变引文的链接区域。
\documentclass[italian]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[autostyle,italian=guillemets]{csquotes}
\usepackage[
backend=biber,
style=numeric-comp,
sorting=none,
natbib=true,
]{biblatex}
\usepackage{color}
\usepackage[colorlinks=true, citecolor=bluepoli]{hyperref}
\definecolor{bluepoli}{cmyk}{0.4,0.1,0,0.4}
\newcommand*{\mkbibcolbrackets}[1]{%
\textcolor{bluepoli}{\mkbibbrackets{#1}}}
\DeclareCiteCommand{\cite}[\mkbibcolbrackets]
{\usebibmacro{cite:init}%
\usebibmacro{prenote}}
{\usebibmacro{citeindex}%
\usebibmacro{cite:comp}}
{}
{\usebibmacro{cite:dump}%
\usebibmacro{postnote}}
\DeclareCiteCommand{\parencite}[\mkbibcolbrackets]
{\usebibmacro{cite:init}%
\usebibmacro{prenote}}
{\usebibmacro{citeindex}%
\usebibmacro{cite:comp}}
{}
{\usebibmacro{cite:dump}%
\usebibmacro{postnote}}
\makeatletter
\DeclareCiteCommand{\cbx@textcite}[\textcolor{bluepoli}]
{\usebibmacro{cite:init}}
{\usebibmacro{citeindex}%
\usebibmacro{textcite}}
{}
{\usebibmacro{cite:dump}%
\usebibmacro{postnote}%
\ifbool{cbx:parens}
{\bibclosebracket\global\boolfalse{cbx:parens}}
{}}
\makeatother
\addbibresource{biblatex-examples.bib}
\begin{document}
Lorem \autocite{sigfridsson}
ipsum \textcite{sigfridsson}
\printbibliography
\end{document}