我将定理标记为subsection.theoremnumber
。例如:定理 2.3 是第 2 节中的第三个定理。如果该节位于第 4 节中,我希望当我在第 6 节中引用此定理时,它显示为定理 4.2.3。
我知道这类似于参考上一章的定理,但那里的答案不适用于部分。
答案1
通过将问题中链接的代码中的所有单词“chapter”更改为“section”,我实现了预期的结果。
输出:
代码:
\documentclass{amsart}
\usepackage{amsthm}
\usepackage{zref}% http://ctan.org/pkg/zref
\makeatletter
\let\oldlabel\label
\renewcommand{\label}[1]{%
\zref@labelbylist{#1}{special}% Special label
\oldlabel{#1}% Old label
}
\newcounter{splabel}
\zref@newlist{special}% Create a new property list called special
\zref@newprop{section}{\arabic{section}}% Section property holds \arabic{section}
\zref@addprop{special}{section}% Add a section property to special
\newcommand*{\thmref}[1]{%
\stepcounter{splabel}% Increment local "special label" counter
\zref@labelbylist{#1-\thesplabel}{special}% Create label
\edef\targetsec{\zref@extractdefault{#1}{section}{-1}}% Extract target section
\edef\sourcesec{\zref@extractdefault{#1-\thesplabel}{section}{-1}}% Extract source section
\ifnum\targetsec=\sourcesec\else\targetsec.\fi%
\ref{#1}%
}
\newtheorem{dummy}{***}[subsection]% Used so that theorems, definitions, etc can have same counter within subsections
\newtheorem{theorem}[dummy]{Theorem}
\newtheorem{lemma}[dummy]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}[dummy]{Definition}
\newtheorem{remark}[dummy]{Remark}
\renewcommand{\thesubsection}{\arabic{subsection}}% Custom numbering on subsection to remove section number
\begin{document}
\section{First Section}
\subsection{First Subsection in Section 1}
\begin{definition}
Good definition.
\end{definition}
\subsection{Second Subsection in Section 1}
\begin{remark}
Interesting remark.
\end{remark}
\begin{lemma} \label{lem:name}
Useful lemma.
\end{lemma}
\subsection{Third subsection in Section 1}
\begin{theorem} \label{thm:name}
Important theorem.
\end{theorem}
\section{Section 2}
\subsection{Another subsection} There's not much to say here.
\subsection{Last Subsection}\
As you can see, Theorem \thmref{thm:name} is very important. Indeed, Lemma~\thmref{lem:name} was quite useful.
\end{document}