引用章节、节和项目

引用章节、节和项目

我正在写一本以 \documentclass[11pt]{amsbook} 开头的书。我在章节内设置了一个项目计数器,我希望交叉引用显示如下:

在章节编号 X 中,对同一章节中第 2 节第 3 项的引用应为 2.3 ;但来自不同章节编号 Y 的对第 2 节第 3 项的引用应为 Y.2.3 。

我怎样才能做到这一点?

\documentclass{amsbook}
 \newcounter{contRef}[section] 
\setcounter{contRef}{0}Thank you 
\renewcommand{\thecontRef}{\thesection.\arabic{contRef}}
\NewDocumentCommand \newItem{mo} {
 \IfNoValueTF {#2}
{\noindent\hskip-.07in\refstepcounter{contRef} {\bf#1}\textbf{ \thecontRef}}
{\noindent\hskip-.07in\refstepcounter{contRef} {\bf#1}\textbf{ \thecontRef}{\bf \hskip.05in#2} }
 }
\begin{document}
\chapter{(title)}
A reference to an item in this chapter from a location in this chapter should be of the form [section number].[item number].
\section{Section One in Chapter One}
\newItem{Definition}\label{def} This is a lemma that is references in this chapter as Definition \ref{def}.
\section{Section Two in Chapter One}
Here we refer to Definition  \ref{def}
Now scroll down to Chapter 2.
\chapter{(title)}
I want a reference from this chapter to the item labelled ``def'' in chapter 1 to have the form 1.1.1.
However, the present numbering system refers to it as Definition  \ref{def}.
\end{document}

答案1

您可以\labelformat添加用于比较章节编号的前缀。

\documentclass{amsbook}

\newcounter{contRef}[section] 

\renewcommand{\thecontRef}{\arabic{section}.\arabic{contRef}}

\labelformat{contRef}{\maybechapter{\arabic{chapter}}{\thechapter}#1}

\NewDocumentCommand{\maybechapter}{mm}{%
  \ifnum#1=\arabic{chapter}\else#2.\fi
}

\NewDocumentCommand \newItem{mo} {%
  \par\noindent
  %\hspace{-.07in}% why?
  \refstepcounter{contRef}%
  \textbf{#1 \thecontRef\IfValueT{#2}{ #2}}%
 }
\begin{document}

\chapter{(title)}

A reference to an item in this chapter from a location in this chapter 
should be of the form [section number].[item number].

\section{Section One in Chapter One}

\newItem{Definition}\label{def} This is a lemma that is references in this 
  chapter as Definition \ref{def}.

\section{Section Two in Chapter One}

Here we refer to Definition  \ref{def} Now scroll down to Chapter 2.

\chapter{(title)}

I want a reference from this chapter to the item labelled ``def'' 
in chapter 1 to have the form 1.1.1.
However, the present numbering system refers to it as Definition  \ref{def}.

\end{document}

在此处输入图片描述

在此处输入图片描述

我使用了两个参数,因此如果您决定用罗马数字表示章节,则输出将符合预期。

如果添加\renewcommand{\thechapter}{\Roman{chapter}},则第二章中的引用就正确了。

在此处输入图片描述

我删除了负缩进,但我觉得这没什么道理。我还添加\par并简化了代码,还删除了多余的空格。

请注意,该功能\bf已被弃用约 30 年了。

答案2

我会进行修改\thechapter以减少混乱。

\documentclass{amsbook}
\usepackage{refcount}% or hyperref
%\usepackage{hyperref}

%\renewcommand{\thechapter}{\Roman{chapter}}

\newcounter{item}[chapter]

\makeatletter
\newcommand{\itemlabel}[1]{% #1 = label name
  \edef\@currentlabel{\arabic{section}.\arabic{item}}% does not depend on \thesection or \theitem
  \label{#1}%
  \edef\@currentlabel{\thechapter.\arabic{section}.\arabic{item}}%
  \edef\@currentlabelname{\thechapter}%
  \label{chapter@#1}%
}
\makeatother

\newcommand{\itemref}[1]% #1 = label name
{\bgroup% use local definitions
  \edef\A{\getrefbykeydefault{chapter@#1}{name}{0}}%
  \edef\B{\thechapter}%
  \ifx\A\B\relax
    \ref{#1}%
  \else
    \ref{chapter@#1}%
  \fi
\egroup}
  
\begin{document}
\chapter{One}
\section{Cross}
\refstepcounter{item}% also sets \@currentlabelname and \@currentHlabel
\itemlabel{testA}

See \itemref{testA} and \itemref{testB}.

\chapter{One}
\section{Party}
\refstepcounter{item}% also sets \@currentlabelname and \@currentHlabel
\itemlabel{testB}

See \itemref{testA} and \itemref{testB}.

\end{document}

相关内容