常规设置
在导言区(\begin{document}
前面)的最后面加入以下代码,正常情况下编译运行即可成功。
\definecolor{mycustompurple}{RGB}{154, 36, 79} % 定义自己的颜色
\usepackage[utf8]{inputenc}
% \usepackage{xcolor} % 引入颜色宏包
\usepackage{hyperref} % 引入超链接宏包
\hypersetup{
colorlinks=true, % 激活链接颜色,去掉链接边框
linkcolor=red, % 文档内部链接颜色(如图表等引用)
citecolor=green, % 文献引用链接颜色
filecolor=mycustompurple, % 文件链接颜色
urlcolor=magenta % 外部URL链接颜色
}
异常显示
如果没有正常显示或者显示成其他颜色的话,可能是你使用的文档类强制更改了相关设置,例如elsarticle
文档类中有如下代码
\AtBeginDocument{\@ifpackageloaded{hyperref}
{\def\@linkcolor{blue}
\def\@anchorcolor{blue}
\def\@citecolor{blue}
\def\@filecolor{blue}
\def\@urlcolor{blue}
\def\@menucolor{blue}
\def\@pagecolor{blue}
\begingroup
\@makeother\`%
\@makeother\=%
\edef\x{%
\edef\noexpand\x{%
\endgroup
\noexpand\toks@{%
\catcode 96=\noexpand\the\catcode`\noexpand\`\relax
\catcode 61=\noexpand\the\catcode`\noexpand\=\relax
}%
}%
\noexpand\x
}%
\x
\@makeother\`
\@makeother\=
}{}}
2023.11.8 ChatGPT-4解读如下:
在
elsarticle
文档类中,这个\AtBeginDocument
命令在文档开始时检查hyperref
宏包是否已加载。如果已加载,它将设置所有超链接的颜色为蓝色。
这段代码中的\def
命令重新定义了一系列与超链接相关的颜色设置命令(比如\@linkcolor
、\@citecolor
、\@urlcolor
等),将它们都设置为蓝色。由于这些命令是在\AtBeginDocument
中定义的,它们会在文档正文开始前生效,这意味着即使你在导言区通过\hypersetup
命令设置了不同的颜色,这些颜色也可能会被这段代码重置为蓝色。
要解决这个问题,你可以在导言区的\hypersetup
命令后面再次调用\AtBeginDocument
,以确保你的颜色设置能够覆盖elsarticle
的默认设置:
\AtBeginDocument{
\hypersetup{
linkcolor=red, % 重新设置内部链接颜色为红色
citecolor=green, % 重新设置引用链接颜色为绿色
filecolor=mycustompurple, % 重新设置文件链接颜色为品红
urlcolor=magenta % 重新设置URL链接颜色为蓝色
}
}
结果
更新 2023.12.25
ChatGPT-4:
改变 LaTeX 中引用文献时方括号
[]
的颜色相对复杂,因为标准的\cite
命令并不提供直接更改括号颜色的功能。不过,可以通过一些间接的方式来实现,比如使用natbib
宏包与xcolor
宏包的组合。以下是一个实现这一功能的方法:
首先,导入必要的宏包。
natbib
用于提供更灵活的引用命令,而xcolor
用于设置颜色。
使用\definecolor
来定义所需的颜色。
重定义natbib
的引用命令,使其在输出引用编号时使用你定义的颜色。
\documentclass{article}
\usepackage[UTF8]{ctex}%显示中文包
\usepackage[utf8]{inputenc}
\usepackage{xcolor} % 引入颜色宏包
\usepackage{hyperref} % 引入超链接宏包
\usepackage[numbers]{natbib} % 使用数字模式
% 重定义引用命令
\makeatletter
\newcommand{\coloredcite}[1]{{\color{red}\cite{#1}}}
\makeatother
\hypersetup{
colorlinks=true, % 激活链接颜色,去掉链接边框
linkcolor=red, % 文档内部链接颜色(如图表等引用)
citecolor=green, % 文献引用链接颜色
filecolor=magenta, % 文件链接颜色
urlcolor=magenta % 外部URL链接颜色
}
\begin{document}
\section*{}
\begin{thebibliography}{9}
\bibitem{latexcompanion}
Michel Goossens, Frank Mittelbach, and Alexander Samarin.
\textit{The \LaTeX\ Companion}.
Addison-Wesley, Reading, Massachusetts, 1993.
\end{thebibliography}
% 常规使用\cite
\cite{latexcompanion}
% 使用自定义的 \coloredcite
\coloredcite{latexcompanion}
\end{document}