Latex中改变文献、图表、外部网址等超链接的颜色命令

常规设置

在导言区(\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}

结果

在这里插入图片描述

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
LaTeX,可以使用hyperref宏包来创建参考文献超链接。你可以通过设置不同的选项来自定义超链接的样式和颜色。 首先,你需要在导言区加载hyperref宏包,可以使用以下命令: ``` \usepackage{hyperref} ``` 然后,你可以使用hypersetup命令来设置超链接的选项。比如,你可以设置超链接颜色,可以使用以下命令: ``` \hypersetup{ colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan, } ``` 上述代码,colorlinks=true表示将超链接显示为彩色,默认为红色。linkcolor表示设置内部链接的颜色,filecolor表示设置文件链接的颜色,urlcolor表示设置URL链接的颜色。 除了设置颜色,你还可以设置其他选项,比如设置PDF的标题、默认显示模式等。 在正文,你可以使用\href命令来创建超链接。比如,你可以使用以下命令创建一个指向网址超链接: ``` \href{http://www.example.com}{Click here} ``` 上述命令会在文档创建一个超链接,点击"Click here"时会跳转到"http://www.example.com"。 此外,你还可以使用\url命令来插入纯文本的URL链接,比如: ``` \url{http://www.example.com} ``` 如果你想在文档创建一个内部链接,可以使用\hyperref命令和\hypertarget命令配合使用。首先,在目标位置使用\hypertarget命令创建一个目标,然后在其他地方使用\hyperref命令创建链接。比如: ``` \hypertarget{target}{The target sentence} ... \hyperref[target]{Link to the target} ``` 上述代码,第一行创建了一个目标位置,第三行创建了一个指向该目标的链接。 综上所述,可以使用hyperref宏包在LaTeX创建参考文献超链接,通过设置选项来自定义超链接的样式和颜色,并使用\href命令和\hyperref命令来创建各种类型的链接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ocodotial

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值