覆蓋自訂方程式環境的自動引用名稱

覆蓋自訂方程式環境的自動引用名稱

我無法根據使用自訂名稱的環境取得\autoref從套件到自訂環境的參考。此類引用不使用自訂名稱,而是使用“方程式”。hyperrefequation

考慮以下範例:

\documentclass{article}

\usepackage{hyperref}

% Define a 'query' counter and environment, based on 'equation'
\newcounter{query}
\newenvironment{query}
{\refstepcounter{query}%
\renewcommand\theequation{Q\arabic{query}}\equation}
{\endequation}

% Attempt to override the name autoref uses for the query environment
\newcommand{\queryautorefname}{Query}

\begin{document}

A labelled instance of the query environment can be found below:
%
\begin{query}
    \label{q:my_query}
    Q(x) = ...
\end{query}

A reference to \autoref{q:my_query} using \texttt{autoref} uses the wrong name to refer to the environment (i.e., Equation rather than Query).

\end{document}

這個問題(如範例中所述)是,以這種方式進行的參考將讀取“方程式 Q1”,而我希望它讀取“查詢 Q1”。為了實現這個目標我可以改變什麼?

我願意採用不同的策略來使其發揮作用(例如,環境的不同定義query),但重要的是,環境的內容query應設置為數學模式,並且環境的計數器獨立於其他櫃檯(如equation櫃檯)。在這種情況下使用cleveref而不是有問題的,因為它給我帶來了與我必須使用的模板/文檔類hyperref結合的麻煩。svjour3

答案1

\documentclass{article}

\usepackage{hyperref}

% Define a 'query' counter and environment, based on 'equation'
\newcounter{query}
\makeatletter
\newenvironment{query}
{\stepcounter{query}%
 \def\Hy@chapterstring{equation}%
 \def\Hy@chapapp{query}%
\renewcommand\theequation{Q\arabic{query}}\equation}
{\endequation}
\makeatother
% Attempt to override the name autoref uses for the query environment
\newcommand{\queryautorefname}{Query}

\begin{document}

A labelled instance of the query environment can be found below:
%
\begin{query}
    \label{q:my_query}
    Q(x) = ...
\end{query}

A reference to \autoref{q:my_query} using \texttt{autoref} uses the wrong name to refer to the environment (i.e., Equation rather than Query).

\end{document}

在此輸入影像描述

相關內容