我创建了自己的计数器和一个名为问题。计数器按章而不是节的级别递增。当我标记(例如)\label{prb:3}
并在文本后面通过 引用它时\ref{prb:3}
,不是插入与该标签链接的真实问题编号,而是插入问题所在的节编号。这显然会造成严重的麻烦。虽然我检查了有关此问题的一些类似的先前问题,但我没有发现对我的情况有帮助的东西。例如,使用Problem~\hyperref[prb:3]{prb:3}
打印问题 prb:3,而它应该是问题 5-8。
本质上,我的\label
“and”\ref
命令应该链接到我的问题编号,而不是问题所在的部分编号。
我相信有足够多的 LaTeX 专家可以帮助我建立和解决这个问题,最好没有复杂的定义。
以下是一些 MWE:
\documentclass[a4paper,11pt, oneside]{book}
\usepackage[utf8]{inputenc}
%%% my custom counter `problem` and the environment, which increments on the chapter level
\newcounter{problem}
\newenvironment{problem}{
\vspace{10pt}\noindent\stepcounter{problem}\\
{\bf\thechapter-\theproblem}\hspace{10pt}
}{}
\begin{document}
\chapter{First Chapter}
\section{Section 11}
\begin{problem}
Problem~1-1. \label{prb 11} % Problem 1-1
\end{problem}
\section{Section 12}
\begin{problem}
Problem 1-2. \label{prb 12} % Problem 1-2
\end{problem}
\begin{problem}
Problem 1-3. \label{prb 13} \\ % Problem 1-3
\end{problem}
\noindent Referring to Problem~1-3 by \ref{prb 13} incorrectly refers to Problem~1.2 because Problem~1-3 is in Section~\ref{prb 13} (that is: Section 1.1). \\ \\
\setcounter{problem}{0}
\chapter{Second Chapter}
\section{Section 21}
\begin{problem}
Problem 2-1. \label{prb 21} % Problem 2-1
\end{problem}
\begin{problem}
Problem 2-2. \label{prb 22} % Problem 2-2
\end{problem}
\begin{problem}
Problem 2-3. \label{prb 23} \\ % Problem 2-3
\end{problem}
\noindent Now referring to Problem~2-3 by \ref{prb 23} erroneously prints as Problem~2.1 because Problem~2-3 is within Section~\ref{prb 23} (that is: Section 2.1). \\ \\
So, not only is the number incorrect, but also the format. Instead of having chapter-problem\# format, it has section\# format. \\
The question is: how can I make the \verb|\label| and \verb|\ref| not refer to a Section number, but to the Problem number within a particular chapter?
\end{document}
答案1
您的问题相关代码至少存在三个问题 [双关语]。
最严重的问题是,你使用
\stepcounter
而不是\refstepcounter
来增加名为 的计数器problem
。\label
-\ref
机制依赖于(除了少数例外)LaTeX 将 的参数\label
与计数器相关联最近增加通过\refstepcounter
指令。\stepcounter
不行;这就是为什么\ref
指令一直指向节号,因为,正如你现在可能已经猜到的,section
计数器是\refstepcounter
每当\section
执行命令时,通过指令增加。目前,您必须
\setcounter{problem}{0}
在每章开始时运行。如果您\newcounter{problem}
用替换,则每次出现指令时\newcounter{problem}[chapter]
,LaTeX 都会自动将计数器重置为 0。problem
\chapter
由于您希望将问题的“编号”显示为“ChapterNum-ProblemNum”,因此您不妨运行
\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
在序言中
\documentclass[oneside]{book}
%% \usepackage[utf8]{inputenc} that's the default nowadays
\newcounter{problem}[chapter]
\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
\newenvironment{problem}{%
\par\vspace{10pt}\noindent\refstepcounter{problem}% % note "\refstepcounter"
\textbf{\theproblem}\hspace{10pt}}%
{\par}
\usepackage[colorlinks]{hyperref} % optional
\begin{document}
\chapter{First Chapter}
\section{Section A}
\begin{problem}
Problem~1-1. \label{prb 11} % Problem 1-1
\end{problem}
\section{Section B}
\begin{problem}
Problem 1-2. \label{prb 12} % Problem 1-2
\end{problem}
\begin{problem}
Problem 1-3. \label{prb 13} % Problem 1-3
\end{problem}
\bigskip\noindent Now cross-referencing Problem~1-3 as \ref{prb 13}, \emph{not} as [section] 1.2.
\chapter{Second Chapter}
\section{Section C}
\begin{problem}
Problem 2-1. \label{prb 21} % Problem 2-1
\end{problem}
\begin{problem}
Problem 2-2. \label{prb 22} % Problem 2-2
\end{problem}
\begin{problem}
Problem 2-3. \label{prb 23} % Problem 2-3
\end{problem}
\bigskip\noindent Now cross-referencing Problem~2-3 as \ref{prb 23}, \emph{not} as [section] 2.1.
\end{document}
回应@Mico,这是新的 MWE,解释了 \ref 在引用主文件中定义的 \label 而不是外部文件中定义的 \label 时如何工作。
\documentclass[oneside]{book}
\newcounter{problem}[chapter]
\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
\newenvironment{problem}{%
\par\vspace{10pt}\noindent\refstepcounter{problem}%
\textbf{\theproblem}\hspace{10pt}}%
{\par}
%\renewcommand\theproblem{\arabic{chapter}-\arabic{problem}}
\usepackage[colorlinks]{hyperref} % optional
\begin{document}
\chapter{Chapter Title}
\section{Section Title}
\begin{problem}
Problem 1-1. \label{prb_11}
\end{problem}
\begin{problem}
Problem 1-2. \label{prb_12}
\end{problem}
\input{prb_13}
\bigskip\noindent When the \verb|\label| is used within the main file, the cross-referencing works well and \verb|\ref{prb_11}| gives \ref{prb_11} and \verb|\ref{prb_12}| gives \ref{prb_12}. Correct!
\medskip\noindent In contrast, when the label is defined within the outer file named \verb|prb_13| and included using the \verb|\input| command, the cross-referencing does not work well and \verb|\ref{prb_13}| gives \ref{prb_13}, which is not the problem number, but the section number.
\end{document}
答案2
编辑:分别参见 David Carlisle 和 Mico 的评论和回答(“值得尊重”)
您可以使用自定义标签来完成此操作,如下所示:
https://tex.stackexchange.com/a/18192/120578
\documentclass[a4paper,11pt, oneside]{book}
\usepackage[utf8]{inputenc}
%%% my custom counter `problem` and the environment, which increments on the chapter level
\newcounter{problem}
\newenvironment{problem}{
\vspace{10pt}\noindent\stepcounter{problem}\\
{\bf\thechapter-\theproblem}\hspace{10pt}
}{}
\makeatletter
\newcommand{\clabel}[1]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{\thechapter-\theproblem}{}}}}
\makeatother
\begin{document}
\chapter{First Chapter}
\section{Section 11}
\begin{problem}
Problem~1-1. \clabel{prb11} % Problem 1-1
\end{problem}
\section{Section 12}
\begin{problem}
Problem 1-2. \clabel{prb12} % Problem 1-2
\end{problem}
\begin{problem}
Problem 1-3. \clabel{prb13} % Problem 1-3
\end{problem}
\noindent Referring to Problem~1-3 by \ref{prb13} correctly refers to Problem~1.2 [because Problem~1-3 is in Section~\ref{prb13} (that is: Section 1.1). ]
\setcounter{problem}{0}
\chapter{Second Chapter}
\section{Section 21}
\begin{problem}
Problem 2-1. \clabel{prb21} % Problem 2-1
\end{problem}
\begin{problem}
Problem 2-2. \clabel{prb22} % Problem 2-2
\end{problem}
\begin{problem}
Problem 2-3. \clabel{prb23} \\ % Problem 2-3
\end{problem}
\noindent Now referring to Problem~2-3 by \ref{prb23} [prints as Problem~2.1 because Problem~2-3 is within Section~\ref{prb23} (that is: Section 2.1). ]
So, not only is the number incorrect, but also the format. Instead of having chapter-problem\# format, it has section\# format.
The question is: how can I make the \verb|\label| and \verb|\ref| not refer to a Section number, but to the Problem number within a particular chapter?
\end{document}