11

I have an equation environment which contains a number of named rules. I would like to give these rules labels and have a command which refers to the name of the rule. This should work with the hyperref package; ideally also with the \autoref command.

MWE:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{proof}
\usepackage{hyperref}

\newcommand{\rulelabel}[2]{#2}
\newcommand{\ruleref}[1]{#1}

\begin{document}

\begin{equation}
  \infer[\rulelabel{rule:conjI}{\mathrm{ConjI}}]
    {A \land B}
    {A & B}
  \qquad
  \infer[\rulelabel{rule:conjE}{\mathrm{ConjE}}]
    {C}
    {A \land B & A \implies B \implies C}
\end{equation}

Note that the rules \ruleref{rule:conjI} and \ruleref{rule:conjE} are complementary.

\end{document}

The \rulelabel command should create a label and display the text in the second argument. The \ruleref command should provide a link to the label and display the text in the second argument of \rulelabel. It would be nice if \autoref{rule:conjI} worked and displayed "Rule #2".

This question is similar to "Refer to the "name" of an equation, while a List of Equations is generated using these names", except that the solutions there do not work with the hyperref package.

cebewee
  • 357

1 Answers1

11

The trick is using \ltx@label (this is defined by amsmath as the default \label inside a display environment); we need also to add some help to hyperref in order it assigns the correct autoref name to the equation number.

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{proof}
\usepackage{etoolbox}
\usepackage[colorlinks]{hyperref}

\newcounter{rulelabel}
\makeatletter
\newcommand{\rulelabel}[2]{%
  \renewcommand{\therulelabel}{\ensuremath{#2}}%
  \refstepcounter{rulelabel}%
  \ltx@label{#1}%
  #2
}
\appto\equation{\let\saved@currentHref\@currentHref}
\AtEndEnvironment{equation}{\let\@currentHref\saved@currentHref}
\makeatother
\newcommand{\rulelabelautorefname}{Rule}

\begin{document}

\begin{equation}\label{test}
  \infer[\rulelabel{rule:conjI}{\mathrm{ConjI}}]
    {A \land B}
    {A & B}
  \qquad
  \infer[\rulelabel{rule:conjE}{\mathrm{ConjE}}]
    {C}
    {A \land B & A \implies B \implies C}
\end{equation}

Note that the rules \ref{rule:conjI} and \ref{rule:conjE} are complementary.

Note that \autoref{rule:conjI} and \autoref{rule:conjE} are complementary;
they are in \autoref{test}.

\begin{align}
\label{testA}
  &\infer[\rulelabel{rule:conjI-1}{\mathrm{ConjI}}]
    {A \land B}
    {A & B}
\\
\label{testB}
  &\infer[\rulelabel{rule:conjE-1}{\mathrm{ConjE}}]
    {C}
    {A \land B & A \implies B \implies C}
\end{align}

Note that the rules \ref{rule:conjI-1} and \ref{rule:conjE-1} are complementary.

Note that \autoref{rule:conjI-1} and \autoref{rule:conjE-1} are complementary;
they are in \autoref{testA} and \autoref{testB}.

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks a lot! I noticed that \autoref{test} in your example created the text "Rule 1" instead of "Equation 1". This goes away if I change the equation environment to align. – cebewee Jul 08 '15 at 08:42
  • 1
    @cebewee Now it should be fixed. – egreg Jul 08 '15 at 09:40