分配号码时出现错误子部分

分配号码时出现错误子部分

我使用了两个不同的命令,一个命令使用可选名称作为参数来分配节/子节/子子节的编号,另一个命令根据节号的深度获取编号。当我打印当前编号时,一切都与实际编号一致。当我使用命令时,子子节编号似乎是错误的。在下面的 MWE 中,它显示子子节 1.1.1 到 1.1.3,但命令总是得到 1.1.1。在我的实际文档中,它发生在 4.2.1 到 4.2.3,但奇怪的是,它总是给我 4.2.2,即使我使用 4.2.1 中的命令。

妇女权利委员会:

\documentclass[12pt,a4paper]{scrartcl}

\newcommand{\setMyNumber}[1]{\newcounter{tocS#1}
\setcounter{tocS#1}{\value{section}}
\newcounter{tocSS#1}
\setcounter{tocSS#1}{\value{subsection}}
\newcounter{tocSSS#1}
\setcounter{tocSSS#1}{\value{subsubsection}}}

\newcommand{\getMyNumber}[1]{
\ifnum\value{tocSSS#1}=0
\ifnum\value{tocSS#1}=0
\arabic{tocS#1}
\else
\arabic{tocS#1}.\arabic{tocSS#1}
\fi
\else
\arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSS#1}
\fi
}

\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.
\subsubsection{Section 1.1.2}
Now the number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\\
But \setMyNumber{Test} sets the number to \getMyNumber{Test}.
\subsubsection{Section 1.1.3}
Here \setMyNumber{TryAgain} still gives \getMyNumber{TryAgain}.
\subsection{Section 1.2}
At this point the command \setMyNumber{ItWorks} seems to work again: \getMyNumber{ItWorks}
\end{document}

答案1

该子小节的计数器缺少S,因此节计数器重复了\getMyNumber

\documentclass[12pt,a4paper]{scrartcl}

\newcommand{\setMyNumber}[1]{%
  \newcounter{tocS#1}%
  \setcounter{tocS#1}{\the\value{section}}%
  \newcounter{tocSS#1}%
  \setcounter{tocSS#1}{\the\value{subsection}}%
  \newcounter{tocSSS#1}%
  \setcounter{tocSSS#1}{\the\value{subsubsection}}%
}

\newcommand{\getMyNumber}[1]{%
\ifnum\value{tocSSS#1}=0
  \ifnum\value{tocSS#1}=0
    \arabic{tocS#1}%
  \else
    \arabic{tocS#1}.\arabic{tocSS#1}%
  \fi
\else
  \arabic{tocS#1}.\arabic{tocSS#1}.\arabic{tocSSS#1}% <- changed, there was a S missing
\fi
}

\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.
\subsubsection{Section 1.1.2}
Now the number is \arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\\
But\setMyNumber{Test} sets the number to \getMyNumber{Test}.
\subsubsection{Section 1.1.3}
Here\setMyNumber{TryAgain} still gives \getMyNumber{TryAgain}.
\subsection{Section 1.2}
At this point the command \setMyNumber{ItWorks} seems to work again: \getMyNumber{ItWorks}
\end{document}

请注意,我已对行尾进行注释以删除多余的空格。

但也许\label\ref你想做的事:

\documentclass[a4paper]{scrartcl}
\begin{document}
\section{Section 1}
\subsection{Section 1.1}
\subsubsection{Section 1.1.1}
The current number is \thesubsubsection.
\subsubsection{Section 1.1.2}\label{Test}
Now the number is \thesubsubsection.\\
And \verb|\label{Test}| refers  to \ref{Test}.
\subsubsection{Section 1.1.3}\label{TryAgain}
Here \verb|\label{TryAgain}| refers to \ref{TryAgain}.
\subsection{Section 1.2}\label{ItWorks}
At this point the command \verb|\label{ItWorks}| seems to work again: \ref{ItWorks}
\end{document}

运行两次即可获得

在此处输入图片描述

相关内容