# Get the subsection using \eqref if the equation isnt't in the current subsection

As the title says, I want to define an eqref command such that if the equation is in the current subsection then it produces the usual (1) and that if the equation is in another subsection then the cite looks like ¶2.3 Equation (4). It is similar to my previous question Include the subsection number in cross-reference to a theorem-like environment (the solution given by @egreg).

I have tried to copy the code with some changes. Here it is:

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


However, it does not work. Can you help me, please?

Here is my MWE:

\documentclass[a4paper,12pt]{article}

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

\declaretheoremstyle[%
spaceabove=\topsep,
spacebelow=\topsep,
bodyfont=\itshape,
notefont=\normalfont,
notebraces={}{.},
%   qed=$\qedsymbol$%
]{theorem}

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

\makeatletter
\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
$$E=mc^2 \label{eq:E}$$
\end{theorem}

\subsection{B}

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

\end{document}

• \perhapssubsection is not a default command, so you need to copy its definition also. Sep 18, 2018 at 17:30
• @Sigur Yes. The document is the same so it is already copied. Maybe I could add a MWE. I'm going to edit the question, thanks. Sep 18, 2018 at 17:36
• The definition of \gobblethinga is quite difficult to understand. Sep 18, 2018 at 20:27
• @egreg: Sure. It was thought to avoid italics when the reference is inside a theorem for example. And I cotomized it a bit, so probably is very diffcult. Sep 19, 2018 at 10:21

I wouldn't do like this: a reference “Theorem 1.1.1” is much clearer than “¶1.1 Theorem 1”.

Anyway, some more juggling can do what you want.

\documentclass[a4paper,12pt]{article}

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

\declaretheoremstyle[
spaceabove=\topsep,
spacebelow=\topsep,
bodyfont=\itshape,
notefont=\normalfont,
notebraces={}{.},
%   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
$$E=mc^2 \label{eq:E}$$
\end{theorem}

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

\subsection{B}

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

\end{document}


• egreg: Thanks for your answer. However, it doesn't work on my computer. Frist of all I have to add the chngcntr package because the command \counterwithin. Now LaTeX says anoher error: literally Undefined control sequence. \perhapssubsection code #1#2->\str_if_eq:eeF {#2}{\thesubsection }{\P \textu... l.80 In \ref{th:E} and \eqref{eq:E} I have just copied yuor code. It's really strange. Sep 19, 2018 at 10:53
• @Dog_69 You should first of all update your TeX system: \counterwithin has been part of LaTeX since April 1, 2018. But loading chngcntr can do as well in the meantime. The other problem is also due to an outdated distribution; you can change \str_if_eq:eeF to \str_if_eq_x:nnF. Sep 19, 2018 at 10:59
• Thanks. i'll do it in a minute. Anyway, I have found a (possibly) very very poor solution that makes what I want, so... But I'll edit my TeX Sep 19, 2018 at 11:15
• Indeed, it works fine. Sep 19, 2018 at 11:43