重新定义方程式的 \autoref 格式会导致子方程式出现不必要的格式

重新定义方程式的 \autoref 格式会导致子方程式出现不必要的格式

为了能够使用 \autoref 命令以“公式 (2.2.3)”这样的格式引用我论文中的公式,我在序言中添加了以下几行:

\makeatletter
\let\oldtheequation\theequation
\renewcommand\tagform@[1]{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\renewcommand\theequation{(\oldtheequation)}
\makeatother

正如 Mico 在这篇文章中的回答所建议的那样:autoref 和公式编号周围的括号

这对于大多数方程式来说都完美无缺,但会将子方程式的格式从标准格式(如“(2.3.1a)”)更改为“(2.3.1)a”。我希望保留此类子方程式的原始格式。也就是说,我希望字母位于括号内。我该如何实现这一点?

梅威瑟:

\documentclass[12pt]{report}
\usepackage{amsmath}
\usepackage{hyperref}
\numberwithin{equation}{section}

\makeatletter
\let\oldtheequation\theequation
\renewcommand\tagform@[1]{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\renewcommand\theequation{(\oldtheequation)}
\makeatother

\begin{document}

\chapter{Test Chapter}

\section{Test Section}

This is a test equation two including subequations:
\begin{subequations} \label{Eq:MainEquation}
    \begin{equation} \label{Eq:SubEquation1}
        E = mc^2
    \end{equation}
    \begin{equation} \label{Eq:SubEquation2}
        c^2 = a^2 + b^2
    \end{equation}
\end{subequations}

As seen in \autoref{Eq:MainEquation} the labels for \autoref{Eq:SubEquation1} and \autoref{Eq:SubEquation2} are in the wrong format. The letters for the subequations should be inside the parentheses.

\end{document}

说明问题的屏幕截图:截屏

答案1

subequations通过重新定义环境来解决amsmath

\documentclass[12pt]{report}
\usepackage{amsmath}
\usepackage{hyperref}
\numberwithin{equation}{section}

\makeatletter
\let\oldtheequation\theequation
\renewcommand\tagform@[1]{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\renewcommand\theequation{(\oldtheequation)}

\renewenvironment{subequations}{%
  \refstepcounter{equation}%
  % old
  % \protected@edef\theparentequation{\theequation}%
  \protected@edef\theparentequation{\oldtheequation}%
  \setcounter{parentequation}{\value{equation}}%
  \setcounter{equation}{0}%
  % old
  % \def\theequation{\theparentequation\alph{equation}}%
  \def\theequation{(\theparentequation\alph{equation})}%
  \ignorespaces
}{%
  \setcounter{equation}{\value{parentequation}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}

\chapter{Test Chapter}

\section{Test Section}

\makeatletter

This is a test equation two including subequations:
\begin{subequations} \label{Eq:MainEquation}
    \begin{equation} \label{Eq:SubEquation1}
        E = mc^2
    \end{equation}
    \begin{equation} \label{Eq:SubEquation2}
        c^2 = a^2 + b^2
    \end{equation}
\end{subequations}

As seen in \autoref{Eq:MainEquation} the labels for \autoref{Eq:SubEquation1} and \autoref{Eq:SubEquation2} are in the wrong format. The letters for the subequations should be inside the parentheses.

\end{document}

在此处输入图片描述

相关内容