如何在 \ref 命令的输出中包含节和子节的编号?

如何在 \ref 命令的输出中包含节和子节的编号?

是否可以包括节号子节编号\ref命令中?如果我想交叉引用一个定理,我目前只能获得类定理环境的编号,例如 theorem 1

如果定理所在章节的编号为x,而相应的子章节的编号为y,我希望 的输出\refx.y.1,而不仅仅是1。这可以实现吗?

我将添加一个例子:

\documentclass[a4paper,11pt,DIV=10,BCOR=8mm,headsepline,footsepline,
               leqno,fleqn]{scrreprt}
%*
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{latexsym}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
%*
\makeatletter
\@addtoreset{Defi}{subsection}
\makeatother
%*
\newtheoremstyle{def_style} % name
{}                          % space above
{}                          % space below
{\upshape}                  % body font
{}                          % indent amount
{\bfseries}                 % theorem head font
{:}                         % punctuation after theorem head
{\newline}                  % space after theorem head
{}                          % theorem head spec
                            % (can be left empty, meaning 'normal')
\theoremstyle{def_style}
\newtheorem{Defi}{Definition}
\newtheorem{Exa}[Defi]{Example}
\newtheorem{Thm}[Defi]{Theorem}
\newtheorem*{Prf}{Proof}
%*
\renewcommand*{\thesection}{\arabic{section}}
%*
\begin{document}
%*
\pagenumbering{arabic}
%*
\chapter*{Part 1}
%*
\setcounter{section}{0}
\section{Section 1}
\label{sec: Section 1}
%*
\setcounter{subsection}{0}
\subsection{Subsection 1}
\label{ssec: Subsection 1}
%*
\begin{Defi} \label {thm: d1}
This ist the first definition in subsection 1 and section 1.
\end{Defi}
%*
\begin{Thm} \label{thm: s1}
This is the first theorem in section 1, subsection 1.
\end{Thm}
%*
\subsection{Subsection 2}
\label{ssec: Subsection 2}
%*
\begin{Defi} \label{thm: d2}
This is the first definition in subsection 2.
\end{Defi}
%*
\begin{Exa} \label{thm: b1}
This is an example.
\end{Exa}
%*
\section{Section 2}
\label{sec: Section 2}
%*
\subsection{Subsection 1 in Section 2}
\label{ssec: Subsection 1 in Section 2}
%*
\begin{Exa} \label{thm: b2}
This is an example in subsection 1 and section 2.
\end{Exa}
%*
\begin{Defi} \label{thm: d3}
This is a definition. We refer to definition~\ref{thm: d2}.
\end{Defi}
%*
\end{document}

答案1

您可以使用\labelformat,但这种交叉引用方法非常不方便,因为读者必须找出某个小节在哪里,而不仅仅是查看语句编号。

我会简单地对各节中的语句进行唯一编号,因此

\newtheorem{Defi}{Definition}[section]

无论如何,这里有一个解决您问题的方法,它将删除一个0.如果语句恰好位于\section和第一个之间\subsection(但是增加了查找的复杂性)。

\documentclass[
  a4paper,
  11pt,
  DIV=10,
  BCOR=8mm,
  headsepline,
  footsepline,
  leqno,
  fleqn
]{scrreprt}
%\usepackage[utf8]{inputenc} % no longer needed
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
%\usepackage{latexsym} % not needed with amssymb
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\newtheoremstyle{def_style} % name
{}                          % space above
{}                          % space below
{\upshape}                  % body font
{}                          % indent amount
{\bfseries}                 % theorem head font
{:}                         % punctuation after theorem head
{\newline}                  % space after theorem head
{}                          % theorem head spec
                            % (can be left empty, meaning 'normal')
\theoremstyle{def_style}
\newtheorem{Defi}{Definition}
\newtheorem{Exa}[Defi]{Example}
\newtheorem{Thm}[Defi]{Theorem}
\newtheorem*{Prf}{Proof}

\counterwithin*{Defi}{section}    % reset Defi when incrementing section
\counterwithin*{Defi}{subsection} % reset Defi when incrementing subsection

\labelformat{Defi}{%
  \arabic{section}.%
  \ifnum\value{subsection}>0 \arabic{subsection}.\fi
  \arabic{Defi}%
}

\renewcommand*{\thesection}{\arabic{section}}

\begin{document}

\chapter*{Part 1}

\section{Section 1}
\label{sec:Section 1}

\subsection{Subsection 1}
\label{ssec:Subsection 1}

\begin{Defi} \label {thm:d1}
This ist the first definition in subsection 1 and section 1.
\end{Defi}

\begin{Thm} \label{thm:s1}
This is the first theorem in section 1, subsection 1.
\end{Thm}

\subsection{Subsection 2}
\label{ssec:Subsection 2}

\begin{Defi} \label{thm:d2}
This is the first definition in subsection 2.
\end{Defi}

\begin{Exa} \label{thm:b1}
This is an example.
\end{Exa}

\section{Section 2}
\label{sec:Section 2}

\subsection{Subsection 1 in Section 2}
\label{ssec:Subsection 1 in Section 2}

\begin{Exa} \label{thm:b2}
This is an example in subsection 1 and section 2.
\end{Exa}

\begin{Defi} \label{thm:d3}
This is a definition. We refer to definition~\ref{thm:d2}.
\end{Defi}

\end{document}

我对你的代码做了其他修复:\@addtoreset已经可用\counterwithin;没有该*命令也会修改计数器的表示,而\counterwithin*只会添加到重置池。

在此处输入图片描述

我会避免在标签中使用空格,因为编辑器将空格作为在输入表示中换行的好位置,并且您可能会将标签拆分到多行中,从而导致查找困难。

请注意,\pagenumbering{arabic}如果\setcounter{section}{0}数字你的章节,你应该做。

如果你不想给章节编号,可以

\stepcounter{section}\addtocounter{section}{-1}

否则相关计数器将不会重置。

答案2

欢迎来到 TeX.SE。我相信以下内容:

\labelformat{Defi}{\thesubsection.\arabic{Defi}}

应该会按照你的意愿进行。这还会打印“部分小节\ref 用于定义或示例时请使用“ ”前缀,因为您定义了DefiExaThm都共享同一个计数器,名为Defi

如果您的 LaTeX 版本太旧而没有该\labelformat命令,请在之前使用以下命令\labelformat{Defi}{...}

\providecommand*{\labelformat}[1]{\expandafter\def\csname p@#1\endcsname##1}

这样,\labelformat仅当尚未定义(通常由 LaTeX 内核定义)时才会使用该定义。

\documentclass{report}
\usepackage{amsthm}

\makeatletter
\@addtoreset{Defi}{subsection}
\makeatother

\newtheoremstyle{def_style} % name
{}                          % space above
{}                          % space below
{\upshape}                  % body font
{}                          % indent amount
{\bfseries}                 % theorem head font
{:}                         % punctuation after theorem head
{\newline}                  % space after theorem head
{}                          % theorem head spec
                            % (can be left empty, meaning 'normal')
\theoremstyle{def_style}
\newtheorem{Defi}{Definition}
\newtheorem{Exa}[Defi]{Example}
\newtheorem{Thm}[Defi]{Theorem}
\newtheorem*{Prf}{Proof}

\renewcommand*{\thesection}{\arabic{section}}
\labelformat{Defi}{\thesubsection.\arabic{Defi}}

\begin{document}

\chapter*{Chapter 1}

\section{Section 1}
\label{sec: Section 1}

\subsection{Subsection 1}
\label{ssec: Subsection 1}

\begin{Defi} \label {thm: d1}
This ist the first definition in subsection 1 and section 1.
\end{Defi}

\begin{Thm} \label{thm: s1}
This is the first theorem in section 1, subsection 1.
\end{Thm}

\subsection{Subsection 2}
\label{ssec: Subsection 2}

\begin{Defi} \label{thm: d2}
This is the first definition in subsection 2.
\end{Defi}

\begin{Exa} \label{thm: b1}
This is an example.
\end{Exa}

\section{Section 2}
\label{sec: Section 2}

\subsection{Subsection 1 in Section 2}
\label{ssec: Subsection 1 in Section 2}

\begin{Exa} \label{thm: b2}
This is an example in subsection 1 and section 2.
\end{Exa}

\begin{Defi} \label{thm: d3}
This is a definition. We refer to definition~\ref{thm: d2} and
theorem~\ref{thm: s1}.
\end{Defi}

\end{document}

在此处输入图片描述

如果你想要“合格的编号”(用“部分小节“前缀)到在陈述定义、例子和定理时使用,将该\labelformat{Defi}{\thesubsection.\arabic{Defi}}行替换为:

\renewcommand{\theDefi}{\thesubsection.\arabic{Defi}}

您将获得以下内容(我故意不再重复不相关的章节标题):

在此处输入图片描述

但是,实现相同目的的更惯用的方法可能是删除它:

\makeatletter
\@addtoreset{Defi}{subsection}
\makeatother

并将subsection计数器声明为计数器的父级Defi

\newtheorem{Defi}{Definition}[subsection]

(则输出与上面相同)。

注意:请不要在最小工作示例中包含不相关的包,并且在类不重要时使用 LaTeX 标准类(article除非您真的想看到效果\chapter... 就像这里的情况一样,因此我选择了report文档类)。

答案3

你写道(经过了一些解释):

假设编号为 的定理1位于编号为 的一节x和编号为 的一小节中y。我希望对该定理的交叉引用看起来像x.y.1,而不仅仅是1

由于您设置了一个称为Defi类定理环境的计数器,因此我所知道的实现目标的最简单方法是添加指令

\renewcommand{\p@Defi}{\thesubsection.}

到序言。创建\p@Defi计数器时,LaTeX 会自动创建该宏Defi。默认情况下,该宏为空;可以通过\renewcommand指令进行更改。

在以下可编译示例中,我已将您的代码精简到我认为的最低限度。我相信,这可以更轻松地理解要点。我加载了hyperrefcleveref包,主要是为了更轻松地查看交叉引用的样子。

在此处输入图片描述

\documentclass[a4paper,11pt,DIV=10,BCOR=8mm, 
   headsepline,footsepline,leqno,fleqn]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
%%\usepackage{latexsym} % don't load this package!!!
\usepackage{amsmath,amssymb,amsthm}

%% Just for this example:
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}

\theoremstyle{definition} % no need to be too fancy for this example...
\newtheorem{Defi}{Definition}
\newtheorem{Exa}[Defi]{Example}
\newtheorem{Thm}[Defi]{Theorem}
\newtheorem*{Prf}{Proof}

\makeatletter
\@addtoreset{Defi}{subsection}
\renewcommand{\p@Defi}{\thesubsection.} % <-- new!
\makeatother

\renewcommand*{\thesection}{\arabic{section}}

\begin{document}

\section{Section 1}
\subsection{Subsection 1} \label{ssec:Subsection1}
\begin{Defi} \label{thm:dx1}
This is the first theorem-like environment of subsection \ref{ssec:Subsection1}.
\end{Defi}

\subsection{Subsection 2} \label{ssec:Subsection2}
\begin{Defi} \label{thm:dx2}
This is the first theorem-like environment of subsection \ref{ssec:Subsection2}.
\end{Defi}
\begin{Exa} \label{thm:ey2}
This is an example.
\end{Exa}
\begin{Thm} \label{thm:tz2}
This is a theorem.
\end{Thm}

\section{Section 2}
We now cross-reference \cref{thm:dx2,thm:ey2,thm:tz2}.
\end{document}

相关内容