我如何引用当前(子)章节编号?

我如何引用当前(子)章节编号?

我想创建一个宏,动态返回我所在的当前章节、节、子节(等等)。我创建了一个名为 的宏来\getCurrentSectionNumber执行此操作,但它缺乏确定深度的级别并相应地缩短/延长结果的能力。此宏本质上应该返回调用它的最近子节、节或章节的完整编号文本。

该宏应该在给定的位置生成以下文本。

  • 答:“1”
  • B:“1.1”
  • C:“1.1.1”
  • D:“1.1.2”
  • E:“1.2”
  • F:“1.2.1”
  • G:“1.2.2”

请注意,在 A 处,它打印“1.??”,因为该点处没有定义任何部分。位置 C、D、F 和 G 均省略了该子部分。

例子:

\documentclass{report}
\usepackage{etoolbox}


\newcommand\getcurrentref[1]{%
 \ifnumequal{\value{#1}}{0}
  {??}
  {\the\value{#1}}%
} 
\newcommand{\getCurrentSectionNumber}{\getcurrentref{chapter}.\getcurrentref{section}}


\begin{document}

\chapter{My Example}
A: Current number is \getCurrentSectionNumber

\section{Section One-point-One}
    B: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-one-point-one}
        C: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-one-point-two}
        D: Current number is \getCurrentSectionNumber
\section{Section Two}
    E: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-two-point-one}
        F: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-two-point-two}
        G: Current number is \getCurrentSectionNumber

\end{document}

示例输出:

上面给出的示例代码的输出

任何帮助都将不胜感激。谢谢!

答案1

您的意思是这样的吗?我们只是利用了这样一个事实:0当该分区级别当前未使用时,分区编号始终为“较低”。

\documentclass{report}

\makeatletter
\newcommand{\getCurrentSectionNumber}{%
  \ifnum\c@section=0 %
  \thechapter
  \else
  \ifnum\c@subsection=0 %
  \thesection
  \else
  \ifnum\c@subsubsection=0 %
  \thesubsection
  \else
  \thesubsubsection
  \fi
  \fi
  \fi
}

\makeatother

\begin{document}

\chapter{My Example}
A: Current number is \getCurrentSectionNumber %1

\section{Section One-point-One}
    B: Current number is \getCurrentSectionNumber % 1.1
    \subsection{subsection one-point-one-point-one}
        C: Current number is \getCurrentSectionNumber % 1.1.1
    \subsection{subsection one-point-one-point-two}
        D: Current number is \getCurrentSectionNumber % 1.1.2
\section{Section Two}
    E: Current number is \getCurrentSectionNumber % 1.2
    \subsection{subsection one-point-two-point-one}
        F: Current number is \getCurrentSectionNumber % 1.2.1
    \subsection{subsection one-point-two-point-two}
        G: Current number is \getCurrentSectionNumber % 1.2.2

\end{document}

答案2

您必须从上到下检查每个计数器,并在到达值为零的计数器时停止。

\documentclass{book}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\getCurrentSectionNumber}{}
 {
  \jvri_get_current:
 }
% the list of examined counters
\clist_const:Nn \c_jvri_counters_clist 
 {
  chapter,section,subsection,subsubsection,paragraph,subparagraph
 }

% for storing the previous counter name
\tl_new:N \l_jvri_previous_tl

\cs_new_protected:Npn \jvri_get_current:
 {
  \tl_set:Nn \l_jvri_previous_tl { part } % for safety
  \clist_map_inline:Nn \c_jvri_counters_clist
   {
    \int_compare:nTF { \arabic{##1} = 0 }
     {% if the counter is zero, break and use the former \the<counter>
      \clist_map_break:n { \use:c { the \l_jvri_previous_tl } }
     }
     {% store the counter's name
      \tl_set:Nn \l_jvri_previous_tl { ##1 }
     }
   }
 }
\ExplSyntaxOff

\begin{document}

Zero: \getCurrentSectionNumber (will have no output)

\chapter{My Example}
A: Current number is \getCurrentSectionNumber

\section{Section One-point-One}
B: Current number is \getCurrentSectionNumber

\subsection{subsection one-point-one-point-one}
C: Current number is \getCurrentSectionNumber

\subsection{subsection one-point-one-point-two}
D: Current number is \getCurrentSectionNumber

\section{Section Two}
E: Current number is \getCurrentSectionNumber

\subsection{subsection one-point-two-point-one}
F: Current number is \getCurrentSectionNumber

\subsection{subsection one-point-two-point-two}
G: Current number is \getCurrentSectionNumber

\chapter{Again}
H: Current number is \getCurrentSectionNumber

\end{document}

这很容易扩展以管理更低的级别。

您会看到“零:”后面什么都没有;可以添加检查以检查最终序列是否为空。

在此处输入图片描述

在此处输入图片描述

答案3

与 tohecz 的好答案相比,这是另一种方法,只需使用标签命令引用最后调用的切片命令即可。

\documentclass{book}

\usepackage[hidelinks]{hyperref}
\newcounter{GN}
\setcounter{GN}{0}
\newcommand{\GetNumber}[0]{\stepcounter{GN}\label{GN:\theGN}\ref{GN:\theGN}}

\begin{document}

\chapter{Foo}
Current number is: \GetNumber
\section{Bar}
Current number is: \GetNumber
\subsection{Baz}
Current number is: \GetNumber
\subsection{Baz2}
Current number is: \GetNumber
\section{Bar2}
Current number is: \GetNumber

\end{document}

输出如下:

输出

编辑这是一个改进我的 MWE 版本来展示该方法对equation和浮点的稳健性。

\documentclass{book}

\usepackage{float}
\usepackage[hidelinks]{hyperref}
\newcounter{GN}
\setcounter{GN}{0}
\newcommand{\GetNumber}[0]{\stepcounter{GN}\label{GN:\theGN}\ref{GN:\theGN}}

\begin{document}

\chapter{Foo}
Current number is: \GetNumber
\section{Bar}
Current number is: \GetNumber
\subsection{Baz}

\begin{equation}c=a+b\end{equation}

\noindent
Current number is: \GetNumber
\subsection{Baz2}

\begin{figure}[H]
\caption{Figure to test the robustness}
\end{figure}

Current number is: \GetNumber
\section{Bar2}
Current number is: \GetNumber

\end{document}

这是它的输出,然而正如 tohecz 和 egreg 在评论中指出的那样,只有当你\GetNumber在计数器已经存在的环境中调用时,此解决方案才有效参考步进

输出2

答案4

如果您能够检索当前的分段级别(我们称之为\currsecnumdepth),您也可以使用普通的旧版本\ifcase

\newcommand*{\getCurrentSectionNumber}{\ifcase\currsecnumdepth\thechapter\or\thesection
\or\thesubsection\or\thesubsubsection\or\theparagraph\or\thesubparagraph\fi}

在下面的例子中,我提供了一种(诚然令人不快的)方法来确保上述句子的前提成立:

\documentclass{report}

\makeatletter
\newcount\currsecnumdepth
%verbose from the standard classes, except line 4
\renewcommand{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi
                        \thispagestyle{plain}%
                        \global\@topnum\z@
                        \global\currsecnumdepth\z@
                        \@afterindentfalse
                        \secdef\@chapter\@schapter}
%verbose from latex.ltx, except line 2
\renewcommand{\@startsection}[6]{%
  \global\currsecnumdepth=#2
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
\makeatother

\newcommand*{\getCurrentSectionNumber}{\ifcase\currsecnumdepth\thechapter\or\thesection
\or\thesubsection\or\thesubsubsection\or\theparagraph\or\thesubparagraph\fi}

\begin{document}

\chapter{My Example}
A: Current number is \getCurrentSectionNumber

\section{Section One-point-One}
    B: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-one-point-one}
        C: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-one-point-two}
        D: Current number is \getCurrentSectionNumber
\section{Section Two}
    E: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-two-point-one}
        F: Current number is \getCurrentSectionNumber
    \subsection{subsection one-point-two-point-two}
        G: Current number is \getCurrentSectionNumber

\end{document}

cursecnmb 演示

相关内容