无需修改 .tex 文件即可更改 hyperref 的链接颜色

无需修改 .tex 文件即可更改 hyperref 的链接颜色

为了编辑目的,我需要编译一个 .tex 文件来更改超链接的颜色。例如,如果我有:

\documentclass[11pt]{article}
\usepackage{amsmath}
\pagestyle{empty}
\usepackage{graphicx} %% This package is already called in the manuscript preamble
\usepackage{xcolor} %% This package could be already called in the manuscript preamble
\usepackage{hyperref} %% This package is already called in the manuscript preamble
\begin{document}

As in eq. \eqref{eq1}
\begin{equation}
 \label{eq1}
x+y=z
\end{equation}

As in figure \ref{fig1}
\begin{figure}
  \centering
  \includegraphics[width=.2\textwidth]{example-image-a}
  \caption{Caption.\label{fig1}}
\end{figure}

\end{document}

我需要按原样进行编译:

\documentclass[11pt]{article}
\usepackage{amsmath}
\pagestyle{empty}
\usepackage{graphicx} %% This package is already called in the manuscript preamble
\usepackage{xcolor} %% This package could be already called in the manuscript preamble
\usepackage{hyperref} %% This package is already called in the manuscript preamble

\makeatletter
\def\p@figure{\color{red}}
\def\p@equation{\color{green}}
\makeatother

\begin{document}

As in eq. \eqref{eq1}
\begin{equation}
 \label{eq1}
x+y=z
\end{equation}

As in figure \ref{fig1}
\begin{figure}
  \centering
  \includegraphics[width=.2\textwidth]{example-image-a}
  \caption{Caption.\label{fig1}}
\end{figure}

\end{document}

在此处输入图片描述

我试图传递字符串

\makeatletter
\def\p@figure{\color{red}}
\def\p@equation{\color{green}}
\makeatother

到 LaTeX 引擎:

latex "\AtBeginDocument{\makeatletter\def\p@equation{\color{red}}\def\p@equation{\color{green}}\makeatother} \input{mydocument.tex}"

但我想那\AtBeginDocument不是做这件事的正确地方。

我也尝试过:

latex "\makeatletter\def\p@figure{\color{red}}\def\p@equation{\color{green}}\makeatother \input{mydocument.tex}"

但只有公式的链接颜色会改变。图形的链接颜色不起作用。

在此处输入图片描述

有办法吗?

答案1

这有效:

latex "\makeatletter\AtBeginDocument{\def\p@figure{\color{red}}\def\p@equation{\color{green}}}\makeatother \input{mydocument.tex}"

也就是说,以松散的形式,

\makeatletter
\AtBeginDocument{
  \def\p@figure{\color{red}}
  \def\p@equation{\color{green}}
}
\makeatother
\input{mydocument.tex}

相关内容