\autoref 的链接只包含数字,不包含文本,并且具有反向相关颜色

\autoref 的链接只包含数字,不包含文本,并且具有反向相关颜色

我希望 的功能hyperref\autoref链接边框仅覆盖数字而不是前面的文本(例如“部分”)。这在包中通常是可能的cleveref(所以这通常已经回答了我的问题),但作为额外的转折,我想链接颜色取决于计数器的类型比如,为章节、部分和小节使用不同深浅的绿色,为表格和图表使用不同深浅的紫色。下面是一个简单、不言自明的例子:

\documentclass{memoir}
  \setsecnumdepth{subsubsection}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{hyperref}
  \renewcommand*{\tableautorefname}{table}
  \renewcommand*{\figureautorefname}{figure}

\begin{document}

\chapter{The only chapter}
\label{theonlychapter}

Text.

\begin{table}
\caption{The only table\label{theonlytable}}
\centering
\begin{tabular}{lll}
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}
\end{table}

\begin{figure}
\caption{The only figure\label{theonlyfigure}}
\centering
A figure.
\end{figure}

\section{The only section}
\label{theonlysection}

Text.

\subsection{The only subsection}
\label{theonlysubsection}

Text.

See \autoref{theonlychapter}. See \autoref{theonlysection}. See \autoref{theonlysubsection}. See \autoref{theonlytable}. See \autoref{theonlyfigure}.

colors: \textcolor{PineGreen}{chapter}, \textcolor{ForestGreen}{section}, \textcolor{LimeGreen}{subsection}, \textcolor{Violet}{table}, \textcolor{Mulberry}{figure}

\end{document}

该解决方案是否使用hyperrefcleveref我来说并不重要,但我需要hyperref独立加载我的文档。

答案1

解决方案是hyperref。这里,链接参考是彩色的,链接边框不可见。对于链接引用是黑色的,但链接边界要着色(按照原始问题的要求),只需删除colorlinks包的选项hyperref(例如,在%之前添加colorlinks)。

\documentclass{memoir}
  \setsecnumdepth{subsubsection}
\usepackage{xparse}
  % for \RenewDocumentCommand and \IfValueTF (for handling the optional argument)
\usepackage{xifthen} % for \isempty (testing whether the optional argument is empty)
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[
  colorlinks % comment this line out for colored boxes around the link numbers
]{hyperref}

\usepackage{letltxmacro}
\usepackage{refcount}

\makeatletter
\LetLtxMacro\org@autoref\autoref % "org" stands for "original"
\RenewDocumentCommand{\autoref}{o m}{%
  % test 1: reference defined?
  \@ifundefined{r@#2}{%
    \org@autoref{#2}%
  }{%
    \begingroup
      \def\ar@next{\org@autoref{#2}}%
      % test 2: anchor available?
      \edef\ar@anchor{\getrefbykeydefault{#2}{anchor}{}}%
      \ifx\ar@anchor\@empty
      \else
        % test 3: counter part extractable from anchor name?
        \expandafter\ar@getcounter\ar@anchor.\@nil
        \ifx\ar@counter\ar@dot
        \else
          % test 4: color defined?
          \@ifundefined{\ar@counter autorefcolor}{%
            \def\ar@next{\IfValueTF{#1}{%
              \ifthenelse{\isempty{#1}}{\ref{#2}}{#1~\ref{#2}}%
            }{\ar@autoref{#2}}}%
          }{%
            \def\ar@next{%
              \hypersetup{%
                linkcolor=\csname\ar@counter autorefcolor\endcsname,%
                linkbordercolor=\csname\ar@counter autorefcolor\endcsname
              }%
              \IfValueTF{#1}{%
                \ifthenelse{\isempty{#1}}{\ref{#2}}{#1~\ref{#2}}%
              }{\ar@autoref{#2}}%
            }%
          }%
        \fi
      \fi
      \ar@next
    \endgroup
  }%
}
\def\ar@getcounter#1.#2\@nil{%
  \def\ar@counter{#1}%
}
\newcommand*{\ar@dot}{.}
\newcommand*{\ar@autoref}[1]{%
  \@ifundefined{\ar@counter autorefname}{%
    \@ifundefined{\ar@counter name}{%
    }{%
      \@nameuse{\ar@counter name}~%
    }%
  }{%
    \@nameuse{\ar@counter autorefname}~%
  }%
  \ref{#1}%
}
\newcommand*{\setautorefcolor}[2]{%
  \@namedef{#1autorefcolor}{#2}%
}
\makeatother

\setautorefcolor{chapter}{PineGreen}
\setautorefcolor{section}{ForestGreen}
\setautorefcolor{subsection}{LimeGreen}
\setautorefcolor{table}{violet}
\setautorefcolor{figure}{Mulberry}

\renewcommand*{\tableautorefname}{table}
\renewcommand*{\figureautorefname}{figure}

\begin{document}

\chapter{The only chapter}
\label{theonlychapter}

Text.

\begin{table}
\caption{The only table\label{theonlytable}}
\centering
\begin{tabular}{lll}
  1 & 2 & 3 \\
  4 & 5 & 6 \\
  7 & 8 & 9 \\
\end{tabular}
\end{table}

\begin{figure}
\caption{The only figure\label{theonlyfigure}}
\centering
A figure.
\end{figure}

\begin{equation}
E=mc^2\label{eq}
\end{equation}

\section{The only section}
\label{theonlysection}

Text.

\subsection{The only subsection}
\label{theonlysubsection}

{
  \parindent0pt % disablement of paragraph indentation
  \setlength{\parskip}{\baselineskip} % empty lines between paragraphs

Text.

See \autoref{theonlychapter}. See \autoref{theonlysection}. See
\autoref{theonlysubsection}.\\
See \autoref{theonlytable}. See \autoref{theonlyfigure}.
See \autoref{eq}.

See \autoref[customtext]{theonlychapter}. See \autoref[customtext]{theonlysection}. See
\autoref[customtext]{theonlysubsection}.\\
See \autoref[customtext]{theonlytable}. See \autoref[customtext]{theonlyfigure}.
See \autoref[customtext]{eq}.

See \autoref[parts]{theonlysection} and especially \autoref[]{theonlysubsection} of
this text.

colors: \textcolor{PineGreen}{chapter}, \textcolor{ForestGreen}{section},
\textcolor{LimeGreen}{subsection}, \textcolor{Violet}{table},
\textcolor{Mulberry}{figure}, \textcolor{red}{equation}.

}

\end{document}

结果

修改计数器描述符

原文\autoref不支持大写和复数形式,这里给出两种解决方案:

1. 手动解决方法

可以在组内指定颜色设置,例如:

\documentclass{memoir}                                                         
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{hyperref}

\newcommand*{\tableref}[1]{%
  \begingroup
    \hypersetup{linkbordercolor=violet}%
    \ref{#1}%
  \endgroup
}

\begin{document}
Tables \tableref{one} and \tableref{two}.

\begin{table}
\caption{First caption}\label{one}
\caption{Second caption}\label{two}
\end{table}
\end{document}

2. 将其融入\autoref

\autoref命令(如上所定义)接受一个可选参数,该参数直接指定链接文本。其语法为\autoref[customtext]{label}。它可以按如下方式使用::see \autoref[parts]{theonlysection} and especially \autoref[]{theonlysubsection} of this text如果可选参数为空([]),则~在无文本参考编号前将插入“no”。(我们假设它前面已经有一个普通空格。否则我们仍然可以像 一样使用它this~\autoref[]{label}。)

如果描述符是通用的,并且引用的实体可以更改类别(例如在“部分”、“子部分”和“子子部分”之间),则第二种方法是必要的。在这里,\autoref使用可选参数将自动选择文本或链接边框的颜色,而使用\sectionref\subsectionref或的方法\subsubsectionref将要求 TeX 用户在源代码中的三者之间进行选择。

相关内容