如果方程式不在当前小节中,则使用 \eqref 获取小节

如果方程式不在当前小节中,则使用 \eqref 获取小节

正如标题所述,我想定义一个eqref命令,如果方程式在当前小节中,则它会产生通常的 (1),如果方程式在另一个小节中,则引用看起来像 ¶2.3 方程式 (4)。这与我之前的问题类似在类定理环境中的交叉引用中包含小节编号(@egreg 给出的解决方案)。

我尝试复制代码并做了一些修改。代码如下:

\let\eqref\relax
\makeatletter
\renewcommand*{\c@equation}{\perhapssubsection{\thesubsection}} 
\newcommand{\eqref}[1]{\textsc{Equation}~\textup{(\ref{#1})}}
\makeatother 

但是,它不起作用。你能帮助我吗?

这是我的 MWE:

\documentclass[a4paper,12pt]{article}

\usepackage{mathtools}
\usepackage{xparse}
\usepackage{amsthm}
\usepackage{thmtools}

\declaretheoremstyle[%
spaceabove=\topsep,
spacebelow=\topsep,
headfont=\scshape,
bodyfont=\itshape,
notefont=\normalfont,
notebraces={}{.},
headpunct={},
postheadspace=1em,
headformat=\NAME\space\NUMBER.---\NOTE,
headindent=\parindent
%   qed=$\qedsymbol$%
]{theorem}

\declaretheorem[style=theorem,name=Theorem,numberwithin=subsection]{theorem}


\makeatletter
\@addtoreset{equation}{subsection}% Reset equation at \section
\makeatother
\makeatletter
\def\gobblesomething#1\csname thesection\endcsname{\begingroup\normalfont\S\thesection\endgroup}
\def\gobblesomethingother#1\csname thesubsection\endcsname{\begingroup\normalfont\P\thesubsection\endgroup}
\renewcommand*{\p@section}{\gobblesomething}
\renewcommand*{\p@subsection}{\gobblesomethingother}
\makeatother

\renewcommand{\thetheorem}{\arabic{theorem}}

\makeatletter
\def\gobblesomethinga#1\csname thetheorem\endcsname{\begingroup\normalfont\textsc{Theorem}~\thetheorem\endgroup}
\renewcommand*{\p@theorem}{\perhapssubsection{\thesubsection}\gobblesomethinga}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\perhapssubsection}{m}
 {
  \str_if_eq_x:nnF { #1 }{\thesubsection}{\P\textup{#1}~}
 }
\ExplSyntaxOff

\begin{document}

\section{One}

\subsection{A}

\begin{theorem}
\label{th:E}
Bla bla bla
\begin{equation}
E=mc^2
\label{eq:E}
\end{equation}
\end{theorem}

\subsection{B}

In \ref{th:E} and \eqref{eq:E}

\end{document}

答案1

我不会这样做:参考“定理 1.1.1”是很多比“¶1.1 定理 1”更清晰。

不管怎样,再多玩一些杂耍就能达到你想要的效果。

\documentclass[a4paper,12pt]{article}

\usepackage{mathtools}
\usepackage{xparse}
\usepackage{amsthm}
\usepackage{thmtools}

\declaretheoremstyle[
  spaceabove=\topsep,
  spacebelow=\topsep,
  headfont=\scshape,
  bodyfont=\itshape,
  notefont=\normalfont,
  notebraces={}{.},
  headpunct={},
  postheadspace=1em,
  headformat=\NAME\space\NUMBER.---\NOTE,
  headindent=\parindent,
%   qed=$\qedsymbol$,
]{theorem}

\declaretheorem[style=theorem,name=Theorem,numberwithin=subsection]{theorem}


\counterwithin{equation}{subsection}

\renewcommand{\thetheorem}{\arabic{theorem}}
\renewcommand{\theequation}{\perhapsbrackets{\arabic{equation}}}

\makeatletter
\renewcommand*{\p@theorem}{\perhapssubsection{Theorem}{\thesubsection}}
\renewcommand*{\p@equation}{\perhapssubsection{Equation}{\thesubsection}}
\makeatother

\ExplSyntaxOn
\RenewDocumentCommand{\eqref}{m}
 {
  \group_begin:
  \bool_set_true:N \l_dog_brackets_bool
  \ref{#1}
  \group_end:
 }
\bool_new:N \l_dog_brackets_bool

\NewDocumentCommand{\perhapsbrackets}{m}
 {
  \bool_if:NTF \l_dog_brackets_bool { \textup{(#1)} } { \textup{#1} }
 }

\NewDocumentCommand{\perhapssubsection}{mm}
 {
  \str_if_eq:eeF { #2 }{\thesubsection}
   {
    \P\textup{#2}\nobreakspace
   }
  \str_case:nn { #1 }
   {
    {Theorem}{\textsc{Theorem}\nobreakspace}
    {Equation}{\textsc{Equation}\nobreakspace}
   }
 }
\ExplSyntaxOff

\begin{document}

\section{One}

\subsection{A}

\begin{theorem}
\label{th:E}
Bla bla bla
\begin{equation}
E=mc^2
\label{eq:E}
\end{equation}
\end{theorem}

In \ref{th:E} and \eqref{eq:E}

\subsection{B}

In \ref{th:E} and \eqref{eq:E}

\end{document}

在此处输入图片描述

相关内容